mcat 0.4.2

a powerfull extended cat command, to cat all the things you couldn't before
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::{borrow::Cow, collections::HashMap, sync::OnceLock, usize};

use itertools::Itertools;
use rasteroid::term_misc;
use regex::Regex;
use strip_ansi_escapes::strip_str;
use syntect::{
    easy::HighlightLines,
    highlighting::Style,
    util::{LinesWithEndings, as_24_bit_terminal_escaped},
};
use unicode_width::UnicodeWidthStr;

use super::render::{AnsiContext, RESET};

static NEWLINE_REGEX: OnceLock<Regex> = OnceLock::new();
static ANSI_ESCAPE_REGEX: OnceLock<Regex> = OnceLock::new();
static TITLE_REGEX: OnceLock<Regex> = OnceLock::new();

pub fn get_lang_icon_and_color(lang: &str) -> Option<(&'static str, &'static str)> {
    let map: HashMap<&str, (&str, &str)> = [
        // code
        ("python", ("\u{e235}", "\x1b[38;5;214m")), // Python yellow-orange
        ("py", ("\u{e235}", "\x1b[38;5;214m")),
        ("rust", ("\u{e7a8}", "\x1b[38;5;166m")), // Rust orange
        ("rs", ("\u{e7a8}", "\x1b[38;5;166m")),
        ("javascript", ("\u{e74e}", "\x1b[38;5;227m")), // JS yellow
        ("js", ("\u{e74e}", "\x1b[38;5;227m")),
        ("typescript", ("\u{e628}", "\x1b[38;5;75m")), // TS blue
        ("ts", ("\u{e628}", "\x1b[38;5;75m")),
        ("go", ("\u{e627}", "\x1b[38;5;81m")), // Go cyan
        ("golang", ("\u{e627}", "\x1b[38;5;81m")),
        ("c", ("\u{e61e}", "\x1b[38;5;68m")),    // C blue
        ("cpp", ("\u{e61d}", "\x1b[38;5;204m")), // C++ pink-red
        ("c++", ("\u{e61d}", "\x1b[38;5;204m")),
        ("cc", ("\u{e61d}", "\x1b[38;5;204m")),
        ("cxx", ("\u{e61d}", "\x1b[38;5;204m")),
        ("java", ("\u{e738}", "\x1b[38;5;208m")), // Java orange
        ("csharp", ("\u{f81a}", "\x1b[38;5;129m")), // C# purple
        ("cs", ("\u{f81a}", "\x1b[38;5;129m")),
        ("ruby", ("\u{e21e}", "\x1b[38;5;196m")), // Ruby red
        ("rb", ("\u{e21e}", "\x1b[38;5;196m")),
        ("php", ("\u{e73d}", "\x1b[38;5;99m")), // PHP purple
        ("swift", ("\u{e755}", "\x1b[38;5;202m")), // Swift orange
        ("kotlin", ("\u{e634}", "\x1b[38;5;141m")), // Kotlin purple
        ("kt", ("\u{e634}", "\x1b[38;5;141m")),
        ("dart", ("\u{e798}", "\x1b[38;5;39m")), // Dart blue
        ("lua", ("\u{e620}", "\x1b[38;5;33m")),  // Lua blue
        ("sh", ("\u{ebca}", "\x1b[38;5;34m")),   // Shell green
        ("bash", ("\u{f489}", "\x1b[38;5;34m")),
        ("zsh", ("\u{f489}", "\x1b[38;5;34m")),
        ("fish", ("\u{f489}", "\x1b[38;5;34m")),
        ("html", ("\u{e736}", "\x1b[38;5;202m")), // HTML orange
        ("htm", ("\u{e736}", "\x1b[38;5;202m")),
        ("css", ("\u{e749}", "\x1b[38;5;75m")),   // CSS blue
        ("scss", ("\u{e749}", "\x1b[38;5;199m")), // SCSS pink
        ("sass", ("\u{e74b}", "\x1b[38;5;199m")), // Sass pink
        ("less", ("\u{e758}", "\x1b[38;5;54m")),  // Less purple
        ("jsx", ("\u{e7ba}", "\x1b[38;5;81m")),   // React cyan
        ("tsx", ("\u{e7ba}", "\x1b[38;5;81m")),
        ("vue", ("\u{fd42}", "\x1b[38;5;83m")),   // Vue green
        ("json", ("\u{e60b}", "\x1b[38;5;185m")), // JSON yellow
        ("yaml", ("\u{f0c5}", "\x1b[38;5;167m")), // YAML orange-red
        ("yml", ("\u{f0c5}", "\x1b[38;5;167m")),
        ("toml", ("\u{e6b2}", "\x1b[38;5;131m")),
        ("svg", ("\u{f0721}", "\x1b[38;5;178m")),
        ("xml", ("\u{e619}", "\x1b[38;5;172m")), // XML orange
        ("md", ("\u{f48a}", "\x1b[38;5;255m")),  // Markdown white
        ("markdown", ("\u{f48a}", "\x1b[38;5;255m")),
        ("rst", ("\u{f15c}", "\x1b[38;5;248m")), // reStructuredText gray
        ("tex", ("\u{e600}", "\x1b[38;5;25m")),  // LaTeX blue
        ("latex", ("\u{e600}", "\x1b[38;5;25m")),
        ("txt", ("\u{f15c}", "\x1b[38;5;248m")), // Text gray
        ("text", ("\u{f15c}", "\x1b[38;5;248m")),
        ("log", ("\u{f18d}", "\x1b[38;5;242m")), // Log dark gray
        ("ini", ("\u{f17a}", "\x1b[38;5;172m")), // INI orange
        ("conf", ("\u{f0ad}", "\x1b[38;5;172m")), // Config orange
        ("config", ("\u{f0ad}", "\x1b[38;5;172m")),
        ("env", ("\u{f462}", "\x1b[38;5;227m")), // Environment yellow
        ("dockerfile", ("\u{f308}", "\x1b[38;5;39m")), // Docker cyan
        ("docker", ("\u{f308}", "\x1b[38;5;39m")),
        ("asm", ("\u{f471}", "\x1b[38;5;124m")), // Assembly dark red
        ("s", ("\u{f471}", "\x1b[38;5;124m")),
        ("haskell", ("\u{e777}", "\x1b[38;5;99m")), // Haskell purple
        ("hs", ("\u{e777}", "\x1b[38;5;99m")),
        ("elm", ("\u{e62c}", "\x1b[38;5;33m")),     // Elm blue
        ("clojure", ("\u{e768}", "\x1b[38;5;34m")), // Clojure green
        ("clj", ("\u{e768}", "\x1b[38;5;34m")),
        ("scala", ("\u{e737}", "\x1b[38;5;196m")), // Scala red
        ("erlang", ("\u{e7b1}", "\x1b[38;5;125m")), // Erlang magenta
        ("erl", ("\u{e7b1}", "\x1b[38;5;125m")),
        ("elixir", ("\u{e62d}", "\x1b[38;5;99m")), // Elixir purple
        ("ex", ("\u{e62d}", "\x1b[38;5;99m")),
        ("exs", ("\u{e62d}", "\x1b[38;5;99m")),
        ("perl", ("\u{e769}", "\x1b[38;5;33m")), // Perl blue
        ("pl", ("\u{e769}", "\x1b[38;5;33m")),
        ("r", ("\u{f25d}", "\x1b[38;5;33m")),       // R blue
        ("matlab", ("\u{f799}", "\x1b[38;5;202m")), // MATLAB orange
        ("m", ("\u{f799}", "\x1b[38;5;202m")),
        ("octave", ("\u{f799}", "\x1b[38;5;202m")), // Octave orange
        ("zig", ("\u{e6a9}", "\x1b[38;5;214m")),
        ("h", ("\u{e61e}", "\x1b[38;5;110m")),
        ("lock", ("\u{f023}", "\x1b[38;5;244m")),
        // images
        ("png", ("\u{f1c5}", "\x1b[38;5;117m")),
        ("jpg", ("\u{f1c5}", "\x1b[38;5;110m")),
        ("jpeg", ("\u{f1c5}", "\x1b[38;5;110m")),
        ("gif", ("\u{f1c5}", "\x1b[38;5;213m")),
        ("bmp", ("\u{f1c5}", "\x1b[38;5;103m")),
        ("webp", ("\u{f1c5}", "\x1b[38;5;149m")),
        ("tiff", ("\u{f1c5}", "\x1b[38;5;144m")),
        ("ico", ("\u{f1c5}", "\x1b[38;5;221m")),
        // videos
        ("mp4", ("\u{f03d}", "\x1b[38;5;203m")),
        ("mkv", ("\u{f03d}", "\x1b[38;5;132m")),
        ("webm", ("\u{f03d}", "\x1b[38;5;111m")),
        ("mov", ("\u{f03d}", "\x1b[38;5;173m")),
        ("avi", ("\u{f03d}", "\x1b[38;5;167m")),
        ("flv", ("\u{f03d}", "\x1b[38;5;131m")),
        // audio
        ("mp3", ("\u{f001}", "\x1b[38;5;215m")),
        ("ogg", ("\u{f001}", "\x1b[38;5;109m")),
        ("flac", ("\u{f001}", "\x1b[38;5;113m")),
        ("wav", ("\u{f001}", "\x1b[38;5;123m")),
        ("m4a", ("\u{f001}", "\x1b[38;5;174m")),
        // archive
        ("zip", ("\u{f410}", "\x1b[38;5;180m")),
        ("tar", ("\u{f410}", "\x1b[38;5;180m")),
        ("gz", ("\u{f410}", "\x1b[38;5;180m")),
        ("rar", ("\u{f410}", "\x1b[38;5;180m")),
        ("7z", ("\u{f410}", "\x1b[38;5;180m")),
        ("xz", ("\u{f410}", "\x1b[38;5;180m")),
        // documents
        ("pdf", ("\u{f1c1}", "\x1b[38;5;196m")),
        ("doc", ("\u{f1c2}", "\x1b[38;5;33m")),
        ("docx", ("\u{f1c2}", "\x1b[38;5;33m")),
        ("xls", ("\u{f1c3}", "\x1b[38;5;70m")),
        ("xlsx", ("\u{f1c3}", "\x1b[38;5;70m")),
        ("ppt", ("\u{f1c4}", "\x1b[38;5;166m")),
        ("pptx", ("\u{f1c4}", "\x1b[38;5;166m")),
        ("odt", ("\u{f1c2}", "\x1b[38;5;33m")),
        ("epub", ("\u{f02d}", "\x1b[38;5;135m")),
        ("csv", ("\u{f1c3}", "\x1b[38;5;190m")),
        // fonts
        ("ttf", ("\u{f031}", "\x1b[38;5;98m")),
        ("otf", ("\u{f031}", "\x1b[38;5;98m")),
        ("woff", ("\u{f031}", "\x1b[38;5;98m")),
        ("woff2", ("\u{f031}", "\x1b[38;5;98m")),
    ]
    .into();

    map.get(lang.to_lowercase().as_str()).copied()
}

