mcat 0.3.5

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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
mod catter;
mod cdp;
mod concater;
mod config;
mod converter;
mod fetch_manager;
mod image_viewer;
mod inspector;
mod markdown;
mod prompter;
mod scrapy;

use clap::{
    Arg, ColorChoice, Command,
    builder::{Styles, styling::AnsiColor},
};
use clap_complete::{Generator, Shell, generate};
use config::McatConfig;
use crossterm::tty::IsTty;
use dirs::home_dir;
use rasteroid::term_misc;
use std::{
    io::{BufWriter, Read, Write},
    path::Path,
};

fn print_completions<G: Generator>(gene: G, cmd: &mut Command) {
    generate(
        gene,
        cmd,
        cmd.get_name().to_string(),
        &mut std::io::stdout(),
    );
}

fn build_core_args() -> Vec<Arg> {
    vec![
        Arg::new("output")
            .long("output")
            .short('o')
            .help("Output format")
            .value_parser(["html", "md", "image", "video", "inline", "interactive"]),
        Arg::new("theme")
            .long("theme")
            .short('t')
            .help("Color theme [default: makurai_mage]")
            .value_parser([
                "catppuccin",
                "nord",
                "monokai",
                "dracula",
                "gruvbox",
                "one_dark",
                "solarized",
                "tokyo_night",
                "makurai_healer",
                "makurai_mage",
                "ayu",
                "ayu_mirage",
                "github",
                "synthwave",
                "material",
                "rose_pine",
                "kanagawa",
                "vscode",
                "everforest",
            ]),
    ]
}

fn build_markdown_viewer_args() -> Vec<Arg> {
    vec![
        Arg::new("no-linenumbers")
            .long("no-linenumbers")
            .help("Disable line numbers in code blocks")
            .action(clap::ArgAction::SetTrue),
        Arg::new("color")
            .long("color")
            .help("Control ANSI formatting [default: auto]")
            .value_parser(["never", "always", "auto"]),
        Arg::new("color-never")
            .short('C')
            .help("Shortcut for --color never")
            .action(clap::ArgAction::SetTrue),
        Arg::new("color-always")
            .short('c')
            .help("Shortcut for --color always")
            .action(clap::ArgAction::SetTrue),
        Arg::new("pager")
            .long("pager")
            .help("Modify the default pager [default: 'less -r']"),
        Arg::new("paging")
            .long("paging")
            .help("Control paging behavior [default: auto]")
            .value_parser(["never", "always", "auto"]),
        Arg::new("paging-never")
            .short('P')
            .help("Shortcut for --paging never")
            .action(clap::ArgAction::SetTrue),
        Arg::new("paging-always")
            .short('p')
            .help("Shortcut for --paging always")
            .action(clap::ArgAction::SetTrue),
    ]
}
fn build_image_viewer_args() -> Vec<Arg> {
    vec![
        Arg::new("inline")
            .short('i')
            .help("Shortcut for --output inline")
            .action(clap::ArgAction::SetTrue),
        Arg::new("style-html")
            .long("style-html")
            .short('s')
            .help("Add style to HTML output (when HTML is the output)")
            .action(clap::ArgAction::SetTrue),
        Arg::new("report")
            .long("report")
            .action(clap::ArgAction::SetTrue)
            .help("Reports image/video dimensions and additional info"),
        Arg::new("silent")
            .long("silent")
            .action(clap::ArgAction::SetTrue)
            .help("Removes loading bars"),
        Arg::new("kitty")
            .long("kitty")
            .help("Use Kitty image protocol")
            .action(clap::ArgAction::SetTrue),
        Arg::new("iterm")
            .long("iterm")
            .help("Use iTerm2 image protocol")
            .action(clap::ArgAction::SetTrue),
        Arg::new("sixel")
            .long("sixel")
            .help("Use Sixel image protocol")
            .action(clap::ArgAction::SetTrue),
        Arg::new("ascii")
            .long("ascii")
            .help("Use ASCII art output")
            .action(clap::ArgAction::SetTrue),
        Arg::new("horizontal")
            .long("hori")
            .action(clap::ArgAction::SetTrue)
            .help("Concatenate images horizontally"),
        Arg::new("delete-all-images")
            .long("delete-images")
            .help("Delete all images (Kitty only)")
            .action(clap::ArgAction::SetTrue),
        Arg::new("inline-options").long("opts").help(
            "Options for --output inline:\n\
                     *  center=<bool>\n\
                     *  inline=<bool>\n\
                     *  width=<string>\n\
                     *  height=<string>\n\
                     *  scale=<f32>\n\
                     *  spx=<string>\n\
                     *  sc=<string>\n\
                     *  zoom=<usize>\n\
                     *  x=<int>\n\
                     *  y=<int>\n\
                     Example: --opts 'center=false,inline=true,width=80%,height=20c,scale=0.5,spx=1920x1080,sc=100x20xforce,zoom=2,x=16,y=8'",
        ),
    ]
}
fn build_fetcher_args() -> Vec<Arg> {
    vec![
        Arg::new("generate-completions")
            .long("generate")
            .help("Generate shell completions")
            .value_parser(["bash", "zsh", "fish", "powershell"]),
        Arg::new("fetch-chromium")
            .long("fetch-chromium")
            .help("Download and prepare chromium")
            .action(clap::ArgAction::SetTrue),
        Arg::new("fetch-ffmpeg")
            .long("fetch-ffmpeg")
            .help("Download and prepare ffmpeg")
            .action(clap::ArgAction::SetTrue),
        Arg::new("fetch-clean")
            .long("fetch-clean")
            .help("Clean up local binaries")
            .action(clap::ArgAction::SetTrue),
    ]
}
fn build_ls_args() -> Vec<Arg> {
    vec![
        Arg::new("hidden")
            .long("hidden")
            .short('a')
            .help("Include hidden files")
            .action(clap::ArgAction::SetTrue),
        Arg::new("ls-options").long("ls-opts").help(
            "Options for directory listings:\n\
                 *  x_padding=<string>\n\
                 *  y_padding=<string>\n\
                 *  min_width=<string>\n\
                 *  max_width=<string>\n\
                 *  height=<string>\n\
                 *  items_per_row=<usize>\n\
                 Example: --ls-opts 'x_padding=4c,y_padding=2c,min_width=4c,max_width=16c,height=8%,items_per_row=12'",
        ),
    ]
}
fn build_input_arg(stdin_streamed: bool) -> Arg {
    let mut input_arg = Arg::new("input")
        .index(1)
        .num_args(1..)
        .help("Input source (file/dir/url/ls)");

    if !stdin_streamed {
        input_arg = input_arg.required_unless_present_any([
            "fetch-clean",
            "fetch-chromium",
            "fetch-ffmpeg",
            "report",
            "generate-completions",
            "delete-all-images",
        ]);
    }
    input_arg
}

