reduced simple flag code reuse

This commit is contained in:
Valerie Wolfe 2023-04-14 13:25:37 -04:00
parent a003aaa797
commit a74f2e5acc
2 changed files with 68 additions and 117 deletions

View file

@ -10,42 +10,60 @@ use termion::{
color::{ Fg, Rgb }, color::{ Fg, Rgb },
cursor, cursor,
input::TermRead, input::TermRead,
raw::IntoRawMode, raw::IntoRawMode
style
}; };
use crate::flag::BLOCK; use crate::flag::BLOCK;
pub fn draw(colors: &[Fg<Rgb>]) { pub fn wait(stdin: io::Stdin) { for _ in stdin.keys() { return; } }
pub fn simple(colors: &[Fg<Rgb>]) {
let mut stdout = io::stdout().into_raw_mode().unwrap(); let mut stdout = io::stdout().into_raw_mode().unwrap();
let stdin = io::stdin(); let stdin = io::stdin();
let count = colors.len();
let (width, height) = terminal_size().unwrap();
let thresh = height as usize / count;
write!(stdout, "{}{}", cursor::Hide, clear::All).ok(); write!(stdout, "{}{}", cursor::Hide, clear::All).ok();
stdout.flush().ok(); stdout.flush().ok();
let stripe = BLOCK.repeat(width as usize); let (width, height) = terminal_size().unwrap();
let reset = style::Reset; stripes(&mut stdout, colors, (width as usize, height as usize));
wait(stdin);
write!(stdout, "{}{}", cursor::Show, clear::All).ok();
stdout.flush().ok();
}
pub fn small(colors: &[Fg<Rgb>]) {
let mut stdout = io::stdout().into_raw_mode().unwrap();
let height = colors.len();
let width = height * 3;
stripes(&mut stdout, colors, (width, height));
}
pub fn stripes(stdout: &mut io::Stdout, colors: &[Fg<Rgb>], size: (usize, usize)) {
let mut stdout = stdout.into_raw_mode().unwrap();
let count = colors.len();
let (width, height) = size;
let thresh = height / count;
let stripe = BLOCK.repeat(width);
let mut index = 0; let mut index = 0;
for n in 0..(height as usize) { for n in 0..height {
if n != 0 && n % thresh == 0 { if n!= 0 && n % thresh == 0 {
index += 1; index += 1;
if index >= count { break; } if index >= count { break; }
} }
write!( write!(
stdout, stdout,
"{color}{stripe}{reset}", "{color}{stripe}\n{repos}",
color = colors[index] color = colors[index],
repos = cursor::Left(width as u16)
).ok(); ).ok();
stdout.flush().ok();
} }
stdout.flush().ok(); stdout.flush().ok();
for _ in stdin.keys() { break; }
write!(stdout, "{}{}", cursor::Show, clear::All).ok();
stdout.flush().ok();
} }

View file

