Skip to content

Commit c24f045

Browse files
authored
feat: add new tui-bar-graph crate (#63)
![Braille demo](https://vhs.charm.sh/vhs-3H7bFj0M1kj0GoHcc4EIJ4.gif) ![Solid demo](https://vhs.charm.sh/vhs-5XMtSFgX3vqOhKcKl8fEQK.gif) ```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); ```
1 parent 7c4bf99 commit c24f045

File tree

8 files changed

+502
-0
lines changed

8 files changed

+502
-0
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ itertools = "0.14.0"
2525
indoc = "2.0.5"
2626
lipsum = "0.9.1"
2727
pretty_assertions = "1.4.1"
28+
rand = "0.9.0"
2829
ratatui = { version = "0.29.0", default-features = false }
2930
ratatui-macros = "0.6.0"
3031
rstest = "0.24.0"
@@ -58,6 +59,7 @@ rust-version.workspace = true
5859
#! # features
5960
## By default, all the widgets are enabled.
6061
default = [
62+
"bar-graph",
6163
"big-text",
6264
"box-text",
6365
"cards",
@@ -66,6 +68,8 @@ default = [
6668
"qrcode",
6769
"scrollview",
6870
]
71+
## Enables the [`bar_graph`] widget
72+
bar-graph = ["tui-bar-graph"]
6973
## Enables the [`big_text`] widget
7074
big-text = ["tui-big-text"]
7175
## Enables the [`box_text`] widget
@@ -84,6 +88,7 @@ scrollview = ["tui-scrollview"]
8488
[dependencies]
8589
document-features.workspace = true
8690
ratatui = { workspace = true }
91+
tui-bar-graph = { version = "0.1.0", path = "tui-bar-graph", optional = true }
8792
tui-big-text = { version = "0.7.0", path = "tui-big-text", optional = true }
8893
tui-box-text = { version = "0.2.0", path = "tui-box-text", optional = true }
8994
tui-cards = { version = "0.2.0", path = "tui-cards", optional = true }

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//!
88
//! Includes the following widgets, which are each also available as standalone crates:
99
//!
10+
//! - [tui-bar-graph](https://crates.io/crates/tui-bar-graph)
1011
//! - [tui-big-text](https://crates.io/crates/tui-big-text)
1112
//! - [tui-box-text](https://crates.io/crates/tui-box-text)
1213
//! - [tui-cards](https://crates.io/crates/tui-cards)
@@ -16,6 +17,9 @@
1617
//! - [tui-scrollview](https://crates.io/crates/tui-scrollview)
1718
#![doc = document_features::document_features!()]
1819

20+
#[cfg(feature = "bar-graph")]
21+
#[doc(inline)]
22+
pub use tui_bar_graph as bar_graph;
1923
#[cfg(feature = "big-text")]
2024
#[doc(inline)]
2125
pub use tui_big_text as big_text;

tui-bar-graph/Cargo.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "tui-bar-graph"
3+
description = "A Ratatui widget for rendering pretty bar graphs in the terminal"
4+
version = "0.1.0"
5+
documentation = "https://docs.rs/tui-bar-graph"
6+
authors.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
categories.workspace = true
12+
keywords.workspace = true
13+
14+
[dependencies]
15+
colorgrad = "0.7.0"
16+
ratatui.workspace = true
17+
strum.workspace = true
18+
19+
[dev-dependencies]
20+
clap.workspace = true
21+
color-eyre.workspace = true
22+
crossterm.workspace = true
23+
rand.workspace = true
24+
ratatui = { workspace = true, default-features = true }

tui-bar-graph/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Tui-bar-graph
2+
3+
<!-- cargo-rdme start -->
4+
5+
A [Ratatui] widget for displaying pretty bar graphs
6+
7+
Uses the [Colorgrad] crate for gradient coloring.
8+
9+
![Braille demo](https://vhs.charm.sh/vhs-3H7bFj0M1kj0GoHcc4EIJ4.gif)
10+
11+
![Solid demo](https://vhs.charm.sh/vhs-5XMtSFgX3vqOhKcKl8fEQK.gif)
12+
13+
[![Crate badge]][Crate]
14+
[![Docs Badge]][Docs]
15+
[![License Badge]](./LICENSE-MIT)
16+
[![Discord Badge]][Discord]
17+
18+
## Installation
19+
20+
```shell
21+
cargo add ratatui tui-bar-graph
22+
```
23+
24+
## Example
25+
26+
```rust
27+
use tui_bar_graph::{BarGraph, BarStyle, ColorMode};
28+
29+
let data = vec![0.0, 0.1, 0.2, 0.3, 0.4, 0.5];
30+
let bar_graph = BarGraph::new(data)
31+
.with_gradient(colorgrad::preset::turbo())
32+
.with_bar_style(BarStyle::Braille)
33+
.with_color_mode(ColorMode::VerticalGradient);
34+
frame.render_widget(bar_graph, area);
35+
```
36+
37+
[Colorgrad]: https://crates.io/crates/colorgrad
38+
[Ratatui]: https://crates.io/crates/ratatui
39+
[Crate]: https://crates.io/crates/tui-bar-graph
40+
[Docs]: https://docs.rs/tui-bar-graph
41+
[Discord]: https://discord.gg/pMCEU9hNEj
42+
[Crate badge]: https://img.shields.io/crates/v/tui-bar-graph.svg?logo=rust&style=for-the-badge
43+
[Docs Badge]: https://img.shields.io/docsrs/tui-bar-graph?logo=rust&style=for-the-badge
44+
[License Badge]: https://img.shields.io/crates/l/tui-bar-graph.svg?style=for-the-badge
45+
[Discord Badge]: https://img.shields.io/discord/1070692720437383208?label=ratatui+discord&logo=discord&style=for-the-badge
46+
47+
<!-- cargo-rdme end -->

tui-bar-graph/examples/braille.tape

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# VHS Tape (see https://github.com/charmbracelet/vhs)
2+
Output "target/tui-bar-graph-braille.gif"
3+
Set Theme "Aardvark Blue"
4+
Set Width 1200
5+
Set Height 800
6+
Hide
7+
Type@0 "cargo run --quiet -p tui-bar-graph --example tui-bar-graph braille"
8+
Enter
9+
Sleep 2s
10+
Show
11+
Screenshot "target/tui-bar-graph-braille.png"
12+
Sleep 1s
13+
Hide
14+
Escape

tui-bar-graph/examples/solid.tape

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# VHS Tape (see https://github.com/charmbracelet/vhs)
2+
Output "target/tui-bar-graph-solid.gif"
3+
Set Theme "Aardvark Blue"
4+
Set Width 1200
5+
Set Height 800
6+
Hide
7+
Type@0 "cargo run --quiet -p tui-bar-graph --example tui-bar-graph solid"
8+
Enter
9+
Sleep 2s
10+
Show
11+
Screenshot "target/tui-bar-graph-solid.png"
12+
Sleep 1s
13+
Hide
14+
Escape
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use clap::Parser;
2+
use crossterm::event::{self, Event, KeyEvent, KeyEventKind};
3+
use rand::Rng;
4+
use ratatui::{DefaultTerminal, Frame};
5+
use tui_bar_graph::{BarGraph, BarStyle, ColorMode};
6+
7+
#[derive(Debug, Parser)]
8+
struct Args {
9+
/// The style of bar to render (solid or braille)
10+
#[arg(default_value_t = BarStyle::Braille)]
11+
bar_style: BarStyle,
12+
}
13+
14+
fn main() -> color_eyre::Result<()> {
15+
let args = Args::parse();
16+
color_eyre::install()?;
17+
let terminal = ratatui::init();
18+
let result = run(terminal, &args);
19+
ratatui::restore();
20+
result
21+
}
22+
23+
fn run(mut terminal: DefaultTerminal, args: &Args) -> color_eyre::Result<()> {
24+
loop {
25+
terminal.draw(|frame| render(frame, args))?;
26+
if matches!(
27+
event::read()?,
28+
Event::Key(KeyEvent {
29+
kind: KeyEventKind::Press,
30+
..
31+
})
32+
) {
33+
break Ok(());
34+
}
35+
}
36+
}
37+
38+
fn render(frame: &mut Frame, args: &Args) {
39+
let width = match args.bar_style {
40+
BarStyle::Solid => frame.area().width as usize,
41+
BarStyle::Braille => frame.area().width as usize * 2,
42+
};
43+
let mut data = vec![0.0; width];
44+
rand::rng().fill(&mut data[..]);
45+
46+
let bar_graph = BarGraph::new(data)
47+
.with_gradient(colorgrad::preset::turbo())
48+
.with_color_mode(ColorMode::VerticalGradient)
49+
.with_bar_style(args.bar_style);
50+
frame.render_widget(bar_graph, frame.area());
51+
}

0 commit comments

Comments
 (0)