Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dsherret/console_static_text
Logging for text that should stay in the same place in a console.
https://github.com/dsherret/console_static_text
Last synced: about 2 months ago
JSON representation
Logging for text that should stay in the same place in a console.
- Host: GitHub
- URL: https://github.com/dsherret/console_static_text
- Owner: dsherret
- License: mit
- Created: 2022-12-11T03:38:25.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-25T21:00:51.000Z (12 months ago)
- Last Synced: 2024-10-30T08:51:39.125Z (about 2 months ago)
- Language: Rust
- Size: 35.2 KB
- Stars: 13
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# console_static_text
[![](https://img.shields.io/crates/v/console_static_text.svg)](https://crates.io/crates/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](https://crates.io/crates/console) crate:
```rs
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.
```rs
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.
```toml
[dependencies]
console_static_text = { version = "...", features = ["sized"] }
```Then you can use the `new_sized` function, which will get the console size automatically:
```rs
let mut static_text = ConsoleStaticText::new_sized();static_text.eprint("initial\ntext");
std::thread::sleep_ms(1000);static_text.eprint("next text");
```