15 个不稳定版本
0.8.2 | 2023 年 12 月 25 日 |
---|---|
0.8.1 | 2023 年 3 月 27 日 |
0.7.1 | 2023 年 3 月 9 日 |
0.5.0 | 2022 年 12 月 17 日 |
#291 在 文本处理
7,731 每月下载量
用于 19 个 软件包(直接使用 10 个)
33KB
910 行
console_static_text
用于在控制台中保持文本位置的日志记录的软件包。它测量单词以处理换行,并具有一些控制台缩放支持。示例用途可能是显示进度条或渲染选择。
与 console 软件包的示例用法
use console_static_text::ConsoleSize;
use console_static_text::ConsoleStaticText;
let mut static_text = ConsoleStaticText::new(|| {
let size = console::Term::stderr().size();
ConsoleSize {
rows: Some(size.0),
cols: Some(size.1),
}
});
static_text.eprint("initial\ntext");
std::thread::sleep_ms(1000);
// will clear the previous text and put this new text
static_text.eprint("new text");
std::thread::sleep_ms(1000);
// or get and output the text manually
if let Some(text) = static_text.render("new text") {
eprint!("{}", text);
std::thread::sleep_ms(1000);
}
// clear out the previous text
static_text.eprint_clear();
悬垂缩进
要获取悬垂缩进,您可以使用较低级别的 "items" API。
static_text.eprint_items(vec![
TextItem::Text(Cow::Borrowed("Some non-hanging text.")),
TextItem::HangingText {
text: Cow::Borrowed("some long text that will wrap at a certain width"),
indent: 4,
},
].iter());
当实现选择用户界面等时,这很有用,其中您希望文本以悬垂缩进的方式换行。
"sized" 功能
默认情况下,此软件包鼓励您使用自己的功能来获取控制台大小,因为您可能已经有了一个做这个的依赖项,但没有的话,您可以使用 sized
Cargo.toml 功能。
[dependencies]
console_static_text = { version = "...", features = ["sized"] }
然后您可以使用 new_sized
函数,该函数将自动获取控制台大小
let mut static_text = ConsoleStaticText::new_sized();
static_text.eprint("initial\ntext");
std::thread::sleep_ms(1000);
static_text.eprint("next text");
依赖项
~0.5–11MB
~109K SLoC