]> arthur.barton.de Git - atom-ax-pipe.git/blob - lib/pipe.coffee
8db43da6be01e501a02c1809e6682f3a6f9f3b19
[atom-ax-pipe.git] / lib / pipe.coffee
1 {Range} = require 'atom'
2 {spawn} = require 'child_process'
3 CommandView = require './command-view'
4
5 history = []
6
7 module.exports =
8   activate: ->
9     atom.workspaceView.command 'pipe:run', => @run()
10
11   run: ->
12     editor = atom.workspace.getActiveEditor()
13     view = atom.workspaceView.getActiveView()
14     return if not editor?
15
16     new CommandView history, (commandString) ->
17       if not commandString
18         view.focus()
19         return
20
21       history.push commandString
22       if history.length > 300
23         history.shift()
24
25       range = editor.getSelectedBufferRange()
26       stdout = ''
27       stderr = ''
28
29       proc = spawn process.env.SHELL, ["-l", "-c", commandString]
30
31       proc.stdout.on 'data', (text) ->
32         stdout += text
33
34       proc.stderr.on 'data', (text) ->
35         stderr += text
36
37       proc.on 'close', (code) ->
38         editor.setTextInBufferRange(range, stderr || stdout)
39         editor.setSelectedBufferRange(new Range(range.start, range.start))
40         view.focus()
41
42       proc.stdin.write(editor.getSelectedText())
43       proc.stdin.end()