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 tui-qrcode crate #56

Merged
merged 4 commits into from
Feb 9, 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
20 changes: 17 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,37 @@ rust-version.workspace = true
[features]
#! # features
## By default, all the widgets are enabled.
default = ["big-text", "popup", "prompts", "scrollview"]
default = [
"big-text",
"box-text",
"cards",
"popup",
"prompts",
"qrcode",
"scrollview",
]
## Enables the [`big_text`] widget
big-text = ["tui-big-text"]
## Enables the [`box_text`] widget
box-text = ["tui-box-text"]
## Enables the [`cards`] widget
cards = ["tui-cards"]
## Enables the [`popup`] widget
popup = ["tui-popup"]
## Enables the [`prompts`] widget
prompts = ["tui-prompts"]
## Enables the [`qrcode`] widget
qrcode = ["tui-qrcode"]
## Enables the [`scrollview`] widget
scrollview = ["tui-scrollview"]
## Enables the [`cards`] widget
cards = ["tui-cards"]

[dependencies]
document-features.workspace = true
ratatui = { workspace = 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 }
tui-popup = { version = "0.6.0", path = "tui-popup", optional = true }
tui-prompts = { version = "0.5.0", path = "tui-prompts", optional = true }
tui-qrcode = { version = "0.1.0", path = "tui-qrcode", optional = true }
tui-scrollview = { version = "0.5.1", path = "tui-scrollview", optional = true }
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@
//! This is a crate that combines multiple previously standalone crates into one in order simplify
//! maintenance and to make it easier to use the widgets together.
//!
//! Includes the following widgets, which are also available as standalone crates:
//! Includes the following widgets, which are each also available as standalone crates:
//!
//! - [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)
//! - [tui-popup](https://crates.io/crates/tui-popup)
//! - [tui-prompts](https://crates.io/crates/tui-prompts)
//! - [tui-qrcode](https://crates.io/crates/tui-qrcode)
//! - [tui-scrollview](https://crates.io/crates/tui-scrollview)
#![doc = document_features::document_features!()]

#[cfg(feature = "big-text")]
#[doc(inline)]
pub use tui_big_text as big_text;

#[cfg(feature = "box-text")]
#[doc(inline)]
pub use tui_box_text as box_text;

#[cfg(feature = "cards")]
#[doc(inline)]
pub use tui_cards as cards;

#[cfg(feature = "popup")]
#[doc(inline)]
pub use tui_popup as popup;

#[cfg(feature = "prompts")]
#[doc(inline)]
pub use tui_prompts as prompts;

#[cfg(feature = "qrcode")]
#[doc(inline)]
pub use tui_qrcode as qrcode;

#[cfg(feature = "scrollview")]
#[doc(inline)]
pub use tui_scrollview as scrollview;
21 changes: 21 additions & 0 deletions tui-qrcode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "tui-qrcode"
version = "0.1.0"
description = "A Ratatui widget for displaying QR codes in the terminal"
documentation = "https://docs.rs/tui-qrcode"

authors.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true
rust-version.workspace = true
categories.workspace = true
keywords.workspace = true

[dependencies]
color-eyre.workspace = true
qrcode = { version = "0.14.1", default-features = false }
ratatui.workspace = true

[dev-dependencies]
ratatui = { workspace = true, features = ["crossterm"] }
97 changes: 97 additions & 0 deletions tui-qrcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# TUI QR Code

<!-- cargo-rdme start -->

TUI QR Code is a library for rendering QR codes in a terminal using the [Ratatui] crate.

[![Crate badge]][tui-qrcode]
[![Docs.rs Badge]][API Docs]
[![Deps.rs Badge]][Dependency Status]
[![License Badge]](./LICENSE-MIT)
[![Discord Badge]][Ratatui Discord]

[GitHub Repository] · [API Docs] · [Examples] · [Changelog] · [Contributing]

