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