From 47d63cd7f53d057b7510e3cb12a820a4ea7ae3c1 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 26 Jun 2023 11:33:36 -0400 Subject: [PATCH] added more documentation --- src/color.rs | 2 ++ src/draw.rs | 18 ++++++++++++++++++ src/flag.rs | 2 ++ src/main.rs | 4 +++- src/variant.rs | 2 ++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/color.rs b/src/color.rs index 5a3a0b5..dcec15d 100644 --- a/src/color.rs +++ b/src/color.rs @@ -9,7 +9,9 @@ pub static WHITE: Color = Fg(Rgb(0xFF, 0xFF, 0xFF)); pub static RESET: Fg = Fg(Reset); +/// converts a hex integer to Fg(Rgb) pub fn rgb(hex: u32) -> Color { + // colors should be 0xrrggbb = 0x__rrggbb; drop the most significant byte let [_, r, g, b] = hex.to_be_bytes(); Fg(Rgb(r, g, b)) diff --git a/src/draw.rs b/src/draw.rs index c60a747..2b3255e 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -12,49 +12,67 @@ use termion::{ use crate::color::{ RESET, Colors }; use crate::flag::BLOCK; +/// draw a fullscreen stripe flag and hold for keypress pub fn full(colors: Colors) { + // prepare stdin and stdout let mut stdout = io::stdout().into_raw_mode().unwrap(); let stdin = io::stdin(); + // get constraints let count = colors.len(); let (width, height) = terminal_size().unwrap(); let thresh = height as usize / count; + // clear the terminal write!(stdout, "{}{}", cursor::Hide, clear::All).ok(); stdout.flush().ok(); + // build terminal width stripe string let stripe = BLOCK.repeat(width as usize); + // create our color index let mut index = 0; + // for every terminal row... for n in 0..(height as usize) { + // ... increment our index at color change threshold if n != 0 && n % thresh == 0 { index += 1; + // and break if out of bounds if index >= count { break; } } + // ... draw the stripe with color at index write!( stdout, "{color}{stripe}{RESET}", color = colors[index] ).ok(); } + // flush stdout stdout.flush().ok(); + // wait for keypress for _ in stdin.keys() { break; } write!(stdout, "{}{}", cursor::Show, clear::All).ok(); stdout.flush().ok(); } +/// draws a small stripe flag pub fn small(colors: Colors) { + // prepare stdout let mut stdout = io::stdout(); + // get constraints let count = colors.len(); let width = count * 3; + // build small stripe string let stripe = BLOCK.repeat(width); + // print a stripe for all colors for color in colors { println!("{color}{stripe}"); } + // reset our foreground color to play nice and flush stdout print!("{RESET}"); stdout.flush().ok(); } diff --git a/src/flag.rs b/src/flag.rs index eb2c31c..db8078b 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -1,3 +1,5 @@ +//! stripe pride flag color functions. +//! all of these return a Vec of colors to be drawn from first to last. use crate::color::*; diff --git a/src/main.rs b/src/main.rs index fe1a93f..65fb719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use crate::color::Colors; static VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { + // collect args let mut args = Arguments::from_env(); // handle help flag @@ -37,6 +38,7 @@ fn main() { let subcommand = args.subcommand().unwrap(); + // get color vec from matched flag let colors: Colors = match subcommand.as_deref() { Some("pride" | "rainbow") | None @@ -95,7 +97,7 @@ fn main() { Some("pansexual" | "pan") => flag::pansexual(), - _ => { help_text(); exit(1) } + _ => { help_text(); exit(1) } // (or die) }; if small { draw::small(colors); } diff --git a/src/variant.rs b/src/variant.rs index 127d71d..d2062cb 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -1,3 +1,5 @@ +//! variant pride flags +//! these aren't in the flag module for organizational reasons. use crate::{ color::*,