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