#console #progress-bar #logging #place #static #bars #words

console_static_text

在控制台中保持相同位置的文本的日志记录

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文本处理

Download history 1670/week @ 2024-03-14 1980/week @ 2024-03-21 2408/week @ 2024-03-28 1863/week @ 2024-04-04 2210/week @ 2024-04-11 1896/week @ 2024-04-18 1689/week @ 2024-04-25 3016/week @ 2024-05-02 2227/week @ 2024-05-09 2329/week @ 2024-05-16 1788/week @ 2024-05-23 1559/week @ 2024-05-30 1516/week @ 2024-06-06 2204/week @ 2024-06-13 2309/week @ 2024-06-20 1475/week @ 2024-06-27

7,731 每月下载量
用于 19 软件包(直接使用 10 个)

MIT 许可证

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