aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index e5cc596..b8e2e4b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,6 +12,7 @@ use clap::{crate_version, Clap, IntoApp};
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_generate::{generate, Shell};
use log::{error, warn};
+use qrcodegen::{QrCode, QrCodeEcc};
use yansi::{Color, Paint};
mod archive;
@@ -183,7 +184,6 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
Some(ref random_route) => format!("{}/{}", url, random_route),
None => url,
})
- .map(|url| Color::Green.paint(url).bold().to_string())
.collect::<Vec<_>>()
};
@@ -246,9 +246,31 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
println!(
"Availabe at (non-exhaustive list):\n {}\n",
- display_urls.join("\n "),
+ display_urls
+ .iter()
+ .map(|url| Color::Green.paint(url).bold().to_string())
+ .collect::<Vec<_>>()
+ .join("\n "),
);
+ // print QR code to terminal
+ if miniserve_config.show_qrcode && atty::is(atty::Stream::Stdout) {
+ for url in display_urls
+ .iter()
+ .filter(|url| !url.contains("//127.0.0.1:") && !url.contains("//[::1]:"))
+ {
+ match QrCode::encode_text(url, QrCodeEcc::Low) {
+ Ok(qr) => {
+ println!("QR code for {} :", Color::Green.paint(url).bold());
+ print_qr(&qr);
+ }
+ Err(e) => {
+ error!("Failed to render QR to terminal: {}", e);
+ }
+ };
+ }
+ }
+
if atty::is(atty::Stream::Stdout) {
println!("Quit by pressing CTRL-C");
}
@@ -338,3 +360,21 @@ async fn css() -> impl Responder {
.insert_header(ContentType(mime::TEXT_CSS))
.message_body(css.into())
}
+
+// Prints the given QrCode object to the console.
+fn print_qr(qr: &QrCode) {
+ let border: i32 = 4;
+ for y in (-border..qr.size() + border).step_by(2) {
+ for x in -border..qr.size() + border {
+ let c: char = match (qr.get_module(x, y), qr.get_module(x, y + 1)) {
+ (false, false) => ' ',
+ (true, false) => '▀',
+ (false, true) => '▄',
+ (true, true) => '█',
+ };
+ print!("{0}", c);
+ }
+ println!();
+ }
+ println!();
+}