1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::io::{self, Write};

use termcolor::{ ColorChoice, ColorSpec, StandardStream, WriteColor };
pub use termcolor::Color;

fn write_bgcolor(out: &str, color: Color, ) -> io::Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    stdout.set_color(ColorSpec::new().set_bg(Some(color)))?;
    write!(&mut stdout, "{}", out)?;
    stdout.reset()?;
    Ok(())
}

fn write_color(out: &str, color: Color) -> io::Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    stdout.set_color(ColorSpec::new().set_fg(Some(color)))?;
    write!(&mut stdout, "{}", out)?;
    stdout.reset()?;
    Ok(())
}

pub struct Rainbow
{
    colors: Vec<Color>
}

impl Rainbow
{
    pub fn custom(colors: Vec<Color>) -> Rainbow
    {
        Rainbow
        {
            colors: colors
        }
    }

    pub fn default() -> Rainbow
    {
        Rainbow
        {
            colors: vec![
                Color::Red,
                Color::Rgb(255,127,0),
                Color::Rgb(255,255,0),
                Color::Rgb(0,255,0),
                Color::Rgb(0,100,255),
                Color::Rgb(0,0,255),
                Color::Rgb(128,0,255),
            ]
        }
    }

    pub fn write_bg(&self, text: &str) -> io::Result<()>
    {
        let mut minus = 0;
        for (index,ch) in text.chars().enumerate()
        {
            if index-minus > self.colors.len()-1
            {
                minus += self.colors.len();
            }
        
            write_bgcolor(&format!("{}",ch), self.colors[
                index-minus
            ])?;
        }
        Ok(())
    }

    pub fn write(&self, text: &str) -> io::Result<()>
    {
        let mut minus = 0;
        for (index,ch) in text.chars().enumerate()
        {
            if index-minus > self.colors.len()-1
            {
                minus += self.colors.len();
            }
        
            write_color(&format!("{}",ch), self.colors[
                index-minus
            ])?;
        }
        Ok(())
    }
}