module.exports =
activate: ->
- atom.workspaceView.command 'pipe:run', => @run()
+ atom.commands.add 'atom-workspace', "pipe:run", => @run()
run: ->
editor = atom.workspace.getActiveEditor()
if history.length > 300
history.shift()
- commandString = "cd '#{atom.project.path}' && #{commandString}"
+ if atom.project.rootDirectory?
+ commandString = "cd '#{atom.project.rootDirectory.path}' && #{commandString}"
properties = { reversed: true, invalidate: 'never' }
- for range in editor.getSelectedBufferRanges()
- marker = editor.markBufferRange range, properties
- processRange marker, editor, commandString
+ ranges = editor.getSelectedBufferRanges()
+ wg = new WaitGroup ->
+ view.focus()
- view.focus()
+ wg.add(ranges.length)
+
+ for range, i in ranges
+ marker = editor.markBufferRange range, properties
+ processRange marker, editor, commandString, wg
-processRange = (marker, editor, commandString) ->
+processRange = (marker, editor, commandString, wg) ->
stdout = ''
stderr = ''
proc.on 'close', (code) ->
text = stderr || stdout
editor.setTextInBufferRange(marker.getBufferRange(), text)
+ wg.done()
proc.stdin.write(editor.getTextInBufferRange(marker.getBufferRange()))
proc.stdin.end()
+
+class WaitGroup
+ constructor: (cb) ->
+ @n = 0
+ @cb = cb
+
+ add: (n) ->
+ @n += n
+
+ done: ->
+ @n -= 1
+ if @n <= 0
+ @cb()