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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
mod colors;
mod emojis;
use regex::Regex;
use regex::escape;
use std::io::{self, Write};


pub fn print<T: AsRef<[U]>, U: std::fmt::Debug + std::fmt::Display>(data: T) {
    let slice: &[U] = data.as_ref();
    
    for item in slice {
        // Process the elements here
        print!("{}", item);
        io::stdout().flush().unwrap();
    }

}

// Function to replace emoji shortcodes in a given text
fn replace_emoji_shortcodes(text: &str) -> String {
    let emoji_map = emojis::emoji_shortcodes();
    let mut replaced_text = String::from(text);

    for (shortcode, emoji) in emoji_map.iter() {
        let regex_str = format!(":{}:", escape(shortcode));
        let regex = Regex::new(&regex_str).unwrap();

        replaced_text = regex.replace_all(&replaced_text, *emoji).to_string();
    }

    replaced_text
}


// fg:100 bg:200 b:0 eff:i
pub fn text(usertext: &str, format: &str) -> String{

    // basic checks
    if  format.is_empty(){
        let retstr = replace_emoji_shortcodes(usertext);
        return retstr;
    }

    
    let escape = "\x1b";
    let end_seq = "\x1b[0m";
    let mut all_effects = String::from("");
    let ansi_codes = colors::ansi_color_codes();
    

    let parts: Vec<&str> = format.split_whitespace().collect();
    for part in parts{
        let part_all: Vec<&str> = part.split(':').collect(); // ex fg:red
        let command = part_all[0]; // ex fg
        let val = part_all[1]; // ex red

        if command == "fg"{ 
            if val.starts_with("rgb("){
                let trimmed_start = val.trim_start_matches("rgb(");
                let trimmed_end = trimmed_start.trim_end_matches(")");
                let rgb: Vec<&str> = trimmed_end.split(',').collect();
                let rgb_formatted = format!("{}[38;2;{};{};{}m", escape, rgb[0], rgb[1], rgb[2]);
                all_effects.push_str(&rgb_formatted);
            } else {
                let fg_code = ansi_codes.get(&val).expect("Cannot find color name").to_string();
                let fg_formatted = format!("{}[38;5;{}m", escape, fg_code);
                all_effects.push_str(&fg_formatted);
            }
        } else if command == "bg"{
            if val.starts_with("rgb("){
                let trimmed_start = val.trim_start_matches("rgb(");
                let trimmed_end = trimmed_start.trim_end_matches(')');
                let rgb: Vec<&str> = trimmed_end.split(',').collect();
                let rgb_formatted = format!("{}[48;2;{};{};{}m", escape, rgb[0], rgb[1], rgb[2]);
                all_effects.push_str(&rgb_formatted);
            } else {
                let bg_code = ansi_codes.get(&val).expect("Cannot find color name").to_string();
                let bg_formatted = format!("{}[48;5;{}m", escape, bg_code);
                all_effects.push_str(&bg_formatted);
            }
        } else if command == "eff"{
            let user_effects: Vec<&str> = val.split(',').collect();
            for effect in user_effects{
                match effect {
                    "i" => {
                        let e = format!("{}[3m", escape);
                        all_effects.push_str(&e);
                    },
                    "blink" => {
                        let e = format!("{}[5m", escape);
                        all_effects.push_str(&e);
                    },
                    "u" => {
                        let e = format!("{}[4m", escape);
                        all_effects.push_str(&e);
                    },
                    "b" => {
                        let e = format!("{}[1m", escape);
                        all_effects.push_str(&e);
                    },
                    "s" => {
                        let e = format!("{}[9m", escape);
                        all_effects.push_str(&e);
                    },
                    "d" => {
                        let e = format!("{}[2m", escape);
                        all_effects.push_str(&e);
                    },
                    "r" => {
                        let e = format!("{}[7m", escape);
                        all_effects.push_str(&e);
                    },
                    "hc" => {
                        let e = format!("{}[?25l", escape);
                        all_effects.push_str(&e);
                    },
                    "sc" => {
                        let e = format!("{}[?25h", escape);
                        all_effects.push_str(&e);
                    },
                    &_ => {
                        all_effects.push_str("");
                    }
                }
            }
        }
    }

    let usert = replace_emoji_shortcodes(usertext);
    // TODO: emoji
    let to_ret = format!("{}{}{}", all_effects, usert, end_seq);

    to_ret
}

use std::iter::Iterator;


// Custom Iterator struct
pub struct MyRange<'a>{
    start: i32,
    end: i32,
    description: &'a str,
    bar_width: i32,
    display: &'a str,
    elapsed_time_f: String
}

// Implement the Iterator trait for MyRange
impl Iterator for MyRange<'_>{
    type Item = i32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.start <= self.end {
            let current = self.start;
            self.start += 1;
            Some(current)
        } else {
            None
        }
    }
}

impl Drop for MyRange<'_>{
    fn drop(&mut self) {
        // Code to be executed after the last iteration.
        print([
            text("\r ", ""),
            text(self.description, ""),
            text(" ",""),
            text(&self.display.repeat(self.bar_width.try_into().unwrap()), "fg:green"),
            text(" 100% ", "fg:magenta"),
            text(&self.elapsed_time_f, "fg:orange1"),
            text("\n",""),
            ]);
    }
}

// Idk what i wrote here
fn create_range<'a>(start: i32, end: i32, description: &'a str, bar_width: i32, display: &'a str, elapsed_time_f: String) -> MyRange<'a> {
    MyRange { start, end, description, bar_width, display, elapsed_time_f }
}

use std::time::{Duration, Instant};

fn format_duration(duration: Duration) -> String {
    let hours = duration.as_secs() / 3600;
    let minutes = (duration.as_secs() % 3600) / 60;
    let seconds = duration.as_secs() % 60;

    format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
}

pub fn track(step: i32, description: &str) -> MyRange<'_>{
    // Downloading ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:21
    let len_itr = step;//range.start_bound() - range.end_bound();
    let bar_width = 40;
    let display = "━";
    let incr = bar_width/len_itr as i32;
    let mut step = 0;
    let start_time = Instant::now();
    for _item in 1..len_itr{
        let perc = (step as f32 /bar_width as f32) * 100.0;
        let elapsed_time = start_time.elapsed();
        let elapsed_time_f = format!("{} ", format_duration(elapsed_time));

        print([
            text("\r ", "eff:hc"),
            text(description, ""),
            text(" ",""),
            text(&display.repeat(step.try_into().unwrap()), "fg:deep_pink3"),
            text(&display.repeat((bar_width-step).try_into().unwrap()), "fg:black"),
            text(&format!(" {}% ", perc as i32), "fg:magenta"),
            text(&elapsed_time_f, "fg:steel_blue eff:sc")
            ]);


        step = step + incr;
    }

    
    let elapsed_time = start_time.elapsed();
    let elapsed_time_f = format_duration(elapsed_time);
    return create_range(1, len_itr, description, bar_width, display, elapsed_time_f);


}