2023-06-19 19:21:05 -04:00
|
|
|
use std::io::{ self, Write };
|
2023-04-13 15:04:05 -04:00
|
|
|
|
|
|
|
use termion::{
|
|
|
|
terminal_size,
|
|
|
|
|
|
|
|
clear,
|
2023-07-03 17:43:54 -04:00
|
|
|
color::{ Bg, Fg, Rgb },
|
2023-04-13 15:04:05 -04:00
|
|
|
cursor,
|
|
|
|
input::TermRead,
|
2023-06-19 19:21:05 -04:00
|
|
|
raw::IntoRawMode
|
2023-04-13 15:04:05 -04:00
|
|
|
};
|
|
|
|
|
2023-07-07 13:36:03 -04:00
|
|
|
use crate::{
|
|
|
|
color::{ RESET, RESET_BG },
|
|
|
|
flag::Flag
|
|
|
|
};
|
2023-06-22 20:20:45 -04:00
|
|
|
|
|
|
|
pub static BLOCK: &str = "█";
|
|
|
|
pub static UHALF: &str = "▀";
|
2023-04-13 15:04:05 -04:00
|
|
|
|
2023-07-07 12:36:31 -04:00
|
|
|
pub fn draw_lines(lines: Vec<String>, hold: bool) {
|
2023-07-03 13:11:45 -04:00
|
|
|
let mut stdout = io::stdout().into_raw_mode().unwrap();
|
|
|
|
|
|
|
|
let count = lines.len() as u16;
|
|
|
|
for _ in 0..count { write!(stdout, "\n").ok(); }
|
|
|
|
write!(stdout, "{}", cursor::Up(count)).ok();
|
|
|
|
|
|
|
|
if hold { write!(stdout, "{}{}", cursor::Hide, clear::All).ok(); }
|
|
|
|
|
|
|
|
let down = cursor::Down(1);
|
|
|
|
for line in lines {
|
|
|
|
let left = cursor::Left(line.len() as u16);
|
|
|
|
write!(stdout, "{line}{left}{down}").ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(stdout, "{RESET}{RESET_BG}").ok();
|
|
|
|
stdout.flush().ok();
|
|
|
|
if hold {
|
|
|
|
let stdin = io::stdin();
|
|
|
|
for _ in stdin.keys() { break; }
|
|
|
|
write!(stdout, "{}", clear::All).ok();
|
|
|
|
}
|
|
|
|
write!(stdout, "{}", cursor::Show).ok();
|
|
|
|
stdout.flush().ok();
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:43:54 -04:00
|
|
|
pub fn fg_stripes(colors: Vec<Fg<Rgb>>, width: u16, height: u16) -> Vec<String> {
|
|
|
|
let width = width as usize;
|
|
|
|
let height = height as usize;
|
|
|
|
let count = colors.len();
|
|
|
|
|
|
|
|
let thresh = height / count;
|
|
|
|
|
|
|
|
let stripe = BLOCK.repeat(width);
|
|
|
|
let mut output = Vec::new();
|
|
|
|
|
2023-06-26 11:33:36 -04:00
|
|
|
// create our color index
|
2023-07-03 17:43:54 -04:00
|
|
|
let mut index = 0;
|
|
|
|
for n in 0..height {
|
|
|
|
if n != 0 && n % thresh == 0 {
|
|
|
|
index += 1;
|
2023-06-26 11:33:36 -04:00
|
|
|
// and break if out of bounds
|
2023-07-03 17:43:54 -04:00
|
|
|
if index >= count { break; }
|
|
|
|
}
|
|
|
|
let color = colors[index];
|
|
|
|
output.push(format!("{color}{stripe}"));
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
pub fn bg_stripes(colors: Vec<Bg<Rgb>>, width: u16, height: u16) -> Vec<String> {
|
|
|
|
let width = width as usize;
|
|
|
|
let height = height as usize;
|
|
|
|
let count = colors.len();
|
|
|
|
|
|
|
|
let thresh = height / count;
|
|
|
|
|
|
|
|
let stripe = " ".repeat(width);
|
|
|
|
let mut output = Vec::new();
|
|
|
|
|
|
|
|
let mut index = 0;
|
|
|
|
for n in 0..height {
|
|
|
|
if n != 0 && n % thresh == 0 {
|
|
|
|
index += 1;
|
|
|
|
if index >= count { break; }
|
|
|
|
}
|
|
|
|
let color = colors[index];
|
|
|
|
output.push(format!("{color}{stripe}"));
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2023-07-07 12:36:31 -04:00
|
|
|
impl Flag {
|
|
|
|
pub fn draw(self, hold: bool) {
|
|
|
|
let lines = match self {
|
|
|
|
Flag::Stripes(colors)
|
|
|
|
=> {
|
|
|
|
let (width, height);
|
|
|
|
if hold { (width, height) = terminal_size().unwrap(); }
|
|
|
|
else {
|
|
|
|
height = colors.len() as u16;
|
|
|
|
width = height * 3;
|
|
|
|
}
|
|
|
|
fg_stripes(colors, width, height)
|
|
|
|
},
|
|
|
|
Flag::Lines(lines)
|
|
|
|
=> lines
|
|
|
|
};
|
|
|
|
draw_lines(lines, hold);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|