根据svn状态着色文件名

Ste*_*ini 14 svn bash ls colors

在调用时ls,我希望文件名具有不同的颜色,具体取决于它们的subversion状态.例如,添加的文件将为青色,修改后的文件为红色,依此类推.是否有可能用bash的力量?在这方面有什么准备好了吗?

Sha*_*hin 5

据我所知,使用纯 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)
Run Code Online (Sandbox Code Playgroud)