4个稳定版本
| 1.1.2 | 2023年7月22日 | 
|---|---|
| 1.0.0 | 2023年7月13日 | 
#274 in 命令行界面
3,717 每月下载量
在 2 个crate中使用(通过 tango-bench)
125KB
 3K  SLoC
colorz
一种零分配、与no_std兼容的终端输出着色方法!
这是owo-colors的一个“分支”,具有更多功能。
use colorz::{Colorize, xterm};
// will print `hello world` in red
println!("{}", "hello world".red());
// will print `hello world` on a red background
println!("{}", "hello world".on_red());
// will print `hello world` on a with an Aqua underline
println!("{}", "hello world".underline_color(xterm::Aqua).underline());
特性
- 使用任何 std格式化特性(Display、Debug等)进行格式化
- 使用自定义格式化函数(StyledValue::fmt_with)进行格式化
- 通过 StyledValue::stream进行按值条件样式化
- 通过- colorz::模式
- strip-colors功能标志
 
- 默认无依赖
- 标准命名方式用于Ansi、Xterm和Css颜色
- 支持RGB颜色
- 支持Ansi修饰符(粗体、斜体、下划线)
- 支持多颜色(前景色、背景色和下划线颜色)
- 在简单情况下,主要是owo-colors的替代品- (某些xterm颜色名称可能不同,某些方法名称略有不同)
 
- 通过颜色代码编译时选择xterm颜色
- 编译时构建样式
- 编译时构建样式值
- NO_COLOR/- ALWAYS_COLOR环境变量:- colorz::mode::{Mode::from_env, set_coloring_mode_from_env}- 需要 std或supports-color功能
 
- 需要 
功能标志
此crate有几个功能标志
- strip-colors- 删除- StyledValue格式化方法的所有着色
- std- 这启用了标准库(因为该库默认为- no_std)
- supports-color- 这启用了- supports-color包(它也使用了- std库)
默认情况下,没有任何功能被启用。它们应该仅由最终的二进制包启用。
如果关闭这些功能,则仅尊重全局模式设置,不会进行基于流的颜色检测。
如果启用 strip-colors,则 colorz::mode::get_coloring_mode 将始终返回 Mode::Never,并且 StyledValue 不会着色。
否则如果启用 supports-color,则使用 supports-color 包检测是否支持 ANSI、Xterm 或 RGB 颜色。如果 StyledValue 尝试使用任何不受支持的彩色类型,则不会进行着色。例如,如果你的终端不支持 Xterm 颜色,并且你编写
use colorz::{Colorize, xterm};
println!("{}", "hello world".fg(xterm::Red));
那么你将看到默认终端颜色下的 hello world。
最后,如果启用 std,则如果流是终端,则使用所有彩色类型。如果不是终端,则不选择任何彩色。
彩色模式
有几种方式可以指定 colorz 的彩色模式,它们之间的交互可能并不明显,因此这里有一个优先级列表。要了解 colorz 如何选择着色,请按照列表向下,第一个适用的元素将被选择。
- 如果启用功能标志 strip-colors-> 不着色
- 如果全局彩色模式是 Mode::Always-> 着色
- 如果全局彩色模式是 Mode::NEVER-> 不着色
- 如果每个值的流设置为- Stream::AlwaysColor-> 着色
- Stream::NeverColor-> 不着色
- Stream::Stdout/- Stream::Stderr-> 使用- std或- support-color检测着色(有关功能标志的详细信息,请参阅文档)
 
- 如果全局流设置为- Stream::AlwaysColor-> 着色
- Stream::NeverColor-> 不着色
- Stream::Stdout/- Stream::Stderr-> 使用- std或- support-color检测着色(有关功能标志的详细信息,请参阅文档)
 
全局流始终设置为可能的 Stream 值之一,因此列表中的一个选项始终会被选择。
注意:从环境设置彩色模式会设置全局彩色模式,因此列表中的第二个或第三个选项。
注意:彩色模式仅影响 StyledValue(它包括所有 Colorize 特性的输出)。直接使用 Style::apply/Style::clear 不会尊重彩色模式,并且可以用于强制着色,而不管当前的彩色模式如何。你可以使用 Style::should_color 来检测基于当前彩色模式是否应该使用给定的样式。
use colorz::{Style, xterm, mode::Stream};
let style = Style::new().fg(xterm::Aquamarine);
if style.should_color(Stream::AlwaysColor) {
    println!("{}style if global is set{}", style.apply(), style.clear());
}
if style.should_color(None) {
    println!("{}style if global is set or default stream is set{}", style.apply(), style.clear());
}
示例
格式化任何值
use colorz::{Colorize, xterm, css};
#[derive(Debug)]
struct MyType {
    value: String,
}
// will print `hello world` in red
println!("{}", "hello world".red());
// will print `100` on an aquamarine background
println!("{}", 100.bg(css::Aquamarine));
// will print `hello world` on a with an Aqua underline
println!("{:?}", MyType { value: "hello world".into() }.underline_color(xterm::Aqua).underline());
按值条件格式化
use colorz::{Colorize, xterm, mode::Stream::*};
// will print `hello world` in red if Stdout points to a terminal
println!("{}", "hello world".red().stream(Stdout));
轻松在任何时候关闭
use colorz::{Colorize, xterm, mode::Stream::*};
colorz::mode::set_coloring_mode(colorz::mode::Mode::Never);
// doesn't style the value
println!("{}", "hello world".red());
assert_eq!(format!("{}", "hello world".red()), "hello world");
创建编译时样式表
use colorz::{Colorize, Style, Effect, xterm};
const MY_STYLE: Style = Style::new()
    .fg(xterm::ForestGreen)
    .effects_array([Effect::Italic, Effect::Bold])
    .const_into_runtime_style();
// styles `my text` in forest green with italics and bold
println!("{}", "my text".style_with(MY_STYLE));
依赖关系
~0-8MB
~43K SLoC