Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new tui-bar-graph crate #63

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ itertools = "0.14.0"
indoc = "2.0.5"
lipsum = "0.9.1"
pretty_assertions = "1.4.1"
rand = "0.9.0"
ratatui = { version = "0.29.0", default-features = false }
ratatui-macros = "0.6.0"
rstest = "0.24.0"
Expand Down Expand Up @@ -58,6 +59,7 @@ rust-version.workspace = true
#! # features
## By default, all the widgets are enabled.
default = [
"bar-graph",
"big-text",
"box-text",
"cards",
Expand All @@ -66,6 +68,8 @@ default = [
"qrcode",
"scrollview",
]
## Enables the [`bar_graph`] widget
bar-graph = ["tui-bar-graph"]
## Enables the [`big_text`] widget
big-text = ["tui-big-text"]
## Enables the [`box_text`] widget
Expand All @@ -84,6 +88,7 @@ scrollview = ["tui-scrollview"]
[dependencies]
document-features.workspace = true
ratatui = { workspace = true }
tui-bar-graph = { version = "0.1.0", path = "tui-bar-graph", optional = true }
tui-big-text = { version = "0.7.0", path = "tui-big-text", optional = true }
tui-box-text = { version = "0.2.0", path = "tui-box-text", optional = true }
tui-cards = { version = "0.2.0", path = "tui-cards", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//!
//! Includes the following widgets, which are each also available as standalone crates:
//!
//! - [tui-bar-graph](https://crates.io/crates/tui-bar-graph)
//! - [tui-big-text](https://crates.io/crates/tui-big-text)
//! - [tui-box-text](https://crates.io/crates/tui-box-text)
//! - [tui-cards](https://crates.io/crates/tui-cards)
Expand All @@ -16,6 +17,9 @@
//! - [tui-scrollview](https://crates.io/crates/tui-scrollview)
#![doc = document_features::document_features!()]

#[cfg(feature = "bar-graph")]
#[doc(inline)]
pub use tui_bar_graph as bar_graph;
#[cfg(feature = "big-text")]
#[doc(inline)]
pub use tui_big_text as big_text;
Expand Down
24 changes: 24 additions & 0 deletions tui-bar-graph/Cargo.toml
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 }
47 changes: 47 additions & 0 deletions tui-bar-graph/README.md
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.

![Braille demo](https://vhs.charm.sh/vhs-3H7bFj0M1kj0GoHcc4EIJ4.gif)

![Solid demo](https://vhs.charm.sh/vhs-5XMtSFgX3vqOhKcKl8fEQK.gif)

[![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 -->
14 changes: 14 additions & 0 deletions tui-bar-graph/examples/braille.tape
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
14 changes: 14 additions & 0 deletions tui-bar-graph/examples/solid.tape
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
51 changes: 51 additions & 0 deletions tui-bar-graph/examples/tui-bar-graph.rs
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());
}
Loading