pub struct QrCode { /* private fields */ }Expand description
A QR code as a renderable.
Charset::Blocks(the default;Charset::Brailledraws the same, as Braille dots are too sparse to scan) draws two modules a cell with half blocks (▀▄█), so the code is square andsize + 2 × quiet zonecells wide.- With colour, every cell is painted: the dark modules in
#000000and the light ones in#ffffff, whatever the terminal’s theme makes of its named colours (colourschanges them), so the code reads on a dark or a light terminal. - Without colour, the glyphs draw the light modules in the terminal’s
text colour, so on a dark background (the common case) the code reads
dark on light.
invertdraws the dark modules instead, for a terminal with a light background. Charset::Ascii(and an ASCII-only console) draws each module as two cells,##or spaces, or as painted spaces with colour, so the code is twice as tall.- The quiet zone is 4 modules, as the specification asks
(
quiet_zone). Given less width, the quiet zone narrows to a module; a code that still does not fit is replaced by a note saying how many columns it needs, as a cut code cannot be read.
use rich::Console;
use rich_ext::qr::{ErrorCorrection, QrCode};
let code = QrCode::new("https://example.com").unwrap();
assert_eq!((code.size(), code.version()), (25, 2));
// Medium was asked for; quartile fits the same size, so it is used.
assert_eq!(code.error_correction(), ErrorCorrection::Quartile);
// 25 modules and 4 of quiet zone each side.
let wide = Console::builder().width(40).color_system(None).build();
assert!(wide.render_export(&code).lines().all(|l| l.chars().count() == 33));
// The quiet zone narrows to fit, down to a module.
let narrow = Console::builder().width(28).color_system(None).build();
assert!(narrow.render_export(&code).lines().all(|l| l.chars().count() == 27));
// Narrower still, a note.
let tiny = Console::builder().width(26).color_system(None).build();
assert_eq!(tiny.render_export(&code), "QR code needs 27 columns\n");Implementations§
Source§impl QrCode
impl QrCode
Sourcepub fn with_error_correction(
text: &str,
ecc: ErrorCorrection,
) -> Result<Self, QrError>
pub fn with_error_correction( text: &str, ecc: ErrorCorrection, ) -> Result<Self, QrError>
text at ecc, or higher when the larger level fits the same
size of code. Digits and upper-case text are packed tighter than
other text.
Sourcepub fn from_bytes(data: &[u8], ecc: ErrorCorrection) -> Result<Self, QrError>
pub fn from_bytes(data: &[u8], ecc: ErrorCorrection) -> Result<Self, QrError>
Bytes at ecc (or higher, as with_error_correction).
Sourcepub fn error_correction(&self) -> ErrorCorrection
pub fn error_correction(&self) -> ErrorCorrection
The error correction the code was made with (it may be higher than asked for).
Sourcepub fn module(&self, x: usize, y: usize) -> bool
pub fn module(&self, x: usize, y: usize) -> bool
Whether the module at column x, row y is dark; false outside
the code.
Sourcepub fn quiet_zone(self, modules: usize) -> Self
pub fn quiet_zone(self, modules: usize) -> Self
The light border around the code, in modules (default 4, as the specification asks; scanners often manage with 2).
Sourcepub fn charset(self, charset: Charset) -> Self
pub fn charset(self, charset: Charset) -> Self
Glyphs to draw with (default Charset::Auto: half blocks).
Trait Implementations§
Source§impl AccessibleText for QrCode
QR code, version 2, 25 by 25 modules, medium error correction, then
what it encodes, when that is text.
impl AccessibleText for QrCode
QR code, version 2, 25 by 25 modules, medium error correction, then
what it encodes, when that is text.
Source§fn accessible_text(&self, _width: usize) -> String
fn accessible_text(&self, _width: usize) -> String
The content as plain lines, laid out for
width where layout matters.Source§impl Renderable for QrCode
impl Renderable for QrCode
fn rich_render( &self, console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Source§fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement
The
(minimum, maximum) cell width this renderable wants. Port of
__rich_measure__. The default is what upstream’s Measurement.get
returns for a renderable without one: anything from nothing to the
whole width, (0, max_width).Source§fn fit_to_measurement(&self) -> bool
fn fit_to_measurement(&self) -> bool
Whether a top-level
Console::print shrinks this renderable to its
measured width. Upstream renders every top-level renderable at the full
console width, so the default is false; an extension may opt in.Source§fn vertical(&self) -> Option<VerticalAlign>
fn vertical(&self) -> Option<VerticalAlign>
The vertical alignment a
Table cell holding this renderable uses in
place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.Source§fn print_end_line(&self, _options: &ConsoleOptions) -> bool
fn print_end_line(&self, _options: &ConsoleOptions) -> bool
Whether a top-level print ends this renderable’s last line. This
port’s renderables separate lines, where upstream’s end each line
with
"\n", so a print adds the final line break. A renderable
whose upstream output stops without one (Screen, LiveRender, a
Rule whose end has no newline) returns false, and the next
output continues its last line. options are the print’s options.impl StructuralPartialEq for QrCode
Auto Trait Implementations§
impl Freeze for QrCode
impl RefUnwindSafe for QrCode
impl Send for QrCode
impl Sync for QrCode
impl Unpin for QrCode
impl UnsafeUnpin for QrCode
impl UnwindSafe for QrCode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more