File: plain.go 1 /* 2 The MIT License (MIT) 3 4 Copyright © 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 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 plain.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 plain [options...] [file...] 46 47 48 Turn potentially ANSI-styled plain-text into actual plain-text. 49 50 Input is assumed to be UTF-8, and all CRLF byte-pairs are turned into line 51 feeds. 52 53 All (optional) leading options start with either single or double-dash: 54 55 -h show this help message 56 -help show this help message 57 ` 58 59 // errNoMoreOutput is a dummy error whose message is ignored, and which 60 // causes the app to quit immediately and successfully 61 var errNoMoreOutput = errors.New(`no more output`) 62 63 func main() { 64 args := os.Args[1:] 65 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 case `--`: 73 args = args[1:] 74 } 75 } 76 77 liveLines := true 78 if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { 79 liveLines = false 80 } 81 82 if err := run(os.Stdout, args, liveLines); isActualError(err) { 83 os.Stderr.WriteString(err.Error()) 84 os.Stderr.WriteString("\n") 85 os.Exit(1) 86 } 87 } 88 89 func run(w io.Writer, args []string, live bool) error { 90 bw := bufio.NewWriter(w) 91 defer bw.Flush() 92 93 if len(args) == 0 { 94 return plain(bw, os.Stdin, live) 95 } 96 97 for _, name := range args { 98 if err := handleFile(bw, name, live); err != nil { 99 return err 100 } 101 } 102 return nil 103 } 104 105 func handleFile(w *bufio.Writer, name string, live bool) error { 106 if name == `` || name == `-` { 107 return plain(w, os.Stdin, live) 108 } 109 110 f, err := os.Open(name) 111 if err != nil { 112 return errors.New(`can't read from file named "` + name + `"`) 113 } 114 defer f.Close() 115 116 return plain(w, f, live) 117 } 118 119 // isActualError is to figure out whether not to ignore an error, and thus 120 // show it as an error message 121 func isActualError(err error) bool { 122 return err != nil && err != errNoMoreOutput 123 } 124 125 // indexEscapeSequence finds the first ANSI-style escape-sequence, which is 126 // the multi-byte sequences starting with ESC[; the result is a pair of slice 127 // indices which can be independently negative when either the start/end of 128 // a sequence isn't found; given their fairly-common use, even the hyperlink 129 // ESC]8 sequences are supported 130 func indexEscapeSequence(s []byte) (int, int) { 131 var prev byte 132 133 for i, b := range s { 134 if prev == '\x1b' && b == '[' { 135 j := indexLetter(s[i+1:]) 136 if j < 0 { 137 return i, -1 138 } 139 return i - 1, i + 1 + j + 1 140 } 141 142 if prev == '\x1b' && b == ']' && i+1 < len(s) && s[i+1] == '8' { 143 j := indexPair(s[i+1:], '\x1b', '\\') 144 if j < 0 { 145 return i, -1 146 } 147 return i - 1, i + 1 + j + 2 148 } 149 150 prev = b 151 } 152 153 return -1, -1 154 } 155 156 func indexLetter(s []byte) int { 157 for i, b := range s { 158 upper := b &^ 32 159 if 'A' <= upper && upper <= 'Z' { 160 return i 161 } 162 } 163 164 return -1 165 } 166 167 func indexPair(s []byte, x byte, y byte) int { 168 var prev byte 169 170 for i, b := range s { 171 if prev == x && b == y { 172 return i 173 } 174 prev = b 175 } 176 177 return -1 178 } 179 180 func plain(w *bufio.Writer, r io.Reader, live bool) error { 181 const gb = 1024 * 1024 * 1024 182 sc := bufio.NewScanner(r) 183 sc.Buffer(nil, 8*gb) 184 185 for sc.Scan() { 186 for line := sc.Bytes(); len(line) > 0; { 187 i, j := indexEscapeSequence(line) 188 if i < 0 { 189 w.Write(line) 190 break 191 } 192 if j < 0 { 193 j = len(line) 194 } 195 196 if i > 0 { 197 w.Write(line[:i]) 198 } 199 200 line = line[j:] 201 } 202 203 if w.WriteByte('\n') != nil { 204 return errNoMoreOutput 205 } 206 207 if !live { 208 continue 209 } 210 211 if err := w.Flush(); err != nil { 212 return errNoMoreOutput 213 } 214 } 215 216 return sc.Err() 217 }