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