[英]How to run an interactive CLI program from within Clojure?
我想從Clojure(例如vim)中運行一個交互式CLI程序,並能夠與其交互。
在bash和其他編程語言中,我可以使用
vim > `tty`
我試圖在Clojure中做同樣的事情:
(require '[clojure.java.shell :as shell])
(shell/sh "vim > `tty`")
但是它只是打開vim而沒有給我tty。
背景:我正在開發一種Clojure CLI工具,該工具可以解析電子郵件,並允許用戶編輯已解析的數據,然后再將其保存到磁盤上。 它的工作方式如下:
此代碼依賴於Clojure Java互操作來利用Java的ProcessBuilder
類。
(defn -main
[]
;use doseq instead of for because for is lazily evaluated
(doseq [i [1 2 3]]
;extract current directory from system variable
(let [file-name (str "test" i ".txt")
working-directory (trim-newline (:out (sh "printenv" "PWD")))]
(spit file-name "")
;this is where fun begins. We use ProcessBuilder to forward commands to terminal
;we pass a list of commands and their arguments to its constructor
(let [process-builder (java.lang.ProcessBuilder. (list "vim" (str working-directory "/" file-name)))
;inherit is a configuration constant
inherit (java.lang.ProcessBuilder$Redirect/INHERIT)]
;we configure input, output and error redirection
(.redirectOutput process-builder inherit)
(.redirectError process-builder inherit)
(.redirectInput process-builder inherit)
;waitFor used to block execution until vim is closed
(.waitFor (.start process-builder))
)
;additional processing here
)
)
;not necessary but script tends to hang for around 30 seconds at end of its execution
;so this command is used to terminate it instantly
(System/exit 0)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.