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