调用ls
时,我希望文件名具有不同的颜色,具体取决于其颠覆状态。例如,添加的文件将为青色,修改后的文件为红色,依此类推。是否有可能用bash的力量?在这方面有什么准备好了吗?
答案 0 :(得分:4)
据我所知,用纯粹的bash(脚本除外)是不可能实现的。
你可以很容易地使用脚本(bash,python,perl,无论你的毒药)获得彩色文件列表。这是一个用python编写的相当粗略的概念验证实现:https://gist.github.com/776093
#!/usr/bin/env python
import re
from subprocess import Popen, PIPE
colormap = {
"M" : "31", # red
"?" : "37;41", # grey
"A" : "32", # green
"X" : "33", # yellow
"C" : "30;41", # black on red
"-" : "31", # red
"D" : "31;1", # bold red
"+" : "32", # green
}
re_svnout = re.compile(r'(.)\s+(.+)$')
file_status = {}
def colorise(line, key):
if key in colormap.keys():
return "\001\033[%sm%s\033[m\002" % (colormap[key], line)
else:
return line
def get_svn_status():
cmd = "svn status"
output = Popen(cmd, shell=True, stdout=PIPE)
for line in output.stdout:
match = re_svnout.match(line)
if match:
status, f = match.group(1), match.group(2)
# if sub directory has changes, mark it as modified
if "/" in f:
f = f.split("/")[0]
status = "M"
file_status[f] = status
if __name__ == "__main__":
get_svn_status()
for L in Popen("ls", shell=True, stdout=PIPE).stdout:
line = L.strip()
status = file_status.get(line, False)
print colorise(line, status)
答案 1 :(得分:3)
Here's a Gist使用第3代小脚本来着色SVN输出。它适用于svn status
命令。我刚刚将alias svns="/path/to/svn-color.py status"
添加到.bash_profile
,现在我可以输入svns
并查看颜色编码的输出。作者建议将svn
默认为他的脚本。