@ -1,7 +1,7 @@
use termion::color; use termion::color;
use crate::draw::draw; use crate::draw;
pub static BLOCK: &str = ""; pub static BLOCK: &str = "";
@ -28,15 +28,9 @@ pub fn pride(small: bool) {
let blue = color::Fg(color::Rgb(0x00, 0x4C, 0xFF)); let blue = color::Fg(color::Rgb(0x00, 0x4C, 0xFF));
let purple = color::Fg(color::Rgb(0x77, 0x00, 0x88)); let purple = color::Fg(color::Rgb(0x77, 0x00, 0x88));
if small { // small flag: 18x6 let stripes = &[red, orange, yellow, green, blue, purple];
let width = 18; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{red}{stripe}\n{orange}{stripe}\n{yellow}{stripe}\n{green}{stripe}\n{blue}{stripe}\n{purple}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[red, orange, yellow, green, blue, purple]); }
} }
pub fn transgender(small: bool) { pub fn transgender(small: bool) {
@ -44,15 +38,9 @@ pub fn transgender(small: bool) {
let blue = color::Fg(color::Rgb(0xEA, 0xAC, 0xB8)); let blue = color::Fg(color::Rgb(0xEA, 0xAC, 0xB8));
let white = color::Fg(color::Rgb(0xFF, 0xFF, 0xFF)); let white = color::Fg(color::Rgb(0xFF, 0xFF, 0xFF));
if small { let stripes = &[pink, blue, white, blue, pink];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{pink}{stripe}\n{blue}{stripe}\n{white}{stripe}\n{blue}{stripe}\n{pink}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[pink, blue, white, blue, pink]); }
} }
// everything below here is alphabetical // everything below here is alphabetical
@ -64,15 +52,9 @@ pub fn aromantic(small: bool) {
let grey = color::Fg(color::Rgb(0xAB, 0xAB, 0xAB)); let grey = color::Fg(color::Rgb(0xAB, 0xAB, 0xAB));
let black = color::Fg(color::Rgb(0x00, 0x00, 0x00)); let black = color::Fg(color::Rgb(0x00, 0x00, 0x00));
if small { let stripes = &[green, lime, white, grey, black];
let width = 15; if small { }
else { draw::simple(stripes); }
println!(
"{green}{stripe}\n{lime}{stripe}\n{white}{stripe}\n{grey}{stripe}\n{black}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[green, lime, white, grey, black]); }
} }
pub fn asexual(small: bool) { pub fn asexual(small: bool) {
@ -81,15 +63,9 @@ pub fn asexual(small: bool) {
let white = color::Fg(color::Rgb(0xFF, 0xFF, 0xFF)); let white = color::Fg(color::Rgb(0xFF, 0xFF, 0xFF));
let purple = color::Fg(color::Rgb(0x81, 0x00, 0x81)); let purple = color::Fg(color::Rgb(0x81, 0x00, 0x81));
if small { let stripes = &[black, grey, white, purple];
let width = 12; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{black}{stripe}\n{grey}{stripe}\n{white}{stripe}\n{purple}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[black, grey, white, purple]); }
} }
pub fn bigender(small: bool) { pub fn bigender(small: bool) {
@ -99,15 +75,9 @@ pub fn bigender(small: bool) {
let purple = color::Fg(color::Rgb(0xAB, 0x6B, 0xBB)); let purple = color::Fg(color::Rgb(0xAB, 0x6B, 0xBB));
let blue = color::Fg(color::Rgb(0x6D, 0x96, 0xDC)); let blue = color::Fg(color::Rgb(0x6D, 0x96, 0xDC));
if small { let stripes = &[pink, yellow, white, purple, blue];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{pink}{stripe}\n{yellow}{stripe}\n{white}{stripe}\n{purple}{stripe}\n{blue}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[pink, yellow, white, purple, blue]); }
} }
pub fn bisexual(small: bool) { pub fn bisexual(small: bool) {
@ -115,15 +85,9 @@ pub fn bisexual(small: bool) {
let purple = color::Fg(color::Rgb(0x91, 0x53, 0x92)); let purple = color::Fg(color::Rgb(0x91, 0x53, 0x92));
let blue = color::Fg(color::Rgb(0x14, 0x37, 0xA2)); let blue = color::Fg(color::Rgb(0x14, 0x37, 0xA2));
if small { let stripes = &[magenta, magenta, purple, blue, blue];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{magenta}{stripe}\n{stripe}\n{purple}{stripe}\n{blue}{stripe}\n{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[magenta, magenta, purple, blue, blue]); }
} }
pub fn gendervoid(small: bool) { pub fn gendervoid(small: bool) {
@ -131,15 +95,9 @@ pub fn gendervoid(small: bool) {
let gray = color::Fg(color::Rgb(0x4A, 0x48, 0x4B)); let gray = color::Fg(color::Rgb(0x4A, 0x48, 0x4B));
let black = color::Fg(color::Rgb(0x00, 0x00, 0x00)); let black = color::Fg(color::Rgb(0x00, 0x00, 0x00));
if small { let stripes = &[navy, gray, black, gray, navy];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{navy}{stripe}\n{gray}{stripe}\n{black}{stripe}\n{gray}{stripe}\n{navy}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[navy, gray, black, gray, navy]); }
} }
pub fn intersex(small: bool) { pub fn intersex(small: bool) {
@ -154,15 +112,9 @@ pub fn lesbian(small: bool) {
let pink = color::Fg(color::Rgb(0xD4, 0x62, 0xA6)); let pink = color::Fg(color::Rgb(0xD4, 0x62, 0xA6));
let magenta = color::Fg(color::Rgb(0xA4, 0x00, 0x62)); let magenta = color::Fg(color::Rgb(0xA4, 0x00, 0x62));
if small { let stripes = &[red, orange, white, pink, magenta];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{red}{stripe}\n{orange}{stripe}\n{white}{stripe}\n{pink}{stripe}\n{magenta}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[red, orange, white, pink, magenta]); }
} }
pub fn multigender(small: bool) { pub fn multigender(small: bool) {
@ -170,15 +122,9 @@ pub fn multigender(small: bool) {
let ltblue = color::Fg(color::Rgb(0x01, 0xA4, 0xE9)); let ltblue = color::Fg(color::Rgb(0x01, 0xA4, 0xE9));
let orange = color::Fg(color::Rgb(0xFB, 0x7F, 0x27)); let orange = color::Fg(color::Rgb(0xFB, 0x7F, 0x27));
if small { let stripes = &[blue, ltblue, orange, ltblue, blue];
let width = 15; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{blue}{stripe}\n{ltblue}{stripe}\n{orange}{stripe}\n{ltblue}{stripe}\n{blue}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[blue, ltblue, orange, ltblue, blue]); }
} }
pub fn nonbinary(small: bool) { pub fn nonbinary(small: bool) {
@ -187,15 +133,9 @@ pub fn nonbinary(small: bool) {
let purple = color::Fg(color::Rgb(0x9B, 0x59, 0xD0)); let purple = color::Fg(color::Rgb(0x9B, 0x59, 0xD0));
let black = color::Fg(color::Rgb(0x00, 0x00, 0x00)); let black = color::Fg(color::Rgb(0x00, 0x00, 0x00));
if small { let stripes = &[yellow, white, purple, black];
let width = 12; if small { draw::small(stripes); }
else { draw::simple(stripes); }
println!(
"{yellow}{stripe}\n{white}{stripe}\n{purple}{stripe}\n{black}{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[yellow, white, purple, black]); }
} }
pub fn pansexual(small: bool) { pub fn pansexual(small: bool) {
@ -203,15 +143,8 @@ pub fn pansexual(small: bool) {
let yellow = color::Fg(color::Rgb(0xFF, 0xDA, 0x00)); let yellow = color::Fg(color::Rgb(0xFF, 0xDA, 0x00));
let cyan = color::Fg(color::Rgb(0x1B, 0xB3, 0xFF)); let cyan = color::Fg(color::Rgb(0x1B, 0xB3, 0xFF));
if small { if small { draw::small(&[magenta, magenta, yellow, yellow, cyan, cyan]); }
let width = 18; else { draw::simple(&[magenta, yellow, cyan]); }
println!(
"{magenta}{stripe}\n{stripe}\n{yellow}{stripe}\n{stripe}\n{cyan}{stripe}\n{stripe}{reset}",
reset = color::Fg(color::Reset),
stripe = BLOCK.repeat(width)
);
} else { draw(&[magenta, yellow, cyan]); }
} }
pub fn polyamorous() { pub fn polyamorous() {