https://github.com/sourcefrog/nutmeg
A flexible model/view Rust progress bar library
https://github.com/sourcefrog/nutmeg
progress-bar rust terminal
Last synced: 6 months ago
JSON representation
A flexible model/view Rust progress bar library
- Host: GitHub
- URL: https://github.com/sourcefrog/nutmeg
- Owner: sourcefrog
- License: mit
- Created: 2022-01-25T03:49:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-15T02:08:35.000Z (about 2 years ago)
- Last Synced: 2024-03-15T02:47:23.198Z (almost 2 years ago)
- Topics: progress-bar, rust, terminal
- Language: Rust
- Homepage:
- Size: 278 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nutmeg - an unopinionated progress bar library
[](https://github.com/sourcefrog/nutmeg/actions/workflows/tests.yml?query=branch%3Amain)
[](https://docs.rs/nutmeg)
[](https://crates.io/crates/nutmeg)
[](https://lib.rs/crates/nutmeg)

Nutmeg draws terminal progress indicators while giving the application complete
control over their appearance and content.
For more information:
License: MIT
## Example
From `examples/basic.rs`:
```rust
use std::io::Write; // to support write!()
// 1. Define a struct holding all the application state necessary to
// render the progress bar.
#[derive(Default)]
struct Model {
i: usize,
total: usize,
last_file_name: String,
}
// 2. Define how to render the progress bar as a String.
impl nutmeg::Model for Model {
fn render(&mut self, _width: usize) -> String {
format!("{}/{}: {}", self.i, self.total, self.last_file_name)
}
}
fn main() -> std::io::Result<()> {
// 3. Create a View when you want to draw a progress bar.
let mut view = nutmeg::View::new(Model::default(),
nutmeg::Options::default());
// 4. As the application runs, update the model via the view.
let total_work = 100;
view.update(|model| model.total = total_work);
for i in 0..total_work {
view.update(|model| {
model.i += 1;
model.last_file_name = format!("file{}.txt", i);
});
// 5. Interleave text output lines by writing to the view.
if i % 10 == 3 {
writeln!(view, "reached {}", i)?;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
// 5. The bar is automatically erased when dropped.
Ok(())
}
```
[](https://asciinema.org/a/oPI37ohOY8yhDxomTzHCsR4sw)