]> arthur.barton.de Git - atom-ax-pipe.git/blobdiff - lib/pipe.coffee
Execute command once per selection range. Resolve #2.
[atom-ax-pipe.git] / lib / pipe.coffee
index 94b5627eb539661bac953ed1fbd42e047d924488..a85b6348e9b88ac50258d0c9a5f2c7237175111f 100644 (file)
@@ -22,23 +22,30 @@ module.exports =
       if history.length > 300
         history.shift()
 
-      range = editor.getSelectedBufferRange()
-      stdout = ''
-      stderr = ''
-
       commandString = "cd '#{atom.project.path}' && #{commandString}"
-      proc = spawn process.env.SHELL, ["-l", "-c", commandString]
+      properties = { reversed: true, invalidate: 'never' }
 
-      proc.stdout.on 'data', (text) ->
-        stdout += text
+      for range in editor.getSelectedBufferRanges()
+        marker = editor.markBufferRange range, properties
+        processRange marker, editor, commandString
 
-      proc.stderr.on 'data', (text) ->
-        stderr += text
+      view.focus()
 
-      proc.on 'close', (code) ->
-        editor.setTextInBufferRange(range, stderr || stdout)
-        editor.setSelectedBufferRange(new Range(range.start, range.start))
-        view.focus()
+processRange = (marker, editor, commandString) ->
+  stdout = ''
+  stderr = ''
+
+  proc = spawn process.env.SHELL, ["-l", "-c", commandString]
+
+  proc.stdout.on 'data', (text) ->
+    stdout += text
+
+  proc.stderr.on 'data', (text) ->
+    stderr += text
+
+  proc.on 'close', (code) ->
+    text = stderr || stdout
+    editor.setTextInBufferRange(marker.getBufferRange(), text)
 
-      proc.stdin.write(editor.getSelectedText())
-      proc.stdin.end()
+  proc.stdin.write(editor.getTextInBufferRange(marker.getBufferRange()))
+  proc.stdin.end()