Skip to content

Commit e702d8d

Browse files
committed
feat: add tui-qrcode crate
1 parent f272002 commit e702d8d

File tree

4 files changed

+477
-0
lines changed

4 files changed

+477
-0
lines changed

tui-qrcode/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "tui-qrcode"
3+
version = "0.1.0"
4+
description = "A Ratatui widget for displaying QR codes in the terminal"
5+
documentation = "https://docs.rs/tui-qrcode"
6+
7+
authors.workspace = true
8+
license.workspace = true
9+
repository.workspace = true
10+
edition.workspace = true
11+
rust-version.workspace = true
12+
categories.workspace = true
13+
keywords.workspace = true
14+
15+
[dependencies]
16+
color-eyre.workspace = true
17+
qrcode = { version = "0.14.1", default-features = false }
18+
ratatui.workspace = true
19+
20+
[dev-dependencies]
21+
ratatui = { workspace = true, features = ["crossterm"] }

tui-qrcode/README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# TUI QR Code
2+
3+
<!-- cargo-rdme start -->
4+
5+
TUI QR Code is a library for rendering QR codes in a terminal using the [Ratatui] crate.
6+
7+
[![Crate badge]][tui-qrcode]
8+
[![Docs.rs Badge]][API Docs]
9+
[![Deps.rs Badge]][Dependency Status]
10+
[![License Badge]](./LICENSE-MIT)
11+
[![Discord Badge]][Ratatui Discord]
12+
13+
[GitHub Repository] · [API Docs] · [Examples] · [Changelog] · [Contributing]
14+
15+
## Usage
16+
17+
Add qrcode and tui-qrcode to your Cargo.toml. You can disable the default features of qrcode as
18+
we don't need the code which renders the QR code to an image.
19+
20+
```shell
21+
cargo add qrcode tui-qrcode --no-default-features
22+
```
23+
24+
## Example
25+
26+
This example can be found in the `examples` directory of the repository.
27+
28+
```rust
29+
use qrcode::QrCode;
30+
use ratatui::{crossterm::event, DefaultTerminal, Frame};
31+
use tui_qrcode::{Colors, QrCodeWidget};
32+
33+
fn main() -> color_eyre::Result<()> {
34+
color_eyre::install()?;
35+
let terminal = ratatui::init();
36+
let result = run(terminal);
37+
ratatui::restore();
38+
result
39+
}
40+
41+
fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
42+
loop {
43+
terminal.draw(render)?;
44+
if matches!(event::read()?, event::Event::Key(_)) {
45+
break Ok(());
46+
}
47+
}
48+
}
49+
50+
fn render(frame: &mut Frame) {
51+
let qr_code = QrCode::new("https://ratatui.rs").expect("failed to create QR code");
52+
let widget = QrCodeWidget::new(qr_code).colors(Colors::Inverted);
53+
frame.render_widget(widget, frame.area());
54+
}
55+
```
56+
57+
Renders the following QR code:
58+
59+
```text
60+
█████████████████████████████████
61+
█████████████████████████████████
62+
████ ▄▄▄▄▄ █▄ ▄▄▄ ████ ▄▄▄▄▄ ████
63+
████ █ █ █▄▄▄█▀▄██ █ █ █ ████
64+
████ █▄▄▄█ █▀ ▄▀ ███ █▄▄▄█ ████
65+
████▄▄▄▄▄▄▄█▄▀▄█ ▀▄▀ █▄▄▄▄▄▄▄████
66+
████ █▄▀▀▀▄▄▀▄▄ ▄█▀▄█▀ █▀▄▀ ████
67+
██████▀█ ▄▀▄▄▀▀ ▄ ▄█ ▄▄█ ▄█▄████
68+
████▄▀▀▀▄▄▄▄▀█▄▄█ ▀ ▀ ▀███▀ ████
69+
████▄▄ ▀█▄▄▀▄▄ ▄█▀█▄▀█▄▀▀ ▄█▄████
70+
████▄▄█▄██▄█ ▄▀▄ ▄█ ▄▄▄ ██▄▀████
71+
████ ▄▄▄▄▄ █▄▄▄▀ ▄ ▀ █▄█ ███ ████
72+
████ █ █ ██ ███ ▄▄ ▄▄ █▀ ▄████
73+
████ █▄▄▄█ █▄▀ ▄█▀█▀ ▄█ ▄█▄▄████
74+
████▄▄▄▄▄▄▄█▄▄█▄▄▄██▄█▄██▄██▄████
75+
█████████████████████████████████
76+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
77+
```
78+
79+
[Ratatui]: https://crates.io/crates/ratatui
80+
[Crate badge]: https://img.shields.io/crates/v/tui-qrcode.svg?style=for-the-badge
81+
[tui-qrcode]: https://crates.io/crates/tui-qrcode
82+
[Docs.rs Badge]: https://img.shields.io/badge/docs.rs-tui--qrcode-blue?style=for-the-badge
83+
[API Docs]: https://docs.rs/tui-qrcode
84+
[Deps.rs Badge]: https://deps.rs/repo/github/joshka/tui-qrcode/status.svg?style=for-the-badge
85+
[Dependency Status]: https://deps.rs/repo/github/joshka/tui-qrcode
86+
[License Badge]: https://img.shields.io/crates/l/tui-qrcode?style=for-the-badge
87+
[Discord Badge]:
88+
https://img.shields.io/discord/1070692720437383208?label=ratatui+discord&logo=discord&style=for-the-badge
89+
[Ratatui Discord]: https://discord.gg/pMCEU9hNEj
90+
[GitHub Repository]: https://github.com/joshka/tui-widgets
91+
[Examples]: https://github.com/joshka/tui-widgets/tree/main/tui-qrcode/examples
92+
[Changelog]: https://github.com/joshka/tui-widgets/blob/main/tui-qrcode/CHANGELOG.md
93+
[Contributing]: https://github.com/joshka/tui-widgets/blob/main/CONTRIBUTING.md
94+
95+
<!-- cargo-rdme end -->

tui-qrcode/examples/qrcode.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use qrcode::QrCode;
2+
use ratatui::{crossterm::event, style::Stylize, DefaultTerminal, Frame};
3+
use tui_qrcode::{Colors, QrCodeWidget};
4+
5+
fn main() -> color_eyre::Result<()> {
6+
color_eyre::install()?;
7+
let terminal = ratatui::init();
8+
let result = run(terminal);
9+
ratatui::restore();
10+
result
11+
}
12+
13+
fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
14+
loop {
15+
terminal.draw(render)?;
16+
if matches!(event::read()?, event::Event::Key(_)) {
17+
break Ok(());
18+
}
19+
}
20+
}
21+
22+
fn render(frame: &mut Frame) {
23+
let qr_code = QrCode::new("https://ratatui.rs").expect("failed to create QR code");
24+
let widget = QrCodeWidget::new(qr_code).colors(Colors::Inverted).blue();
25+
frame.render_widget(widget, frame.area());
26+
}

0 commit comments

Comments
 (0)