fn build_cli(stdin_streamed: bool) -> Command {
    Command::new("mcat")
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about("A powerful extended cat command - cat all the things you couldn't before")
        .color(ColorChoice::Always)
        .styles(
            Styles::styled()
                .header(AnsiColor::Green.on_default().bold())
                .literal(AnsiColor::Blue.on_default()),
        )
        // Core arguments and input
        .arg(build_input_arg(stdin_streamed))
        .next_help_heading("Core Options")
        .args(build_core_args())
        // Markdown viewing options
        .next_help_heading("Markdown Viewing")
        .args(build_markdown_viewer_args())
        // Image/Media viewing options
        .next_help_heading("Image/Video Viewing")
        .args(build_image_viewer_args())
        // Directory listing options
        .next_help_heading("Directory Listing")
        .args(build_ls_args())
        // System operations
        .next_help_heading("System Operations")
        .args(build_fetcher_args())
}

fn main() {
    let stdin_streamed = !std::io::stdin().is_tty();
    let stdout = std::io::stdout().lock();
    let mut out = BufWriter::new(stdout);
    let opts = build_cli(stdin_streamed).get_matches();

    let mut config = McatConfig::default();
    config.extend_from_env();
    config.extend_from_args(&opts);

    // setting the winsize
    let _ = term_misc::init_wininfo(
        &term_misc::break_size_string(config.inline_options.spx).unwrap_or_exit(),
        &term_misc::break_size_string(config.inline_options.sc).unwrap_or_exit(),
        config.inline_options.scale,
        config.is_tmux,
        config.inline_options.inline,
    );

    // fn and leave
    if let Some(fn_and_leave) = config.fn_and_leave {
        match fn_and_leave {
            config::FnAndLeave::ShellGenerate(shell) => {
                let mut cmd = build_cli(stdin_streamed);
                match shell.as_str() {
                    "bash" => print_completions(Shell::Bash, &mut cmd),
                    "zsh" => print_completions(Shell::Zsh, &mut cmd),
                    "fish" => print_completions(Shell::Fish, &mut cmd),
                    "powershell" => print_completions(Shell::PowerShell, &mut cmd),
                    _ => unreachable!(),
                }
            }
            config::FnAndLeave::DeleteImages => {
                rasteroid::kitty_encoder::delete_all_images(&mut out).unwrap_or_exit()
            }
            config::FnAndLeave::FetchChromium => fetch_manager::fetch_chromium().unwrap_or_exit(),
            config::FnAndLeave::FetchFfmpeg => fetch_manager::fetch_ffmpeg().unwrap_or_exit(),
            config::FnAndLeave::FetchClean => fetch_manager::clean().unwrap_or_exit(),
            config::FnAndLeave::Report => report_full(),
        };
        return;
    };

    // if ls
    if config.is_ls {
        let d = ".".to_string();
        let input = config.input.get(1).unwrap_or(&d);
        if config.is_tmux {
            rasteroid::set_tmux_passthrough(true);
        }
        converter::lsix(
            input,
            &mut out,
            &config.ls_options,
            config.hidden,
            &config.inline_encoder,
        )
        .unwrap_or_exit();
        return;
    }

    // gathering all the inputs
    let mut tmp_files = Vec::new(); //for lifetime
    let mut path_bufs = Vec::new();
    // if stdin is streamed into
    if stdin_streamed {
        let mut buffer = Vec::new();
        std::io::stdin().read_to_end(&mut buffer).unwrap_or_exit();

        let inter = inspector::InspectedBytes::from_bytes(&buffer).unwrap_or_exit();
        match inter {
            inspector::InspectedBytes::File(named_temp_file) => {
                let path = named_temp_file.path().to_path_buf();
                path_bufs.push((path, Some("stdin input".to_string())));
                tmp_files.push(named_temp_file);
            }
            inspector::InspectedBytes::Path(path_buf) => path_bufs.push((path_buf, None)),
        };
    }
    for i in config.input.iter() {
        if i.starts_with("https://") {
            if let Ok(tmp) = scrapy::scrape_biggest_media(&i, config.silent) {
                let path = tmp.path().to_path_buf();
                tmp_files.push(tmp);
                path_bufs.push((path, Some(i.clone())));
            } else {
                eprintln!("{} didn't contain any supported media", i);
            }
        } else {
            let i = expand_tilde(&i);
            let path = Path::new(&i);
            if !path.exists() {
                eprintln!("{} doesn't exists", path.display());
                std::process::exit(1);
            }
            if path.is_dir() {
                let mut selected_files =
                    prompter::prompt_for_files(path, config.hidden).unwrap_or_exit();
                selected_files.sort();
                path_bufs.extend_from_slice(&selected_files);
            } else {
                path_bufs.push((path.to_path_buf(), None));
            }
        }
    }

    // concating and printing the result
    let main_format = concater::check_unified_format(&path_bufs);
    match main_format {
        "text" => {
            if path_bufs.len() == 1 {
                catter::cat(&path_bufs[0].0, &mut out, &config).unwrap_or_exit();
            } else {
                let path_bufs = concater::assign_names(&path_bufs);
                let tmp = concater::concat_text(path_bufs);
                catter::cat(tmp.path(), &mut out, &config).unwrap_or_exit();
            }
        }
        "video" => {
            match config.inline_encoder {
                rasteroid::InlineEncoder::Ascii | rasteroid::InlineEncoder::Sixel => {}
                _ => {
                    if config.is_tmux {
                        rasteroid::set_tmux_passthrough(true);
                    }
                }
            }
            if path_bufs.len() == 1 {
                catter::cat(&path_bufs[0].0, &mut out, &config).unwrap_or_exit();
            } else {
                #[allow(unused_variables)]
                let (dir, path) = concater::concat_video(&path_bufs).unwrap_or_exit();
                catter::cat(&path, &mut out, &config).unwrap_or_exit();
            }
        }
        "image" => {
            match config.inline_encoder {
                rasteroid::InlineEncoder::Ascii => {}
                _ => {
                    if config.is_tmux {
                        rasteroid::set_tmux_passthrough(true);
                    }
                }
            }
            if path_bufs.len() == 1 {
                catter::cat(&path_bufs[0].0, &mut out, &config).unwrap_or_exit();
            } else {
                let img = concater::concat_images(path_bufs, config.horizontal_image_stacking)
                    .unwrap_or_exit();
                catter::cat(img.path(), &mut out, &config).unwrap_or_exit();
            }
        }
        _ => {}
    }
    out.flush().unwrap();
}

