File: tcatl.go 1 /* 2 The MIT License (MIT) 3 4 Copyright (c) 2026 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 To compile a smaller-sized command-line app, you can use the `go` command as 27 follows: 28 29 go build -ldflags "-s -w" -trimpath tcatl.go 30 */ 31 32 package main 33 34 import ( 35 "bufio" 36 "errors" 37 "io" 38 "os" 39 ) 40 41 // Note: the code is avoiding using the fmt package to save hundreds of 42 // kilobytes on the resulting executable, which is a noticeable difference. 43 44 const info = ` 45 tcatl [options...] [file...] 46 47 48 Title and Concatenate lines emits lines from all the named sources given, 49 preceding each file's contents with its name, using an ANSI reverse style. 50 51 The name "-" stands for the standard input. When no names are given, the 52 standard input is used by default. 53 54 All (optional) leading options start with either single or double-dash: 55 56 -h show this help message 57 -help show this help message 58 ` 59 60 // errNoMoreOutput is a dummy error whose message is ignored, and which 61 // causes the app to quit immediately and successfully 62 var errNoMoreOutput = errors.New(`no more output`) 63 64 func main() { 65 args := os.Args[1:] 66 if len(args) > 0 { 67 switch args[0] { 68 case `-h`, `--h`, `-help`, `--help`: 69 os.Stdout.WriteString(info[1:]) 70 return 71 } 72 } 73 74 if len(args) > 0 && args[0] == `--` { 75 args = args[1:] 76 } 77 78 if err := run(os.Stdout, args); isActualError(err) { 79 os.Stderr.WriteString(err.Error()) 80 os.Stderr.WriteString("\n") 81 os.Exit(1) 82 } 83 } 84 85 func run(w io.Writer, args []string) error { 86 bw := bufio.NewWriter(w) 87 defer bw.Flush() 88 89 if len(args) == 0 { 90 return tcatl(bw, os.Stdin, `-`) 91 } 92 93 for _, name := range args { 94 if err := handleFile(bw, name); err != nil { 95 return err 96 } 97 } 98 return nil 99 } 100 101 func handleFile(w *bufio.Writer, name string) error { 102 if name == `` || name == `-` { 103 return tcatl(w, os.Stdin, `-`) 104 } 105 106 f, err := os.Open(name) 107 if err != nil { 108 return errors.New(`can't read from file named "` + name + `"`) 109 } 110 defer f.Close() 111 112 return tcatl(w, f, name) 113 } 114 115 // isActualError is to figure out whether not to ignore an error, and thus 116 // show it as an error message 117 func isActualError(err error) bool { 118 return err != nil && err != io.EOF && err != errNoMoreOutput 119 } 120 121 func tcatl(w *bufio.Writer, r io.Reader, name string) error { 122 const gb = 1024 * 1024 * 1024 123 sc := bufio.NewScanner(r) 124 sc.Buffer(nil, 8*gb) 125 126 w.WriteString("\x1b[7m") 127 w.WriteString(name) 128 w.WriteString("\x1b[0m\n") 129 if err := w.Flush(); err != nil { 130 // a write error may be the consequence of stdout being closed, 131 // perhaps by another app along a pipe 132 return errNoMoreOutput 133 } 134 135 for i := 0; sc.Scan(); i++ { 136 s := sc.Bytes() 137 if i == 0 && len(s) > 2 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf { 138 s = s[3:] 139 } 140 141 w.Write(s) 142 if w.WriteByte('\n') != nil { 143 return errNoMoreOutput 144 } 145 } 146 147 return sc.Err() 148 }