How can I send a string in TextMate to an irb process running in a terminal window?

I often code with the TextMate window open and the irb process in the terminal window next to it. I want to be able to press a key sequence in TextMate, which does the following:

  • Copies the current selection, or if not, the current line.
  • Inserts it into the topmost terminal window on which irb is running.
  • Press the enter button to execute the line of code in the irb window.

I used this style of interactive development when coding in R and found it very convenient. I'm sure emacs and SLIME will also allow you to work this way. Is this possible with Ruby and TextMate?

+3
source share
2 answers

To do this, create a Bundle command and a keyboard shortcut.

  • In TextMate, go to Bundles -> Bundle Editor
  • Create a new team inside our own package. Name it Run in Terminal
  • Set "Save" to "No", set "Input" to the selected text and "or" to "Line".
  • Set "Exit to Uninstall"
  • In Activation, select your own shortcut. I chose Apple Shift U
  • Paste the command below into the "Command" field (formatting is causing me problems).
  • Close the Bundle Editor, then select Bundles β†’ Bundle Editor β†’ Reload Bundles
  • Create a new document containing the string "puts" Hello World "
  • Open IRB in terminal
  • Select the line you just wrote in Textmate and press the key combination.
  • Watch β€œHello World” appear on IRB.

Command:

#!/usr/bin/ruby input = STDIN.gets `osascript << EOF tell application "Terminal" activate end tell delay 1 tell application "System Events" keystroke "#{input.gsub('"', '\"')}" keystroke return end tell EOF` 
+6
source

Suppose you do not want to view the terminal, but instead want the result to appear in TextMate, for example, in the Smalltalk workspace.

Essentially, you want to run ruby ​​in a text mat, but you want variables to be remembered between executions. You can have it.

(Thanks to stef for instructions on adding a new command)

  • Run gem install daemons
  • Launch the irb server. The file is located below.
  • In TextMate, go to Bundles -> Bundle Editor
  • Create a new team inside our own package. Name it Run in Terminal
  • Set "Save" to "No", set "Input" to the selected text and "or" to "Line".
  • Set "Exit to Uninstall"
  • In Activation, select your own shortcut. I chose Apple Shift U
  • Paste the command below into the "Command" field (formatting is causing me problems).
  • Close the Bundle Editor, then select Bundles β†’ Bundle Editor β†’ Reload Bundles
  • Create a new document containing the lines @@hi = "Hello World" and @@hi + "ya"
  • Select the first line you just wrote in Textmate, and press the key combination.
  • Do the same with the second line
  • Watch how "hiya" appears in a text mat.

Irb server:

 #!/usr/bin/env ruby -w require 'rubygems' require 'daemons' require 'socket' LARGE = 100000000 PIPE = "/tmp/.irbservepipe" def kill_pipe `rm -f #{PIPE}` end def respond_to(pipe) inp = pipe.recv LARGE inp.nil? and return begin out = eval(inp) rescue Exception => e out = e end pipe.send out.inspect, 0 end def ensure_server ["EXIT", "INT", "HUP", "TERM"].each {|ea| trap( ea ) { kill_pipe }} File.exists?(PIPE) and kill_pipe server = UNIXServer.new(PIPE) loop { c = server.accept respond_to c c.close } end Daemons.daemonize ensure_server 

Command:

 #!/usr/bin/env macruby -w require 'socket' LARGE = 100000000 PIPE = "/tmp/.irbservepipe" input = STDIN.read socket = UNIXSocket.new(PIPE) socket.send input, 0 puts socket.recv LARGE 
0
source

Source: https://habr.com/ru/post/908077/


All Articles