-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new tui-bar-graph crate (#63)
  ```rust use tui_bar_graph::{BarGraph, BarStyle, ColorMode}; let data = vec![0.0, 0.1, 0.2, 0.3, 0.4, 0.5]; let bar_graph = BarGraph::new(data) .with_gradient(colorgrad::preset::turbo()) .with_bar_style(BarStyle::Braille) .with_color_mode(ColorMode::VerticalGradient); frame.render_widget(bar_graph, area); ```
- Loading branch information
Showing
8 changed files
with
502 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "tui-bar-graph" | ||
description = "A Ratatui widget for rendering pretty bar graphs in the terminal" | ||
version = "0.1.0" | ||
documentation = "https://docs.rs/tui-bar-graph" | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
categories.workspace = true | ||
keywords.workspace = true | ||
|
||
[dependencies] | ||
colorgrad = "0.7.0" | ||
ratatui.workspace = true | ||
strum.workspace = true | ||
|
||
[dev-dependencies] | ||
clap.workspace = true | ||
color-eyre.workspace = true | ||
crossterm.workspace = true | ||
rand.workspace = true | ||
ratatui = { workspace = true, default-features = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Tui-bar-graph | ||
|
||
<!-- cargo-rdme start --> | ||
|
||
A [Ratatui] widget for displaying pretty bar graphs | ||
|
||
Uses the [Colorgrad] crate for gradient coloring. | ||
|
||
 | ||
|
||
 | ||
|
||
[![Crate badge]][Crate] | ||
[![Docs Badge]][Docs] | ||
[![License Badge]](./LICENSE-MIT) | ||
[![Discord Badge]][Discord] | ||
|
||
## Installation | ||
|
||
```shell | ||
cargo add ratatui tui-bar-graph | ||
``` | ||
|
||
## Example | ||
|
||
```rust | ||
use tui_bar_graph::{BarGraph, BarStyle, ColorMode}; | ||
|
||
let data = vec![0.0, 0.1, 0.2, 0.3, 0.4, 0.5]; | ||
let bar_graph = BarGraph::new(data) | ||
.with_gradient(colorgrad::preset::turbo()) | ||
.with_bar_style(BarStyle::Braille) | ||
.with_color_mode(ColorMode::VerticalGradient); | ||
frame.render_widget(bar_graph, area); | ||
``` | ||
|
||
[Colorgrad]: https://crates.io/crates/colorgrad | ||
[Ratatui]: https://crates.io/crates/ratatui | ||
[Crate]: https://crates.io/crates/tui-bar-graph | ||
[Docs]: https://docs.rs/tui-bar-graph | ||
[Discord]: https://discord.gg/pMCEU9hNEj | ||
[Crate badge]: https://img.shields.io/crates/v/tui-bar-graph.svg?logo=rust&style=for-the-badge | ||
[Docs Badge]: https://img.shields.io/docsrs/tui-bar-graph?logo=rust&style=for-the-badge | ||
[License Badge]: https://img.shields.io/crates/l/tui-bar-graph.svg?style=for-the-badge | ||
[Discord Badge]: https://img.shields.io/discord/1070692720437383208?label=ratatui+discord&logo=discord&style=for-the-badge | ||
|
||
<!-- cargo-rdme end --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# VHS Tape (see https://github.com/charmbracelet/vhs) | ||
Output "target/tui-bar-graph-braille.gif" | ||
Set Theme "Aardvark Blue" | ||
Set Width 1200 | ||
Set Height 800 | ||
Hide | ||
Type@0 "cargo run --quiet -p tui-bar-graph --example tui-bar-graph braille" | ||
Enter | ||
Sleep 2s | ||
Show | ||
Screenshot "target/tui-bar-graph-braille.png" | ||
Sleep 1s | ||
Hide | ||
Escape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# VHS Tape (see https://github.com/charmbracelet/vhs) | ||
Output "target/tui-bar-graph-solid.gif" | ||
Set Theme "Aardvark Blue" | ||
Set Width 1200 | ||
Set Height 800 | ||
Hide | ||
Type@0 "cargo run --quiet -p tui-bar-graph --example tui-bar-graph solid" | ||
Enter | ||
Sleep 2s | ||
Show | ||
Screenshot "target/tui-bar-graph-solid.png" | ||
Sleep 1s | ||
Hide | ||
Escape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use clap::Parser; | ||
use crossterm::event::{self, Event, KeyEvent, KeyEventKind}; | ||
use rand::Rng; | ||
use ratatui::{DefaultTerminal, Frame}; | ||
use tui_bar_graph::{BarGraph, BarStyle, ColorMode}; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Args { | ||
/// The style of bar to render (solid or braille) | ||
#[arg(default_value_t = BarStyle::Braille)] | ||
bar_style: BarStyle, | ||
} | ||
|
||
fn main() -> color_eyre::Result<()> { | ||
let args = Args::parse(); | ||
color_eyre::install()?; | ||
let terminal = ratatui::init(); | ||
let result = run(terminal, &args); | ||
ratatui::restore(); | ||
result | ||
} | ||
|
||
fn run(mut terminal: DefaultTerminal, args: &Args) -> color_eyre::Result<()> { | ||
loop { | ||
terminal.draw(|frame| render(frame, args))?; | ||
if matches!( | ||
event::read()?, | ||
Event::Key(KeyEvent { | ||
kind: KeyEventKind::Press, | ||
.. | ||
}) | ||
) { | ||
break Ok(()); | ||
} | ||
} | ||
} | ||
|
||
fn render(frame: &mut Frame, args: &Args) { | ||
let width = match args.bar_style { | ||
BarStyle::Solid => frame.area().width as usize, | ||
BarStyle::Braille => frame.area().width as usize * 2, | ||
}; | ||
let mut data = vec![0.0; width]; | ||
rand::rng().fill(&mut data[..]); | ||
|
||
let bar_graph = BarGraph::new(data) | ||
.with_gradient(colorgrad::preset::turbo()) | ||
.with_color_mode(ColorMode::VerticalGradient) | ||
.with_bar_style(args.bar_style); | ||
frame.render_widget(bar_graph, frame.area()); | ||
} |
Oops, something went wrong.