pub fn trim_ansi_string(mut str: String) -> String {
    let stripped = strip_str(&str);
    let mut leading = stripped.chars().take_while(|c| c.is_whitespace()).count();
    let mut trailing = stripped
        .chars()
        .rev()
        .take_while(|c| c.is_whitespace())
        .count();

    if leading == 0 && trailing == 0 {
        return str;
    }

    // Remove first N spaces
    str.retain(|c| {
        if c == ' ' && leading > 0 {
            leading -= 1;
            false
        } else {
            true
        }
    });

    // Remove last N spaces
    let mut i = str.len();
    while i > 0 && trailing > 0 {
        i -= 1;
        if str.as_bytes()[i] == b' ' {
            str.remove(i);
            trailing -= 1;
        }
    }

    str
}

pub fn string_len(str: &str) -> usize {
    strip_ansi_escapes::strip_str(&str).width()
}

fn find_last_fg_color_sequence(text: &str) -> Option<String> {
    let re = ANSI_ESCAPE_REGEX.get_or_init(|| Regex::new(r"\x1b\[[0-9;]*m").unwrap());
    let mut last_fg_color = None;

    for m in re.find_iter(text) {
        let seq = m.as_str();
        let codes_str = &seq[2..seq.len() - 1];

        if codes_str.is_empty() || codes_str == "0" {
            last_fg_color = None;
        } else {
            for code in codes_str.split(';') {
                if let Ok(num) = code.parse::<u32>() {
                    if (30..=37).contains(&num) || (90..=97).contains(&num) || num == 38 {
                        last_fg_color = Some(seq.to_string());
                        break;
                    }
                }
            }
        }
    }

    last_fg_color
}

