2017/11/8

mac - 移除所有 ANSI escape code

當你用 rspec 跑測試並且生成結果檔案時,檔案的內容可能會包含 ANSI escape code 所表示的顏色。

舉例來說,如果你用以下程式跑測試:

rspec --format documentation --out result.txt

你可以使用 cat 去讀他

cat result.txt

結果看起來像這樣:

但是打開 result.txt 時,你會看到這樣的東西:

...
Finished in 1.76 seconds (files took 6.55 seconds to load)
 [32m1 example, 0 failures [0m

Randomized with seed 1

[32m [0m 代表的就是顏色,這被稱為 ANSI escape code

但是我希望讓他消失,在請教大大之後得到一個不錯的解法,有人用 node.js 做了一個簡單的指令strip-ansi

安裝方法(如果你有node.js)

# 這裡是 bash
npm install --global strip-ansi-cli

使用方法

# 基本用法
strip-ansi 字串

# 透過 |
cat 輸入檔案路徑 | strip-ansi > 輸出檔案路徑

沒有留言: