簡體   English   中英

如何從 Java 程序內部執行 vimdiff 命令

[英]How to execute vimdiff command from inside a java program

我有一個文件列表,我必須運行 vimdiff 命令並將輸出保存為 html 文件。我正在使用 Java 執行此操作。 下面是我試圖執行的命令

String cmd = "vimdiff -c 'set foldlevel=9999' src/test/resources/testdata/output/html_output_before_changes/file1.html src/test/resources/testdata/output/html_output_after_changes/file2.html -c TOhtml -c 'w! different.html' -c 'qa!'"

當我運行下面的代碼時,代碼正在執行。 但是我看不到生成的文件。

Runtime rt = Runtime.getRuntime();
Process process = rt.exec(cmd);

從終端執行時,該命令運行良好。 但是在 java 程序中執行時它不起作用。 有人可以幫我解決這個問題嗎? 我做了很多搜索,但無法繼續進行。

您正在使用:TOhtml並將結果寫為different.html 如果您不確定該文件的位置,請檢查 Java 進程的當前工作目錄,對硬盤進行文件搜索,或在 Vim 命令中指定絕對路徑以確保確定。

你不會從 Vim 的操作中看到任何東西。 使用process. getInputStream() ,您可以獲取 Vim 在其操作期間寫入終端的內容,但這只會造成亂碼,因為 Vim 使用特殊的 ANSI 轉義序列來控制終端、定位光標等。

要以非交互方式使用 Vim,建議傳遞以下選項:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-n                No swapfile.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                  user's settings).
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                  with messages or output to avoid blocking.

由於無法與 Vim 交互(從 Java 程序內部),故障排除提示是啟用詳細日志記錄:您可以使用-V20vimlog捕獲 Vim 會話的完整日志。 退出 Vim 后,檢查vimlog日志文件是否有錯誤。

兩天后,我找到了以下解決方案:

我將 vimdiff 命令添加到 shell 腳本並使用以下方法執行它,它就像一個 gem。

Java方法

try {
            File[] uiDiffDir = getFiles();

            for (File file : uiDiffDir) {

                String[] cmd = {"sh", shellScriptPath, file1, file2,
                        outputfile};
                Process p = Runtime.getRuntime().exec(cmd);
                p.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

外殼文件

vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'

筆記:

  • file1 將作為參數傳遞來代替 $1
  • file2 將作為參數傳遞來代替 $2
  • outputfile 將作為參數傳遞來代替 $3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM