Sign in

    clicolor

    Terminal color and hyperlink escape sequences for MoonBit

    ansi
    color
    terminal
    cli
    style
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    11 hours ago
    Downloads
    8

    #clicolor

    终端颜色与超链接转义序列库,moonbit 工作区里渲染类模块(clitable 等)的基建。

    样式是不可变的 Style 值:颜色、属性、背景色是公开常量,链式返回新值,text 收尾产出串。坏色值按黑色降级,不中断渲染。

    #用法

    发布到 mooncakes 之后:

    moon add chensuiyi/clicolor

    唯一写法:@clicolor.<链首常量>.<链>.text(文本)。链首取实际存在的最前一项:有前景用前景色,没有前景用背景色,两色皆无才用 default(专供纯修饰与运行期色值)。固定顺序 前景 > 背景 > 修饰 不变。链步零参带括号(moonbit 语言规则),带参的只有 hex / bg_hex,收尾永远 text。

    // 前景
    @clicolor.red.text("失败")
    @clicolor.blue.underline().inverse().text("锁定")

    // 前景 > 背景
    @clicolor.red.bg_blue().text("失败")

    // 前景 > 背景 > 修饰
    @clicolor.red.bg_blue().bold().text("失败")
    @clicolor.green.bold().underline().text("成功")

    // 纯背景起步:没有前景,背景常量直接当链首
    @clicolor.bg_red.text("背景")

    // 两色皆无:default 起步(纯修饰、运行期色值)
    @clicolor.default.bold().text("加强")
    @clicolor.default.underline().italic().text("标注")

    // 预设
    @clicolor.error.text("文件缺失")
    @clicolor.warn.text("磁盘将满")
    @clicolor.ok.text("部署完成")
    @clicolor.info.text("提示")
    @clicolor.dim.text("2026-10-15 07:00")

    常量一览:前景色 red green yellow blue magenta cyan white 与对应 bright_*;背景色 bg_* 全套同上;修饰 bold italic underline inverse strikethrough;预设 error warn ok info dim;起步兜底 default。

    运行期色值与运行期背景:

    @clicolor.default.hex(c).text(t)
    @clicolor.red.bg_hex("#101010").text("hi")

    OSC 8 可点击超链接:

    @clicolor.hyperlink("https://mooncakes.io", "docs")

    #输出开关

    不做 TTY 与 NO_COLOR 自动探测,本库保持纯函数。上层调用方自己判定后显式关掉:

    @clicolor.set_enabled(false) // 返回旧值,方便恢复

    关闭后所有 text 原样返回文本。

    #迁移

    Ansi 枚举、wrap_ansi 数组入口、Color::paint、Style::new 均已除名,统一收敛到上面的唯一写法。

    #设计要点

    攒样式与产串分离。 链上每步只拷贝一个小记录(前景 4 bit、背景 4 bit、属性 5 bit 的位掩码),零字符串分配;text 阶段单个缓冲区一遍走完,发射顺序与书写顺序一致。

    空样式不添字节。 default 与关闭开关时 text 都原样返回,一字节转义都不输出。

    分工纪律。 转义序列与宽度是两件事:本包只管序列(生成与收尾),宽度、截断、断行由引用它的库自己处理,strip_ansi 一类的宽度剥离不进本包,避免互相依赖成环。

    边界(当前明确不做)。 嵌套样式(富文本树,内层关闭时重开外层)暂不实现,等 clitable 出现带色单元格截断的实需求再动;届时会新增独立的组合层,不复用 Style。256 色降级、TTY 自动探测同样在边界外。

    #demo

    src/main/demo.mbt 是全部写法的实景展示,十段对应用法各一,直接跑:

    moon run src/main

    #许可证

    MIT。

    clicolor.moonbitlang.dev 已上线,写法见 docs。

    Style

    pub struct Style {
    // private fields
    } derive(
    Debug
    )

    终端样式。不可变值,链式方法返回新值并与顺序无关,后续设的覆盖先设的。

    实现视图:fg / bg / attrs 三个 Int 位掩码(前景 4 bit、背景 4 bit、属性 5 bit),真彩色以字符串跟随;攒样式阶段零字符串分配,text 阶段一个 StringBuilder 一遍走完。

    模块视图:本包是终端转义序列的唯一来源。会算宽、会截断、会断行的库引用它, 自己保持「序列不管宽度、宽度不管序列」的分工。

    Style::bg_black

    fn Style::bg_black(self : Style) -> Style

    Style::bg_blue

    fn Style::bg_blue(self : Style) -> Style

    Style::bg_bright_black

    fn Style::bg_bright_black(self : Style) -> Style

    Style::bg_bright_blue

    fn Style::bg_bright_blue(self : Style) -> Style

    Style::bg_bright_cyan

    fn Style::bg_bright_cyan(self : Style) -> Style

    Style::bg_bright_green

    fn Style::bg_bright_green(self : Style) -> Style

    Style::bg_bright_magenta

    fn Style::bg_bright_magenta(self : Style) -> Style

    Style::bg_bright_red

    fn Style::bg_bright_red(self : Style) -> Style

    Style::bg_bright_white

    fn Style::bg_bright_white(self : Style) -> Style

    Style::bg_bright_yellow

    fn Style::bg_bright_yellow(self : Style) -> Style

    Style::bg_cyan

    fn Style::bg_cyan(self : Style) -> Style

    Style::bg_green

    fn Style::bg_green(self : Style) -> Style

    Style::bg_hex

    fn Style::bg_hex(self : Style, hex : String) -> Style

    真彩色背景。

    Style::bg_magenta

    fn Style::bg_magenta(self : Style) -> Style

    Style::bg_red

    fn Style::bg_red(self : Style) -> Style

    Style::bg_white

    fn Style::bg_white(self : Style) -> Style

    Style::bg_yellow

    fn Style::bg_yellow(self : Style) -> Style

    Style::black

    fn Style::black(self : Style) -> Style

    Style::blue

    fn Style::blue(self : Style) -> Style

    Style::bold

    fn Style::bold(self : Style) -> Style

    Style::bright_black

    fn Style::bright_black(self : Style) -> Style

    Style::bright_blue

    fn Style::bright_blue(self : Style) -> Style

    Style::bright_cyan

    fn Style::bright_cyan(self : Style) -> Style

    Style::bright_green

    fn Style::bright_green(self : Style) -> Style

    Style::bright_magenta

    fn Style::bright_magenta(self : Style) -> Style

    Style::bright_red

    fn Style::bright_red(self : Style) -> Style

    Style::bright_white

    fn Style::bright_white(self : Style) -> Style

    Style::bright_yellow

    fn Style::bright_yellow(self : Style) -> Style

    Style::cyan

    fn Style::cyan(self : Style) -> Style

    Style::green

    fn Style::green(self : Style) -> Style

    Style::hex

    fn Style::hex(self : Style, hex : String) -> Style

    真彩色前景,接受 #RGB 与 #RRGGBB,坏值渲染时按黑色降级。

    Style::inverse

    fn Style::inverse(self : Style) -> Style

    Style::italic

    fn Style::italic(self : Style) -> Style

    Style::magenta

    fn Style::magenta(self : Style) -> Style

    Style::red

    fn Style::red(self : Style) -> Style

    Style::strikethrough

    fn Style::strikethrough(self : Style) -> Style

    Style::text

    fn Style::text(self : Style, s : String) -> String

    把实例化好的 Style 装上文本、产出串。空 Style 原样返回,一字节转义都不添。

    Style::to_repr

    Style::underline

    fn Style::underline(self : Style) -> Style

    Style::white

    fn Style::white(self : Style) -> Style

    Style::yellow

    fn Style::yellow(self : Style) -> Style

    ESC

    let ESC : Char

    ESC 字符。转义序列一律以它开头。

    OSC8_PREFIX

    let OSC8_PREFIX : String

    OSC 8 超链接的起始序列前缀。截断或分包的库在切断内容后要用它判断 链接是否还开着,开着就补上收尾序列。

    OSC8_SUFFIX

    let OSC8_SUFFIX : String

    关掉当前 OSC 8 超链接的收尾序列。

    bg_black

    let bg_black : Style

    bg_blue

    let bg_blue : Style

    bg_bright_black

    let bg_bright_black : Style

    bg_bright_blue

    let bg_bright_blue : Style

    bg_bright_cyan

    let bg_bright_cyan : Style

    bg_bright_green

    let bg_bright_green : Style

    bg_bright_magenta

    let bg_bright_magenta : Style

    bg_bright_red

    let bg_bright_red : Style

    bg_bright_white

    let bg_bright_white : Style

    bg_bright_yellow

    let bg_bright_yellow : Style

    bg_cyan

    let bg_cyan : Style

    bg_green

    let bg_green : Style

    bg_magenta

    let bg_magenta : Style

    bg_red

    let bg_red : Style

    bg_white

    let bg_white : Style

    bg_yellow

    let bg_yellow : Style

    black

    let black : Style

    链首只从常量取值,方法仅供链身后继续叠加。

    blue

    let blue : Style

    bold

    let bold : Style

    bright_black

    let bright_black : Style

    bright_blue

    let bright_blue : Style

    bright_cyan

    let bright_cyan : Style

    bright_green

    let bright_green : Style

    bright_magenta

    let bright_magenta : Style

    bright_red

    let bright_red : Style

    bright_white

    let bright_white : Style

    bright_yellow

    let bright_yellow : Style

    cyan

    let cyan : Style

    default

    let default : Style

    dim

    let dim : Style

    error

    let error : Style

    green

    let green : Style

    fn hyperlink(url : String, text : String) -> String

    把文本包成 OSC 8 超链接。终端支持时文本可以点击,不支持时按普通文本显示。

    info

    let info : Style

    inverse

    let inverse : Style

    is_enabled

    fn is_enabled() -> Bool

    当前输出开关状态。

    italic

    let italic : Style

    magenta

    let magenta : Style

    let ok : Style

    red

    let red : Style

    set_enabled

    fn set_enabled(on : Bool) -> Bool

    显式输出开关。默认 true;设为 false 后所有 text 原样返回文本。 NO_COLOR / isatty 判断由上层调用方自己决定,本库保持纯函数。 返回旧值,方便改完再恢复。

    strikethrough

    let strikethrough : Style

    underline

    let underline : Style

    warn

    let warn : Style

    white

    let white : Style

    yellow

    let yellow : Style

    Source Files