gpt4 book ai didi

svn - 根据 svn 状态为文件名着色

转载 作者:行者123 更新时间:2023-12-04 18:34:41 24 4
gpt4 key购买 nike

调用 ls 时,我希望文件名具有不同的颜色,具体取决于它们的颠覆状态。例如,添加的文件将为青色,修改的文件为红色等。 bash 的强大功能是否有可能?在这方面有什么准备吗?

最佳答案

据我所知,用纯 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)

关于svn - 根据 svn 状态为文件名着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com