File: tcatl.rs
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 Single-file source-code for tcatl.
  27 
  28 To compile a smaller-sized command-line app, you can use the `rustc` command
  29 as follows:
  30 
  31 rustc -C opt-level=3 -C strip=symbols -C lto=true tcatl.rs
  32 */
  33 
  34 use std::env;
  35 use std::ffi::OsString;
  36 use std::fs::File;
  37 use std::io::{BufRead, BufReader, Error, Read, StdoutLock, Write, stdin, stdout};
  38 use std::process::exit;
  39 
  40 fn main() {
  41     // let dashes = env::args_os().skip(1).count_if(|e| e == "-");
  42     let mut dashes = 0;
  43     for arg in env::args_os().skip(1) {
  44         if arg == "-" {
  45             dashes += 1;
  46         }
  47     }
  48     let dashes = dashes;
  49 
  50     if dashes > 1 {
  51         let msg = "can't use standard input (dash) more than once";
  52         eprintln!("\x1b[31m{}\x1b[0m", msg);
  53         exit(1);
  54     }
  55 
  56     let w = stdout();
  57     let mut errors = 0;
  58 
  59     for arg in env::args_os().skip(1) {
  60         if arg == "-" {
  61             match catl(&mut w.lock(), stdin().lock(), arg) {
  62                 Ok(()) => continue,
  63                 Err(_) => return,
  64             }
  65         }
  66 
  67         let file = File::open(&arg);
  68         _ = match file {
  69             Ok(r) => catl(&mut w.lock(), r, arg),
  70 
  71             Err(e) => {
  72                 eprintln!("\x1b[31m{}\x1b[0m", e);
  73                 errors += 1;
  74                 Ok(())
  75             },
  76         };
  77     }
  78 
  79     if env::args_os().len() < 2 {
  80         _ = catl(&mut w.lock(), stdin().lock(), OsString::from("-"));
  81     }
  82 
  83     if errors > 0 {
  84         exit(1);
  85     }
  86 }
  87 
  88 fn catl(w: &mut StdoutLock, r: impl Read, path: OsString) -> Result<(), Error> {
  89     let br = BufReader::new(r);
  90 
  91     match path.clone().as_os_str().to_str() {
  92         Some(path) => {
  93             _ = w.write(b"\x1b[7m");
  94             _ = w.write(path.as_bytes());
  95             _ = w.write(b"\x1b[0m\n");
  96             _ = w.flush();
  97         },
  98 
  99         None => return Ok(()),
 100     }
 101 
 102     for (i, line) in br.lines().enumerate() {
 103         match line {
 104             Ok(l) => {
 105                 let l = l.as_bytes();
 106                 let skip = i == 0 && l.starts_with(b"\xef\xbb\xbf");
 107                 if skip {
 108                     _ = w.write(&l[3..])
 109                 } else {
 110                     _ = w.write(l)
 111                 }
 112 
 113                 _ = w.write(b"\n");
 114                 match w.flush() {
 115                     Ok(()) => (),
 116                     Err(e) => return Err(e),
 117                 };
 118             },
 119 
 120             Err(e) => return Err(e)
 121         }
 122     }
 123 
 124     Ok(())
 125 }