pub fn wrap_char_based(
    original: &str,
    char: char,
    indent: usize,
    prefix: &str,
    sub_prefix: &str,
) -> String {
    let (space, sub_space, indent, sub_indent) = info_for_wrapping(indent, prefix, sub_prefix);
    let suffix = if original.ends_with("\n") { "\n" } else { "" };

    original
        .lines()
        .map(|line| {
            let char_index = line.rfind(char).map(|v| v + char.len_utf8()).unwrap_or(0);
            let str_to_char = line.get(..char_index).unwrap_or("");
            let line = format!("{indent}{line}");
            let sub_prefix = format!("{sub_indent}{str_to_char} ");
            let sub_space = sub_space.saturating_sub(string_len(&sub_prefix));
            wrap_highlighted_line(line, space, sub_space, &sub_prefix, false)
                .trim_matches('\n')
                .to_owned()
        })
        .join("\n")
        + suffix
}

fn info_for_wrapping(
    indent: usize,
    prefix: &str,
    sub_prefix: &str,
) -> (usize, usize, String, String) {
    let space = (term_misc::get_wininfo().sc_width as usize).saturating_sub(indent * 2);
    let sub_space = space.saturating_sub(string_len(sub_prefix));
    let space = space.saturating_sub(string_len(prefix));

    let indent = " ".repeat(indent);
    let sub_indent = format!("{indent}{sub_prefix}");
    let indent = format!("{indent}{prefix}");

    (space, sub_space, indent, sub_indent)
}

