1 个不稳定版本
使用旧 Rust 2015
0.0.1 | 2014年11月24日 |
---|
#32 in #color-string
570KB
858 代码行数,不包括注释
包含 (WOFF 字体, 120KB) doc/Heuristica-Italic.woff, (WOFF 字体, 90KB) doc/FiraSans-Medium.woff, (WOFF 字体, 92KB) doc/FiraSans-Regular.woff, (WOFF 字体, 56KB) doc/SourceCodePro-Regular.woff, (WOFF 字体, 56KB) doc/SourceCodePro-Semibold.woff, (WOFF 字体, 49KB) doc/SourceSerifPro-Bold.woff 以及更多.
颜色
颜色是一个帮助您对文本进行着色和样式的库。它是从 NodeJS/NPM 的颜色库移植过来的,主要是作为一个学习练习 :)
示例
use colors::Style; // Import this to add colors to your string types
println!("{}", "I'm now magically printed in green!".green());
如果您不希望将特性添加到您的 &str 和字符串中,则只需直接调用样式,如下所示
use colors::{Styles,style};
println!("{}", style(Styles::Blue, "I'm now blue!"))
虽然前一种样式提供了便利的链式语法
use colors::Style;
println!("{}", "Bolded, underlined, and red!".bold().underline().red());
问题
-
您可能在示例中注意到了一个问题,我们失去了 Rusts "println!" 宏带来的原生字符串替换功能。这非常不幸,看起来这是不可避免的。根据 文档,编译器要求传递的值必须是一个字符串字面量,以便执行有效性检查。如果您知道任何解决方案,请提交一个 pull 请求。
-
整个技术也非常不 Rust。动态分配大量字符串以将它们包装在花哨的 Unicode 字符中不会很高效。作为替代方案,您可以使用 Rust 标准库中的巧妙 TerminfoTerminal。
移植中缺少的/待办事项
- 额外的(愚蠢的)样式
- 颜色主题
- 字符串替换,如上所述的原因