共同工作流程是:
- 呼叫者應用程序創建的臨時文件;
- 確定默認編輯器(對於基於Debian的編輯器,它將爲
/usr/bin/editor
,對於其他的編譯器 - 編譯器變量的內容爲$EDITOR
等);
- 在子窗體中運行shell命令(不帶反引號!
- 等待它退出;
- 確定退出代碼,如果不是
0
,則跳過以下代碼;
- 讀取在步驟1中創建的臨時文件的內容並刪除該文件。
在紅寶石這將是這樣的:
▶ f = Tempfile.new 'cocoapods'
#⇒ #<File:/tmp/am/cocoapods20151120-6901-u2lubx>
-rw------- 1 am am 0 nov 20 15:03 /tmp/am/cocoapods20151120-6901-u2lubx
▶ path = f.path
#⇒ "/tmp/am/cocoapods20151120-6901-u2lubx"
▶ f.puts 'This content is already presented in file'
#⇒ nil
▶ f.close # HERE MUST BE ENSURE BLOCK, BUT FOR THE SAKE OF AN EXAMPLE...
#⇒ nil
▶ system "editor #{path}"
#⇒ Vim: Warning: Output is not to a terminal
如果您在控制檯測試此,只需要輸入任何東西,然後Esc鍵:瓦特q。在現實生活中,會有正常的vim
(或默認編輯器)打開。
▶ File.read path
#⇒ "GGGGGGGGGThis content is already presented in file\n"
一起:
#!/usr/bin/env ruby
require 'tempfile'
f = Tempfile.new 'cocoapods'
path = f.path
f.puts 'This content is already presented in file'
f.close # HERE MUST BE ENSURE BLOCK, BUT FOR THE SAKE OF AN EXAMPLE...
system "editor #{path}"
puts File.read path
的'interactive_editor'寶石似乎與你試圖達到的目標。我沒有檢查消息來源,但可能有想法可以選擇。 –