trait UnwrapOrExit<T> {
    fn unwrap_or_exit(self) -> T;
}

impl<T, E: std::fmt::Display> UnwrapOrExit<T> for Result<T, E> {
    fn unwrap_or_exit(self) -> T {
        match self {
            Ok(value) => value,
            Err(err) => {
                eprintln!("{}", err);
                std::process::exit(1);
            }
        }
    }
}

fn expand_tilde(path: &str) -> String {
    if path.starts_with("~") {
        if let Some(home) = home_dir() {
            return path.replace("~", &home.to_string_lossy().into_owned());
        }
    }
    path.to_string()
}

fn report_full() {
    let is_chromium_installed = fetch_manager::is_chromium_installed();
    let is_ffmpeg_installed = fetch_manager::is_ffmpeg_installed();
    let mut env = term_misc::EnvIdentifiers::new();
    let kitty = rasteroid::kitty_encoder::is_kitty_capable(&mut env);
    let iterm = rasteroid::iterm_encoder::is_iterm_capable(&mut env);
    let sixel = rasteroid::sixel_encoder::is_sixel_capable(&mut env);
    let ascii = true; //not sure what doesn't support it
    let winsize = term_misc::get_wininfo();
    let tmux = winsize.is_tmux;
    let inline = winsize.needs_inline;
    let os = env.data.get("OS").map(|f| f.as_str()).unwrap_or("Unknown");
    let term = if tmux {
        env.data
            .get("TMUX_ORIGINAL_TERM")
            .map(|f| f.as_str())
            .unwrap_or("Unknonwn")
    } else {
        env.data
            .get("TERM")
            .map(|f| f.as_str())
            .unwrap_or("Unknonwn")
    };
    let tmux_program = if tmux {
        env.data
            .get("TMUX_ORIGINAL_SPEC")
            .map(|f| f.as_str())
            .unwrap_or("Unknown")
    } else {
        env.data
            .get("TERM_PROGRAM")
            .map(|f| f.as_str())
            .unwrap_or("Unknown")
    };
    let ver = env!("CARGO_PKG_VERSION");

    // Print header with fancy box
    println!("┌────────────────────────────────────────────────────┐");
    println!("│               SYSTEM CAPABILITIES                  │");
    println!("├────────────────────────────────────────────────────┤");

    // Color function helpers
    fn green(text: &str) -> String {
        format!("\x1b[32m{}\x1b[0m", text)
    }

    fn red(text: &str) -> String {
        format!("\x1b[31m{}\x1b[0m", text)
    }

    fn format_status(status: bool) -> String {
        if status {
            green("✓ INSTALLED")
        } else {
            red("× MISSING")
        }
    }
    fn format_capability(status: bool) -> String {
        if status {
            green("✓ SUPPORTED")
        } else {
            red("× UNSUPPORTED")
        }
    }
    fn format_info(status: bool) -> String {
        if status {
            green("✓ YES")
        } else {
            red("× NO")
        }
    }

    // Print required dependencies
    println!("│ Dependencies:                                      │");
    println!(
        "│   Chromium: {:<47} │",
        format_status(is_chromium_installed)
    );
    println!("│   FFmpeg:   {:<47} │", format_status(is_ffmpeg_installed));

    // Print terminal capabilities
    println!("├────────────────────────────────────────────────────┤");
    println!("│ Terminal Graphics Support:                         │");
    println!("│   Kitty:    {:<47} │", format_capability(kitty));
    println!("│   iTerm2:   {:<47} │", format_capability(iterm));
    println!("│   Sixel:    {:<47} │", format_capability(sixel));
    println!("│   ASCII:    {:<47} │", format_capability(ascii));

    // Print terminal dimensions
    println!("├────────────────────────────────────────────────────┤");
    println!("│ Terminal Info:                                     │");
    println!("│   Width:          {:<32} │", winsize.sc_width);
    println!("│   Height:         {:<32} │", winsize.sc_height);
    println!("│   Pixel Width:    {:<32} │", winsize.spx_width);
    println!("│   Pixel Height:   {:<32} │", winsize.spx_height);

    // Others
    println!("├────────────────────────────────────────────────────┤");
    println!("│ Others:                                            │");
    println!("│   Tmux:       {:<45} │", format_info(tmux));
    println!("│   Inline:     {:<45} │", format_info(inline));
    println!("│   OS:         {:<36} │", os);
    println!("│   TERM:       {:<36} │", term);
    println!("│   TERMTYPE:   {:<36} │", tmux_program);
    println!("│   Version:    {:<36} │", ver);

    // Print footer
    println!("└────────────────────────────────────────────────────┘");
}