Skip to content

Commit 61f78d7

Browse files
committed
feat: add tui-qrcode crate
1 parent 88e3bdd commit 61f78d7

File tree

4 files changed

+451
-0
lines changed

4 files changed

+451
-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

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
## Usage
8+
9+
Add qrcode and tui-qrcode to your Cargo.toml. You can disable the default features of qrcode as
10+
we don't need the code which renders the QR code to an image.
11+
12+
```shell
13+
cargo add qrcode tui-qrcode --no-default-features
14+
```
15+
16+
## Example
17+
18+
This example can be found in the `examples` directory of the repository.
19+
20+
```rust
21+
use qrcode::QrCode;
22+
use ratatui::{crossterm::event, DefaultTerminal, Frame};
23+
use tui_qrcode::{Colors, QrCodeWidget};
24+
25+
fn main() -> color_eyre::Result<()> {
26+
color_eyre::install()?;
27+
let terminal = ratatui::init();
28+
let result = run(terminal);
29+
ratatui::restore();
30+
result
31+
}
32+
33+
fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
34+
loop {
35+
terminal.draw(render)?;
36+
if matches!(event::read()?, event::Event::Key(_)) {
37+
break Ok(());
38+
}
39+
}
40+
}
41+
42+
fn render(frame: &mut Frame) {
43+
let qr_code = QrCode::new("https://ratatui.rs").expect("failed to create QR code");
44+
let widget = QrCodeWidget::new(qr_code).colors(Colors::Inverted);
45+
frame.render_widget(widget, frame.area());
46+
}
47+
```
48+
49+
Renders the following QR code:
50+
51+
```text
52+
█████████████████████████████████
53+
█████████████████████████████████
54+
████ ▄▄▄▄▄ █▄ ▄▄▄ ████ ▄▄▄▄▄ ████
55+
████ █ █ █▄▄▄█▀▄██ █ █ █ ████
56+
████ █▄▄▄█ █▀ ▄▀ ███ █▄▄▄█ ████
57+
████▄▄▄▄▄▄▄█▄▀▄█ ▀▄▀ █▄▄▄▄▄▄▄████
58+
████ █▄▀▀▀▄▄▀▄▄ ▄█▀▄█▀ █▀▄▀ ████
59+
██████▀█ ▄▀▄▄▀▀ ▄ ▄█ ▄▄█ ▄█▄████
60+
████▄▀▀▀▄▄▄▄▀█▄▄█ ▀ ▀ ▀███▀ ████
61+
████▄▄ ▀█▄▄▀▄▄ ▄█▀█▄▀█▄▀▀ ▄█▄████
62+
████▄▄█▄██▄█ ▄▀▄ ▄█ ▄▄▄ ██▄▀████
63+
████ ▄▄▄▄▄ █▄▄▄▀ ▄ ▀ █▄█ ███ ████
64+
████ █ █ ██ ███ ▄▄ ▄▄ █▀ ▄████
65+
████ █▄▄▄█ █▄▀ ▄█▀█▀ ▄█ ▄█▄▄████
66+
████▄▄▄▄▄▄▄█▄▄█▄▄▄██▄█▄██▄██▄████
67+
█████████████████████████████████
68+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
69+
```
70+
71+
<!-- 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)