rcli_loader/
drawer_helper.rs

1use crate::{loading_element::LoadingElement, terminal_helper::V2Usz};
2pub enum Position {
3    TOP,BOTTOM
4}
5
6pub trait LoadingColorScheme {
7    fn get_char_block_color(&self, l_elem: &LoadingElement) -> Box<str>; // WARNING: Return of string as color with ANSI codes is unreliable, this should be made to some kind of struct?
8}
9
10pub struct RedGreenScheme {}
11impl LoadingColorScheme for RedGreenScheme {
12    fn get_char_block_color(&self, l_elem: &LoadingElement) -> Box<str> {
13        let ratio = if l_elem.get_max() > 0 {255 * l_elem.get_progress() / l_elem.get_max()} else {0}; // Note: This ratio calculation might break if 56 bits of precision is used on a 64 bit machine, or 24 bits on a 32 bit machine
14        return Box::from(format!("\x1b[38;2;{r};{g};{b}m", r=255-ratio, g=ratio, b=0)) // Returns 24 bit color
15    }
16}
17
18pub struct BlueScheme {}
19impl LoadingColorScheme for BlueScheme {
20    fn get_char_block_color(&self, _l_elem: &LoadingElement) -> Box<str> {
21        Box::from(format!("\x1b[38;2;{r};{g};{b}m", r=0, g=0, b=255)) // Returns 24 bit color
22    }
23}
24
25// TODO: This might work more effectively as a macro
26pub fn set_terminal_pos(pos: V2Usz) {
27    print!("\x1B[{line};{column}H", column = pos.x as usize, line = pos.y as usize);
28}
29
30// Note: Does not flush stdout
31// Prints Unicode U+2500 '─'
32pub fn print_splitter_line(terminal_size: &V2Usz, offset_height: usize) {
33    set_terminal_pos(V2Usz { x: 0, y: offset_height }); // Set proper positioning
34    print!("{end:\u{2500}>times$}", end="", times=terminal_size.x as usize); // Print 
35}
36
37