sin_wave 0.2.0

Sin Wave creater for nice effects in the terminal.
use termion;
use std::{thread, time::{self, Duration}, env};
use round;
use colored::{ColoredString, Colorize};

fn main() {
    let t = time::Instant::now();

    let args: Vec<String> = env::args().collect::<Vec<String>>()[1..].to_vec();
    // if args.len() == 1 {
    //     let mut l10 = [0; 10];
    //     let c = get_col(&args[0]);
    //     loop {
    //         l10 = sin(t, l10, c);
    //     }
    // } else if args.len() == 2 {
    //     let mut l20 = [0; 20];
    //     let c1 = get_col(&args[1]);
    //     let c2 = get_col(&args[0]);
    //     loop {
    //         // l20 = sin_cos(t, l20, c1, c2);
    //         break
    //     }
    // } else {
    //     panic!("Not correct amount of arguments.")
    // }

    let mut points = Vec::new();
    let mut shifts = Vec::new();
    let mut cols: Vec<(u8,u8,u8)> = Vec::new();

    //Sets everything up -- all the colours, points and displacements
    for i in 0..args.len() {
        let l10 = [0;10];
        points.push(l10);
        shifts.push(round::round((i as f64 / args.len() as f64) * 628.0, 0) as usize);
        cols.push(get_col(&args[i]));
    }

    let x:usize = termion::terminal_size().unwrap().0 as usize;
    let mut string = vec![0;x];
    
    loop {
        string = vec![0;x];
        for index in 0..points.len() {
            let i = &points[index];
            // change_point(t,  &mut i, x);
            (string, points[index]) = add_on_str(t, &mut string, i, x, &shifts[index]);
        }
        let out = print_format(&string, &cols);
        thread::sleep(time::Duration::from_millis(10));
        println!("{out}");
    }



}

fn print_format(l: &Vec<u8>, cols:  &Vec<(u8,u8,u8)>) -> String {
    return l.into_iter().map(|x| 
        if *x == 0 { format!("{}", ColoredString::from(" ")) } 
        else {
            let mut o = format!("{}", ColoredString::from(" "));
            for c in 0..cols.len() {
                if (x - 1) / 2 == c as u8 {
                    if  x % 2 == 0 {
                        o = format!("{}", "0".truecolor(cols[c].0,cols[c].1,cols[c].2))
                    } else {
                        o = format!("{}", "-".truecolor(cols[c].0,cols[c].1,cols[c].2))
                    }
                    
                }
            }
            o
        }
    ).collect::<String>();
}

fn get_col(s: &String) -> (u8,u8,u8) {
    let m = &*s.to_lowercase();
    let m = m.trim();
    match m {
        "red" | "r" => (230, 25, 25),
        "green" | "g" => (0,255,0),
        "blue" | "b" => (59, 121, 229),
        "white" | "w"=> (200,200,200),
        "black" | "k"=> (0,0,0),
        "purple" | "p" => (152, 19, 209),
        _ => todo!(),
    }
}

fn add_on_str(now: time::Instant, base: &Vec<u8>, addition: &[usize;10], x: usize, shift: &usize) -> (Vec<u8>, [usize;10]) {
    let raw_sin = f64::sin(now.elapsed().as_millis() as f64 / 200.0 + *shift as f64 / 100.0) / 2. + 0.5;
    let pos:usize = round::round(raw_sin * (x-1) as f64, 0) as usize;

    let mut add_out = [0;10];
    let mut base_out = base.clone();

    for i in 0..9 {
        add_out[i] = addition[i + 1];
    }

    add_out[9] = pos;

    let dash_num:u8 = get_biggest(base) + 1;
    let point_num:u8 = dash_num + 1;
    for i in &addition[..addition.len() - 1] {
        if base[*i] != 0 {
            if base[*i]%2 != 0 {
                base_out[*i] = dash_num;
            }
        } else if base[*i] == 0 {
            base_out[*i] = dash_num;
        }
    }
    // println!("{:?}", addition);
    // base[2] = 3;

    base_out[addition[9]] = point_num;
    // println!("{:?}",base_out);
    (base_out, add_out)
}

fn change_point(now: time::Instant, lt: &mut [usize;11], x: usize) {
    let raw_sin = f64::sin(now.elapsed().as_millis() as f64 / 200.0 + lt[10] as f64 / 100.0) / 2. + 0.5;
    let pos:usize = round::round(raw_sin * (x-1) as f64, 0) as usize;

    let mut out = [0;11];
    for i in 0..9 {
        out[i] = lt[i + 1];
        println!("{}",lt[i+2]);
    }
    out[9] = pos;
    out[10] = lt[10];

    *lt = out;
    // println!("{:?}", lt);
}

