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