2011-01-12 42 views
14

当调用ls时,我想根据它们的颠覆状态使用不同颜色的文件名。例如,添加的文件将是青色,修改后的文件是红色等等。 bash的裸机有可能吗?这方面有没有准备好?根据svn状态着色文件名

回答

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) 
+0

对于那些仍在使用svn。 有python的svn绑定,可能比运行子进程更优雅,你也可以着色其他命令。 – 2016-09-01 03:06:18

3

Here's a Gist与第三代小脚本的上色SVN输出。它适用于svn status命令。我刚刚将alias svns="/path/to/svn-color.py status"添加到我的.bash_profile,现在我可以输入svns并查看颜色编码输出。作者建议将svn默认为他的脚本。