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

console_static_text

Logging for text that should stay in the same place in a console

15 unstable releases

0.8.2 Dec 25, 2023
0.8.1 Mar 27, 2023
0.7.1 Mar 9, 2023
0.5.0 Dec 17, 2022

#123 in Text processing

Download history 1323/week @ 2024-08-19 1763/week @ 2024-08-26 1753/week @ 2024-09-02 1854/week @ 2024-09-09 1637/week @ 2024-09-16 1734/week @ 2024-09-23 1679/week @ 2024-09-30 2466/week @ 2024-10-07 2278/week @ 2024-10-14 1703/week @ 2024-10-21 1831/week @ 2024-10-28 1744/week @ 2024-11-04 1235/week @ 2024-11-11 1419/week @ 2024-11-18 1653/week @ 2024-11-25 1678/week @ 2024-12-02

6,327 downloads per month
Used in 26 crates (14 directly)

MIT license

33KB
910 lines

console_static_text

Crate for logging text that should stay in the same place in a console. This measures words to handle wrapping and has some console resizing support. Example use might be for displaying progress bars or rendering selections.

Example use with the console crate:

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();

Hanging indentation

To get hanging indentation, you can use the lower level "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());

This is useful when implementing something like a selection UI where you want text to wrap with hanging indentation.

"sized" feature

By default, this crate encourages you to use your own functionality for getting the console size since you'll likely already have a dependency that does that, but if not, then you can use the sized Cargo.toml feature.

[dependencies]
console_static_text = { version = "...", features = ["sized"] }

Then you can use the new_sized function, which will get the console size automatically:

let mut static_text = ConsoleStaticText::new_sized();

static_text.eprint("initial\ntext");
std::thread::sleep_ms(1000);

static_text.eprint("next text");

Dependencies

~1–11MB
~124K SLoC