![Demo](https://vhs.charm.sh/vhs-nUpcmCP1igCcGoJ5iio07.gif)

## Usage

Add qrcode and tui-qrcode to your Cargo.toml. You can disable the default features of qrcode as
we don't need the code which renders the QR code to an image.

```shell
cargo add qrcode tui-qrcode --no-default-features
```

## Example

This example can be found in the `examples` directory of the repository.

```rust
use qrcode::QrCode;
use ratatui::{crossterm::event, DefaultTerminal, Frame};
use tui_qrcode::{Colors, QrCodeWidget};

fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let result = run(terminal);
ratatui::restore();
result
}

fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
loop {
terminal.draw(render)?;
if matches!(event::read()?, event::Event::Key(_)) {
break Ok(());
}
}
}

fn render(frame: &mut Frame) {
let qr_code = QrCode::new("https://ratatui.rs").expect("failed to create QR code");
let widget = QrCodeWidget::new(qr_code).colors(Colors::Inverted);
frame.render_widget(widget, frame.area());
}
```

Renders the following QR code:

```text
█████████████████████████████████
█████████████████████████████████
████ ▄▄▄▄▄ █▄ ▄▄▄ ████ ▄▄▄▄▄ ████
████ █ █ █▄▄▄█▀▄██ █ █ █ ████
████ █▄▄▄█ █▀ ▄▀ ███ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀▄█ ▀▄▀ █▄▄▄▄▄▄▄████
████ █▄▀▀▀▄▄▀▄▄ ▄█▀▄█▀ █▀▄▀ ████
██████▀█ ▄▀▄▄▀▀ ▄ ▄█ ▄▄█ ▄█▄████
████▄▀▀▀▄▄▄▄▀█▄▄█ ▀ ▀ ▀███▀ ████
████▄▄ ▀█▄▄▀▄▄ ▄█▀█▄▀█▄▀▀ ▄█▄████
████▄▄█▄██▄█ ▄▀▄ ▄█ ▄▄▄ ██▄▀████
████ ▄▄▄▄▄ █▄▄▄▀ ▄ ▀ █▄█ ███ ████
████ █ █ ██ ███ ▄▄ ▄▄ █▀ ▄████
████ █▄▄▄█ █▄▀ ▄█▀█▀ ▄█ ▄█▄▄████
████▄▄▄▄▄▄▄█▄▄█▄▄▄██▄█▄██▄██▄████
█████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
```

[Ratatui]: https://crates.io/crates/ratatui
[Crate badge]: https://img.shields.io/crates/v/tui-qrcode.svg?style=for-the-badge
[tui-qrcode]: https://crates.io/crates/tui-qrcode
[Docs.rs Badge]: https://img.shields.io/badge/docs.rs-tui--qrcode-blue?style=for-the-badge
[API Docs]: https://docs.rs/tui-qrcode
[Deps.rs Badge]: https://deps.rs/repo/github/joshka/tui-qrcode/status.svg?style=for-the-badge
[Dependency Status]: https://deps.rs/repo/github/joshka/tui-qrcode
[License Badge]: https://img.shields.io/crates/l/tui-qrcode?style=for-the-badge
[Discord Badge]:
https://img.shields.io/discord/1070692720437383208?label=ratatui+discord&logo=discord&style=for-the-badge
[Ratatui Discord]: https://discord.gg/pMCEU9hNEj
[GitHub Repository]: https://github.com/joshka/tui-widgets
[Examples]: https://github.com/joshka/tui-widgets/tree/main/tui-qrcode/examples
[Changelog]: https://github.com/joshka/tui-widgets/blob/main/tui-qrcode/CHANGELOG.md
[Contributing]: https://github.com/joshka/tui-widgets/blob/main/CONTRIBUTING.md

<!-- cargo-rdme end -->
26 changes: 26 additions & 0 deletions tui-qrcode/examples/qrcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use qrcode::QrCode;
use ratatui::{crossterm::event, DefaultTerminal, Frame};
use tui_qrcode::{Colors, QrCodeWidget};

fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let result = run(terminal);
ratatui::restore();
result
}

fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
loop {
terminal.draw(render)?;
if matches!(event::read()?, event::Event::Key(_)) {
break Ok(());
}
}
}

fn render(frame: &mut Frame) {
let qr_code = QrCode::new("https://ratatui.rs").expect("failed to create QR code");
let widget = QrCodeWidget::new(qr_code).colors(Colors::Inverted);
frame.render_widget(widget, frame.area());
}
Loading
Loading