根据 svn 状态对文件名着色

发布于 2024-10-11 14:15:10 字数 102 浏览 0 评论 0原文

当调用ls时,我希望文件名根据其颠覆状态具有不同的颜色。例如,添加的文件将显示为青色,修改的文件将显示为红色等。仅靠 bash 的力量就可以实现吗?这方面有准备吗?

When invoking ls, I would like to have file names with a different color depending on their subversion status. For example, an added file will be cyan, a modified file red and so on. Is it possible with the bare power of bash? Is there something ready on this regard ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-10-18 14:15:10

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

As far as I know, it is not possible to achieve that with pure bash (scripting aside).

You can quite easily get colorised file listing using scripts (bash, python, perl, whatever your poison). Here's a rather crude proof-of-concept implementation written in 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)
横笛休吹塞上声 2024-10-18 14:15:10

这是一个要点,其中包含第三代小脚本,用于对 SVN 输出进行着色。它非常适合 svn status 命令。我刚刚将 alias svns="/path/to/svn-color.py status" 添加到我的 .bash_profile 中,现在我可以输入 svns并查看颜色编码的输出。作者建议将 svn 默认为他的脚本。

Here's a Gist with the 3rd generation of a small script to colorize SVN output. It works perfectly for svn status commands. I just added alias svns="/path/to/svn-color.py status" to my .bash_profile and now I can type svns and see the color-coded output. The author recommends making svn default to his script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文