diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6a1a9..c9e4251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,18 @@ # Changelog -## [unreleased] +## [0.2.0] - 2022-07-18 ### Added +- Feature to read string to encode from standard input, which allows to pipe + commands to `qr-rs` (closing issue #1). - `border` CLI option to specify border size. -- `error-correction-level` CLI option to specify QR *error-correction-level*. +- `error-correction-level` CLI option to specify QR *error-correction-level* as + one of the following values: + - **low**; + - **medium**; + - **quartile**; + - **high**. ## [0.1.0] - 2022-07-15 diff --git a/Cargo.lock b/Cargo.lock index 36a3a86..6b20300 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -618,7 +618,7 @@ dependencies = [ [[package]] name = "qr-rs" -version = "0.1.0" +version = "0.2.0" dependencies = [ "atty", "clap", diff --git a/Cargo.toml b/Cargo.toml index 5f80fe7..e478274 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "qr-rs" -version = "0.1.0" +version = "0.2.0" authors = ["Marco Radocchia -Encode URLs or text into QR codes. +A CLI utility to encode URLs or text into QR codes in various formats and colors. USAGE: - qr [OPTIONS] + qr [OPTIONS] [STRING] ARGS: String to encode OPTIONS: - -b, --bg Foreground color (hex code) [default: #FFF] - -f, --fg Background color (hex code) [default: #000] - -h, --help Print help information - -o, --output Output file (supported file extensions: jpeg, jpg, png, svg); omit to - print QR code to console - -s, --scale Scale factor (raster image output only) [default: 25] - -V, --version Print version information + -b, --bg + Foreground color (hex code) [default: #FFF] + + -B, --border + Border size (expressed in unit blocks) [default: 1] + + --error-correction-level + QR error orrection level [default: medium] [possible values: low, medium, quartile, + high] + + -f, --fg + Background color (hex code) [default: #000] + + -h, --help + Print help information + + -o, --output + Output file (supported file extensions: jpeg, jpg, png, svg); omit to print QR code to + console + + -s, --scale + Scale factor (raster image output only) [default: 25] + + -V, --version + Print version information ``` ## Changelog diff --git a/src/args.rs b/src/args.rs index 687792e..6881781 100644 --- a/src/args.rs +++ b/src/args.rs @@ -19,7 +19,7 @@ pub use clap::Parser; use qrcodegen::QrCodeEcc; use std::path::PathBuf; -/// Encode URLs or text into QR codes. +/// A CLI utility to encode URLs or text into QR codes in various formats and colors. #[derive(Parser, Debug)] #[clap( author = "Marco Radocchia ", @@ -72,5 +72,5 @@ pub struct Args { /// String to encode. #[clap(value_parser)] - pub string: String, + pub string: Option, } diff --git a/src/main.rs b/src/main.rs index 1459185..3fc65b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,13 @@ use dialoguer::{theme::ColorfulTheme, Confirm}; use error::{Error, ErrorKind, Warning}; use image::{ImageBuffer, RgbImage}; use qrcodegen::QrCode; -use std::{fmt::Write as _, fs, io::Write, path::Path, process}; +use std::{ + fmt::Write as _, + fs, + io::{self, Read, Write}, + path::Path, + process, +}; use utils::hex_to_rgb; /// QR code. @@ -42,7 +48,7 @@ impl Qr { fn new(data: QrCode, border: u8) -> Self { Self { data, - border: border.into() + border: border.into(), } } } @@ -144,10 +150,18 @@ impl QrOutput for Qr { Ok(()) } + /// Print QR code to standard output. fn console(&self) { for y in -self.border..self.data.size() + self.border { for x in -self.border..self.data.size() + self.border { - print!("{0}{0}", if self.data.get_module(x, y) { '█' } else { ' ' }); + print!( + "{0}{0}", + if self.data.get_module(x, y) { + '█' + } else { + ' ' + } + ); } println!(); } @@ -155,9 +169,26 @@ impl QrOutput for Qr { } /// Runs the program & catches errors. -fn run(args: &Args) -> Result<(), ErrorKind> { +fn run(args: Args) -> Result<(), ErrorKind> { + // If string to encode is not passed in as CLI argument, check stdin for piped string. + let string = args.string.unwrap_or_else(|| { + if atty::is(atty::Stream::Stdin) { + clap::Command::new("qr [OPTIONS] [STRING]") + .error( + clap::ErrorKind::MissingRequiredArgument, + "Missing input string.\n\n\ + \tEither provide it as a CLI argument or pipe it in from standard input.", + ) + .exit(); + } else { + let mut string = String::new(); + io::stdin().lock().read_to_string(&mut string).unwrap(); + string.trim_end().to_string() + } + }); + // Generate QR code. - let qr = match QrCode::encode_text(&args.string, args.error_correction_level) { + let qr = match QrCode::encode_text(&string, args.error_correction_level) { Ok(data) => Qr::new(data, args.border), Err(err) => return Err(ErrorKind::Error(Error::QrCodeErr(err.to_string()))), }; @@ -200,7 +231,7 @@ fn run(args: &Args) -> Result<(), ErrorKind> { /// Main function: calls run function and prints errors. fn main() { - if let Err(e) = run(&Args::parse()) { + if let Err(e) = run(Args::parse()) { e.colorize().unwrap(); process::exit(1); } diff --git a/src/utils.rs b/src/utils.rs index e9622fc..eff02d7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -41,7 +41,11 @@ pub fn parse_error_correction_level(ecl: &str) -> Result { }) } -/// This conversion assumes the HEX string as valid color and returns corresponding RGB value. +/// Convert HEX color code to RGB values +/// +/// # Note +/// +/// This function assumes `hex` parameter being a valid HEX color code. pub fn hex_to_rgb(hex: &str) -> [u8; 3] { let mut hex = hex.strip_prefix('#').unwrap().to_string(); if hex.len() == 3 {