/// for braindead indenting any element.
pub fn wrap_lines(
    original: &str,
    multi_line: bool,
    indent: usize,
    prefix: &str,
    sub_prefix: &str,
) -> String {
    let (space, sub_space, indent, sub_indent) = info_for_wrapping(indent, prefix, sub_prefix);
    let suffix = if original.ends_with("\n") { "\n" } else { "" };

    if multi_line {
        original
            .lines()
            .map(|line| {
                let line = format!("{indent}{line}");
                wrap_highlighted_line(line, space, sub_space, &sub_indent, false)
                    .trim_matches('\n')
                    .to_owned()
            })
            .join("\n")
            + suffix
    } else {
        let line = format!("{indent}{original}");
        wrap_highlighted_line(line, space, sub_space, &indent, false)
    }
}

fn wrap_with_sub(original: String, first_width: usize, sub_width: usize) -> Vec<String> {
    let lines: Vec<String> = textwrap::wrap(&original, first_width)
        .into_iter()
        .map(|cow| cow.into_owned())
        .collect();

    let first_line = match lines.first() {
        Some(v) => v.clone(),
        None => return vec![original],
    };
    let sub_lines = lines.into_iter().skip(1).join(" ");

    let sub_width = sub_width;
    let lines: Vec<String> = textwrap::wrap(&sub_lines, sub_width)
        .into_iter()
        .map(|cow| cow.into_owned())
        .collect();

    let mut res = vec![first_line];
    res.extend_from_slice(&lines);

    res
}

/// first_width: the space for text on the first line.
/// sub_width:   the space for left for sub lines. doesn't factor in sub_prefix width, calc yourself.
/// auto_indent: add the firstline indent to the sub lines.
pub fn wrap_highlighted_line(
    original: String,
    first_width: usize,
    sub_width: usize,
    sub_prefix: &str,
    auto_indent: bool,
) -> String {
    if string_len(&original) <= first_width {
        return original;
    }

    let suffix = if original.ends_with("\n") { "\n" } else { "" };

    // wrap lines
    let pre_padding = if auto_indent {
        strip_str(&original)
            .find(|c: char| !c.is_whitespace())
            .unwrap_or(0)
    } else {
        0
    };
    let lines = wrap_with_sub(original, first_width, sub_width.saturating_sub(pre_padding));

    let padding = " ".repeat(pre_padding);
    let mut buf = String::new();
    let mut pre_last_color = "".to_owned();

    // add prefix and lost colors
    for (i, line) in lines.iter().enumerate() {
        if i == 0 || line.trim().is_empty() {
            buf.push_str(line);
        } else {
            buf.push_str(&format!("\n{sub_prefix}{padding}{pre_last_color}{line}"));
        }
        match find_last_fg_color_sequence(line) {
            Some(color) => pre_last_color = color,
            None => {}
        }
    }
    buf.push_str(suffix);

    buf
}

