2

複製 Command 視窗彩色輸出文字

 2 years ago
source link: https://blog.darkthread.net/blog/copy-cmd-output-with-color/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

複製 Command 視窗彩色輸出文字

calendar.svg 2021-09-09 09:19 PM comment.svg 0 eye.svg 1,565

這些年隨著 PowerShell、Git、.NET Core 愈玩愈多,使用 CLI (Command Line Interface) 的機會也多了起來,以 Git 為例,我偏好開 Cmder 下指令勝過操作 GUI 軟體,理由是打字對眼睛跟運動神經的負擔比滑滑鼠小很多,大腦能更專注於思考,上手後敲指令操作比 GUI 更快更精準。

但有個小困擾,有時我想將 Cmder 內容複製貼上到 Word/Outlook,沒法保留顏色標示。以 git diff 為例,原本刪除、新增會以不同顏色標示,複製文字貼到 Word 只剩黑白,若要保留顏色只能改用抓圖,損失複製文字再利用的優勢:

原本覺得這是天生限制,默默接受多年。今天有個重要需求得保留顏色,才認真研究了一下,發現 Cmder 有支援:

有兩種格式可資利用:Copy HTML Format 及 Copy as HTML,前者即可在 Word 貼上同樣式(背景、顏色、字型)的文字,後者則是貼上包含樣式的 HTML 原始碼。

美中不足的是 Cmder 會將畫面空白轉成 HTML 的   (即圖中的成串小方格):

Cmder 複製功能有不少微調選項,但我沒找到忽略空白部分的方法;

後來改試了 Windows Terminal,驚喜發現 WT 也有提供複製文字選項,支援純文字、HTML、RTF、HTML+RTF:

而且貼在 Word 的效果甚好:(上半段是貼上 HTML 格式的效果,下半段是 RTF)

未來想要複製包含顏色樣式的 Cmder 內容,就用 Windows Terminal 吧。

and has 0 comments

Comments

Be the first to post a comment

Post a comment

Comment
Name Captcha 69 - 17 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK