|
| 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 --> |
0 commit comments