#command #command-line-tool #utility #terminal #parser #cli-parser #cli

已删除 clrr

简单的命令行工具,可以为默认不带颜色的命令添加颜色

1.0.0 2022年7月11日

#79#cli-parser

MIT 许可证

255KB
324 代码行

Colorer

Crates.io GitHub last commit

Colorer 是一个简单且可扩展的文本解析命令行工具,旨在为默认不带颜色的命令添加颜色。

该命令使用用户定义的配置文件。

安装

使用 Cargo 安装

如果已安装 Rust 工具链,则可以使用 cargo 安装 colorer

cargo install colorer # from crates.io

或者,在克隆此存储库后,运行

cargo install --path .

从发布版安装

安装此程序的另一种方法是下载发布页面上的二进制文件。有多种选择

  • 下载二进制文件并将其放入路径中(手动创建包含颜色文件的目录)
  • 下载软件包(deb 或 rpm)并安装它。这将还会创建包含配置文件的目录。

用法

程序在 /etc/colorer 中查找配置文件。此存储库包含在 color_patters 目录中提供的某些基本配置文件。

要使用 colorer,将需要运行的命令作为参数传递,例如

colorer -- df -h

定义新的配置文件

用于定义颜色方案的文件是简单的 TOML 文件,以命令命名(例如 ls.toml)。颜色方案包含一个 command 向量,其中每个组件包含以下变量

  • regex:用于查找文本的正则表达式。此值是必需的。
  • default decorator:它包含一个颜色数组。数组用于可以添加背景颜色、前景颜色以及粗体、斜体。此值也是必需的。
  • optionals_decorators:此值是可选的,包含使用正则表达式找到的字符串的替代值数组。元素表示为一个元组,其中第一个值包含一个正则表达式,它只搜索通过必需的正则表达式找到的内容的图案,以及装饰器的列表。

以下是一个为 df 定义的示例片段,用于突出显示表示为百分比的用法。

[[command]]
regex = "(?<=(\\b(\\d+((\\.|\\,)\\d+)?(\\w+)?\\s+){3}))\\d+%"   # main regex.
                                                                # This searches for all
                                                                # percentages
default_decorator = ["GreenFg"] # this is the default decorator and it is used 
                                # if none of the optional decorators are used.
                                # By exclusion, for this example, it includes 
                                # all the values from 0% up to 49%
optional_decorators = [
    # the following regexs apply only for the text extracted by the main regex
    [
        "([5-7][0-9])%", # values from 50% up to 79%
        ["YellowFg"] # yellow text
    ], 
    [
        "([8][0-9]|[9][0-6])%", # values from 80% up to 96%
        ["MagentaFg"] # magenta text
    ], 
    [
        "([9][7-9]|100)%", # values above 97%
        ["RedBg"] # red backgroud
    ]
]

可以为命令(如 dfpingnmap)以及子命令(如 docker psdocker search)定义颜色方案。对于子命令的定义,将 command 替换为 subcommand.<subcommand>。例如,在 docker search 的情况下

[[subcommand.search]]
regex = "(^[^NAME]\\w+(?=\\s)|(?<=(^\\w+\\/))\\S+)"
default_decorator = ["YellowFgBright", "Bold"]

注意

如果使用 colorer 来别名命令或使用选项时,始终使用双短横线是一个好主意。例如,通过定义 ll='colorer ls -lahF'll --help 将会打印来自 colorer 的帮助信息。这个问题可以通过 ll='colorer -- ls -lahF' 来避免

屏幕截图

df screenshot

ping screenshot

nmap screenshot

依赖项

~6–14MB
~217K SLoC