]> arthur.barton.de Git - atom-ax-pipe.git/blobdiff - lib/pipe.coffee
Make it work when a file is opened without a project
[atom-ax-pipe.git] / lib / pipe.coffee
index 7a3b9658495fbd3394c2d62cff31bfbd28027dd4..5294840bd507d0833bc6a85f296f20a40320056f 100644 (file)
@@ -2,6 +2,8 @@
 {spawn} = require 'child_process'
 CommandView = require './command-view'
 
+history = []
+
 module.exports =
   activate: ->
     atom.workspaceView.command 'pipe:run', => @run()
@@ -11,33 +13,40 @@ module.exports =
     view = atom.workspaceView.getActiveView()
     return if not editor?
 
-    new CommandView (commandString) ->
+    new CommandView history, (commandString) ->
       if not commandString
         view.focus()
         return
 
-      range = editor.getSelectedBufferRange()
-      stdout = ''
-      stderr = ''
+      history.push commandString
+      if history.length > 300
+        history.shift()
 
-      proc = spawn process.env.SHELL, ["-l", "-c", commandString]
+      if atom.project.rootDirectory?
+        commandString = "cd '#{atom.project.rootDirectory.path}' && #{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) ->
-        text = stderr || stdout
-        if not text then return
+processRange = (marker, editor, commandString) ->
+  stdout = ''
+  stderr = ''
 
-        if text.slice(-1) is '\n'
-          text = text.slice(0, -1)
+  proc = spawn process.env.SHELL, ["-l", "-c", commandString]
 
-        editor.setTextInBufferRange(range, text)
-        editor.setSelectedBufferRange(new Range(range.start, range.start))
-        view.focus()
+  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()