题
调用时 ls
, ,我想拥有具有不同颜色的文件名,具体取决于其颠覆状态。例如,添加的文件将是青色,修改后的文件红等。狂欢的裸露力量有可能吗?这方面准备好了吗?
解决方案
据我所知,不可能通过纯粹的bash(一边脚本)实现这一目标。
您可以很容易地使用脚本(无论您的毒药,bash,python,perl,perl,perl,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)
其他提示
这是要点 第三代小脚本以使SVN输出着色。它非常适合 svn status
命令。我刚刚添加 alias svns="/path/to/svn-color.py status"
对我的 .bash_profile
现在我可以输入 svns
并查看颜色编码的输出。作者建议制作 svn
默认为他的脚本。
不隶属于 StackOverflow