pub fn format_code_simple<'a>(code: &str, lang: &str, ctx: &AnsiContext, indent: usize) -> String {
    let header = match get_lang_icon_and_color(lang) {
        Some((icon, color)) => &format!("{color}{icon} {lang}{RESET}",),
        None => lang,
    };

    let ts = ctx.theme.to_syntect_theme();
    let syntax = ctx
        .ps
        .find_syntax_by_token(lang)
        .unwrap_or_else(|| ctx.ps.find_syntax_plain_text());
    let mut highlighter = HighlightLines::new(syntax, &ts);

    let line_count = code.lines().count().saturating_sub(1);
    let content = LinesWithEndings::from(code)
        .enumerate()
        .filter_map(|(i, line)| {
            if line_count == i && line.trim().is_empty() {
                return None;
            }
            let ranges: Vec<(Style, &str)> = highlighter.highlight_line(line, &ctx.ps).unwrap();
            let highlighted = as_24_bit_terminal_escaped(&ranges[..], false);
            Some(format!("  {}", highlighted.trim_matches('\n')))
        })
        .join("\n");

    let sub_indent = 4usize;
    let sub_indent = " ".repeat(sub_indent.saturating_sub(indent));
    let (space, sub_space, indent, sub_indent) = info_for_wrapping(indent, "", &sub_indent);
    let content = content
        .lines()
        .map(|line| {
            let line = format!("{indent}{line}");
            wrap_highlighted_line(line, space, sub_space, &sub_indent, true)
                .trim_matches('\n')
                .to_owned()
        })
        .join("\n");

    format!("\n\n{indent}{header}\n{content}\n\n")
}

pub fn format_code_full<'a>(code: &str, lang: &str, ctx: &AnsiContext) -> String {
    let ts = ctx.theme.to_syntect_theme();
    let syntax = ctx
        .ps
        .find_syntax_by_token(lang)
        .unwrap_or_else(|| ctx.ps.find_syntax_plain_text());
    let mut highlighter = HighlightLines::new(syntax, &ts);

    let header = match get_lang_icon_and_color(lang) {
        Some((icon, color)) => &format!("{color}{icon} {lang}",),
        None => lang,
    };

    let max_lines = code.lines().count();
    let num_width = max_lines.to_string().chars().count() + 2;
    // -1 because the indent is 1 based
    let term_width = term_misc::get_wininfo().sc_width;
    let text_size = (term_width as usize)
        .saturating_sub(num_width)
        .saturating_sub(3); // -2 for spacing both ways, -1 for the | char after line num
    let color = ctx.theme.border.fg.clone();
    let mut buffer = String::new();

    let after_num_width = (term_width as usize)
        .saturating_sub(num_width)
        .saturating_sub(1); // because the connected char ┬
    let top_header = format!(
        "{color}{}{}{RESET}",
        "".repeat(num_width),
        "".repeat(after_num_width)
    );
    let middle_header = format!("{color}{}{header}{RESET}", " ".repeat(num_width),);
    let bottom_header = format!(
        "{color}{}{}{RESET}",
        "".repeat(num_width),
        "".repeat(after_num_width)
    );
    buffer.push_str(&format!("{top_header}\n{middle_header}\n{bottom_header}\n"));

    let mut num = 1;
    let prefix = format!("{}{color}{RESET}     ", " ".repeat(num_width));
    let sub_text_size = text_size.saturating_sub(4); // 4 extra space for visual indent.
    for line in LinesWithEndings::from(code) {
        let left_space = num_width - num.to_string().chars().count();
        let left_offset = left_space / 2;
        let right_offset = left_space - left_offset;
        let ranges: Vec<(Style, &str)> = highlighter.highlight_line(line, &ctx.ps).unwrap();
        let highlighted = as_24_bit_terminal_escaped(&ranges[..], false);
        let highlighted =
            wrap_highlighted_line(highlighted, text_size, sub_text_size, &prefix, true);
        buffer.push_str(&format!(
            "{color}{}{num}{}{RESET}{}",
            " ".repeat(left_offset),
            " ".repeat(right_offset),
            highlighted
        ));
        num += 1;
    }

    let last_border = format!(
        "{color}{}{}{RESET}",
        "".repeat(num_width),
        "".repeat(term_width as usize - num_width - 1)
    );
    buffer.push_str(&last_border);
    format!("\n\n{buffer}\n\n")
}

pub fn format_tb(ctx: &AnsiContext, offset: usize) -> String {
    let w = term_misc::get_wininfo().sc_width as usize;
    let br = "".repeat(w.saturating_sub(offset.saturating_sub(1)));
    let border = &ctx.theme.guide.fg;
    format!("{border}{br}{RESET}")
}

pub fn limit_newlines<'a>(original: &'a str) -> Cow<'a, str> {
    let re = NEWLINE_REGEX.get_or_init(|| Regex::new(r"\n([ \t]*\n){2,}").unwrap());
    re.replace_all(&original, "\n\n")
}

pub fn get_title_box<'a>(literal: &'a str) -> Option<&'a str> {
    let re = TITLE_REGEX.get_or_init(|| Regex::new(r#"<!--\s*S-TITLE:\s*(.*?)\s*-->"#).unwrap());

    let caps = re.captures(literal)?;
    caps.get(1).map(|v| v.as_str())
}