如何将 TextMate 中的一行发送到在终端窗口中运行的 irb 进程?

发布于 2024-10-09 04:37:31 字数 294 浏览 8 评论 0原文

我经常在打开 TextMate 窗口并在其旁边的终端窗口中运行 irb 进程的情况下进行编码。我希望能够在 TextMate 中按下一个按键序列,该序列执行以下操作:

  1. 复制当前选择,或者如果没有,则复制当前行。
  2. 将其粘贴到运行 irb 的最上面的终端窗口中。
  3. 按 Enter 键,以便在 irb 窗口中执行该行代码。

我在 R 中编码时使用了这种交互式开发风格,发现它非常方便。我很确定 emacs 和 SLIME 也可以让你像这样工作。 Ruby 和 TextMate 可以吗?

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

  1. Copies the current selection, or if there is none, the current line.
  2. Pastes it in the topmost Terminal window that is running irb.
  3. Presses enter so that the line of code is executed in the irb window.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-10-16 04:37:31

您必须创建捆绑命令和键盘快捷键才能执行此操作。

  1. 在 TextMate 中转到 Bundles ->;捆绑包编辑器
  2. 在我们自己的捆绑包中创建一个新命令。将其命名为“在终端中执行”,
  3. 将“保存”设置为“无”,将“输入”设置为所选文本,将“或”设置为行。
  4. 将“Output to Discard”设置
  5. 为“Activation”,选择您自己的快捷方式。我选择了 Apple Shift U
  6. 将下面的命令粘贴到“Command”框中(格式给我带来了麻烦)
  7. 关闭 Bundle Editor,然后选择 Bundles -> Bundle Editor - > 重新加载捆绑包
  8. 创建一个包含“puts“Hello World””行的新文档
  9. 在终端中打开 IRB
  10. 选择您刚刚在 Textmate 中编写的行,然后按键盘快捷键,

观察“Hello World”命令 :

#!/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`

You must create a Bundle Command and a keyboard shortcut to do this.

  1. In TextMate go to Bundles -> Bundle Editor
  2. Create a new Command inside our own Bundle. Call it "Execute in Terminal"
  3. Set "Save" to Nothing, set "Input" to Selected Text and "or" to Line.
  4. Set "Output to Discard
  5. In "Activation" choose your own shortcut. I chose Apple Shift U
  6. Paste the command below into the "Command" box (formatting is causing me trouble)
  7. Close the Bundle Editor and then choose Bundles -> Bundle Editor -> Reload Bundles
  8. Create a new document containing the line 'puts "Hello World"'
  9. Open up IRB in Terminal
  10. Select the line you have just written in Textmate and press your keyboard shortcut.
  11. Watch as "Hello World" appears in IRB.

The 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`
南风几经秋 2024-10-16 04:37:31

假设您不想查看终端,而是希望结果显示在 TextMate 中,就像在 Smalltalk 工作区中一样。

本质上,您希望在文本配合中运行 ruby​​,但希望在执行之间记住变量。你可以拥有那个。

(感谢 stef 有关如何添加新命令的说明)

  1. 运行 gem install daemons
  2. 运行IRB 服务器。该文件如下。
  3. 在 TextMate 中转到 Bundles ->;捆绑包编辑器
  4. 在我们自己的捆绑包中创建一个新命令。将其命名为“在终端中执行”,
  5. 将“保存”设置为“无”,将“输入”设置为所选文本,将“或”设置为行。
  6. 将“Output to Discard”设置
  7. 为“Activation”,选择您自己的快捷方式。我选择了 Apple Shift U
  8. 将下面的命令粘贴到“Command”框中(格式给我带来了麻烦)
  9. 关闭 Bundle Editor,然后选择 Bundles -> Bundle Editor - > 重新加载捆绑包
  10. 创建一个包含行 @@hi = "Hello World"@@hi + "ya" 的新文档
  11. 选择您刚刚编写的第一行Textmate 并按键盘快捷键,
  12. 对第二行执行相同操作,
  13. 观察“hiya”出现在 text mate 中

:命令

#!/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

#!/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

Suppose you don't want to look over at the Terminal, but instead want the result to show up in TextMate, like in a Smalltalk workspace.

In essence, you want to run ruby within text mate, but you want variables to be remembered between executions. You can have that.

(Thanks stef for instructions on how to add a new command)

  1. Run gem install daemons
  2. Run an irb server. The file is below.
  3. In TextMate go to Bundles -> Bundle Editor
  4. Create a new Command inside our own Bundle. Call it "Execute in Terminal"
  5. Set "Save" to Nothing, set "Input" to Selected Text and "or" to Line.
  6. Set "Output to Discard
  7. In "Activation" choose your own shortcut. I chose Apple Shift U
  8. Paste the command below into the "Command" box (formatting is causing me trouble)
  9. Close the Bundle Editor and then choose Bundles -> Bundle Editor -> Reload Bundles
  10. Create a new document containing the lines @@hi = "Hello World" and @@hi + "ya"
  11. Select the first line you have just written in Textmate and press your keyboard shortcut.
  12. Do the same with the second line
  13. Watch as "hiya" appears in text mate.

The 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

The 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文