fn get_biggest(x: &Vec<u8>) -> u8{
    let mut largest = &0;
    for i in x{
        if i > largest {
            largest = i;
        }
    }
    *largest
}


fn to_str(l:Vec<char>, c1: (u8,u8,u8), c2: (u8,u8,u8)) -> String {
    return l.into_iter().map(|x|
        if x == 's' { format!("{}","0".to_string().truecolor(c1.0,c1.1,c1.2)) } 
        else if x == 'c' { format!("{}","0".to_string().truecolor(c2.0,c2.1,c2.2)) }
        else if x == '-' { format!("{}","-".to_string().truecolor(c1.0,c1.1,c1.2)) }
        else if x == '`' { format!("{}","-".to_string().truecolor(c2.0,c2.1,c2.2)) }
        else { " ".to_string() }
    ).collect::<String>();
    // let mut out = String::new();

    // let r = format!("{}","0".to_string().red());
    // let b = format!("{}","0".to_string().blue());
    // let b_d = format!("{}","-".to_string().blue());
    // let r_d = format!("{}","-".to_string().red());

    // for i in 0..l.len() {
    //     out.push_str(
            
    //     );
    // }   
    // // out.pop();
    // // out.pop();
    // out

}



// 

fn to_str_basic(l:Vec<char>, c: (u8,u8,u8)) -> String{
    // println!("{}",l.clone().into_iter().map(|x| if x == 's' { format!("{}", "0".to_string().red()) } else { format!("{}", ColoredString::from(" "))} ).collect::<String>());
    return l.into_iter().map(|x| if x == '0' { format!("{}", "0".to_string().truecolor(c.0, c.1, c.2)) } else if x == '-' { format!("{}", "-".to_string().truecolor(c.0,c.1,c.2)) } else { format!("{}", ColoredString::from(" "))} ).collect::<String>();
}


// fn sin_cos(now: time::Instant, lt: [u16;20], c1: (u8,u8,u8), c2: (u8,u8,u8))  -> [u16;20] {

//     let x = termion::terminal_size().unwrap().0;

//     let mut line = vec![' ';x as usize];

//     let raw_sin = f64::sin(now.elapsed().as_millis() as f64 / 200.0) / 2. + 0.5;
//     let pos_s:u16 = round::round(raw_sin * (x-1) as f64, 0) as u16;

//     let raw_cos = f64::cos(now.elapsed().as_millis() as f64 / 200.0) / 2. + 0.5;
//     let pos_c:u16 = round::round(raw_cos * (x-1) as f64, 0) as u16;

//     for i in 0..lt.len() {
//         line[lt[i] as usize] = '-'
//     }
//     for i in (lt.len() / 2)..lt.len() {
//         line[lt[i] as usize] = '`'
//     }

//     line[pos_c as usize] = 'c';

//     line[pos_s as usize] = 's';

//     println!("{}", to_str(line, c1, c2));
//     // println!("{}", to_str_basic(line));
//     thread::sleep(Duration::from_millis(10));


//     [lt[1], lt[2], lt[3], lt[4], lt[5], lt[6], lt[7], lt[8], lt[9],pos_s, lt[11], lt[12], lt[13], lt[14], lt[15], lt[16], lt[17], lt[18], lt[19],pos_c]
// }

// fn sin(now: time::Instant, lt: [u16;10], c: (u8,u8,u8))  -> [u16;10] {
    //     let x = termion::terminal_size().unwrap().0;
    
    //     let mut line = vec![' ';x as usize];
    
    
    //     let raw_sin = f64::sin(now.elapsed().as_millis() as f64 / 200.0) / 2. + 0.5;
    //     let pos:u16 = round::round(raw_sin * (x-1) as f64, 0) as u16;
    
    //     // let raw_cos = 1.0 - (f64::sin(now.elapsed().as_millis() as f64 / 200.0) / 2. + 0.5);
    //     // let pos_c:u16 = round::round(raw_cos * (x-1) as f64, 0) as u16;
        
    //     for i in lt {
    //         line[i as usize] = '-'
    //     }
    
    //     // line[pos as usize] = '0';
    //     // if pos != 0 {for i in 0..pos { line.push_str("-") }}
    //     // line.push('0');
    //     // if pos != (x-1) {for i in (pos+1)..(x-1) { line.push_str("-") }}
    
    //     line[pos as usize] = '0';
        
    //     // thread::sleep(time::Duration::from_millis(6));
    
    //     println!("{}", to_str_basic(line, c));
    
    //     thread::sleep(Duration::from_millis(10));
    
    
    //     [lt[1], lt[2], lt[3], lt[4], lt[5], lt[6], lt[7], lt[8], lt[9],pos]
    // }