当我有一个差异时,我如何给它上色,使它看起来更好?
我想它的命令行,所以请没有GUI解决方案。
当我有一个差异时,我如何给它上色,使它看起来更好?
我想它的命令行,所以请没有GUI解决方案。
当前回答
对我来说,我找到了一些解决方案:这是一个有效的解决方案
@echo off
Title a game for YouTube
explorer "https://thepythoncoding.blogspot.com/2020/11/how-to-echo-with-different-colors-in.html"
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
echo say the name of the colors, don't read
call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
echo(
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"
goto :Beginoffile
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
:Beginoffile
其他回答
对我来说,我找到了一些解决方案:这是一个有效的解决方案
@echo off
Title a game for YouTube
explorer "https://thepythoncoding.blogspot.com/2020/11/how-to-echo-with-different-colors-in.html"
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
echo say the name of the colors, don't read
call :ColorText 0a "blue"
call :ColorText 0C "green"
call :ColorText 0b "red"
echo(
call :ColorText 19 "yellow"
call :ColorText 2F "black"
call :ColorText 4e "white"
goto :Beginoffile
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
:Beginoffile
由于wdiff接受在插入和删除的开头和结尾指定字符串的参数,您可以使用ANSI颜色序列作为这些字符串:
wdiff -n -w $'\033[30;41m' -x $'\033[0m' -y $'\033[30;42m' -z $'\033[0m' file1 file2
例如,这是比较两个CSV文件的输出:
来自2.2的例子wdiff的实际使用示例。
下面是另一个解决方案,它调用sed来插入适当的颜色ANSI转义序列,分别以红色、绿色和青色显示+、-和@行。
diff -u old new | sed "s/^-/$(tput setaf 1)&/; s/^+/$(tput setaf 2)&/; s/^@/$(tput setaf 6)&/; s/$/$(tput sgr0)/"
与此问题的其他解决方案不同,此解决方案没有显式地阐明ANSI转义序列。相反,它调用tput setaf和tput sgr0命令来生成ANSI转义序列,分别设置适当的颜色和重置终端属性。
要查看tput setaf的每个参数的可用颜色,使用以下命令:
for i in {0..255}; do tput setaf $i; printf %4d $i; done; tput sgr0; echo
下面是输出的样子:
下面是tput setaf和tput sgr0命令生成适当的ANSI转义序列的证据:
$ tput setaf 1 | xxd -g1
00000000: 1b 5b 33 31 6d .[31m
$ tput setaf 2 | xxd -g1
00000000: 1b 5b 33 32 6d .[32m
$ tput setaf 6 | xxd -g1
00000000: 1b 5b 33 36 6d .[36m
$ tput sgr0 | xxd -g1
00000000: 1b 28 42 1b 5b 6d .(B.[m
彩色,字级差异输出
下面是你可以用下面的脚本和diff-highlight做的事情:
#!/bin/sh -eu
# Use diff-highlight to show word-level differences
diff -U3 --minimal "$@" |
sed 's/^-/\x1b[1;31m-/;s/^+/\x1b[1;32m+/;s/^@/\x1b[1;34m@/;s/$/\x1b[0m/' |
diff-highlight
(这要归功于@retracile对sed高亮的回答)
到目前为止还没有人提到delta。它支持带有语法高亮的语法彩色差异视图。