]> arthur.barton.de Git - atom-ax-pipe.git/blob - lib/command-view.coffee
Use "atom.workspace.addBottomPanel"
[atom-ax-pipe.git] / lib / command-view.coffee
1 {View, TextEditorView} = require 'atom-space-pen-views'
2
3
4 module.exports =
5 class CommandView extends View
6   @placeholders: [
7     'sort -n'
8     'tac'
9     'sed \'s/^/\\/\\//g\''
10     'grep foo'
11     'tee ~/temp.txt'
12   ]
13
14   @content: ->
15     @div class: 'pipe-command', =>
16       @subview 'commandLine', new TextEditorView(
17         mini: true
18         placeholderText: @samplePlaceholder()
19       )
20
21   @samplePlaceholder: ->
22     @placeholders[Math.floor(Math.random()*@placeholders.length)]
23
24   initialize: (history, callback) ->
25     historyPos = history.length
26     cur = ''
27
28     @on 'core:cancel core:close', =>
29       callback(null)
30       @detach()
31     @on 'core:confirm', =>
32       callback(@commandLine.getText())
33       @detach()
34     @commandLine.on 'keydown', (e) =>
35       if history.length is 0 then return
36
37       switch e.keyCode
38         when 38 # up
39           unless historyPos <= 0
40             historyPos--
41             @commandLine.setText history[historyPos]
42
43         when 40 # down
44           if historyPos >= history.length-1
45             historyPos = history.length
46             @commandLine.setText cur
47           else
48             historyPos++
49             @commandLine.setText history[historyPos]
50
51         else
52           if historyPos >= history.length
53             cur = @commandLine.getText()
54
55     atom.workspace.addBottomPanel(item: this)
56     @commandLine.focus()