/* The MIT License (MIT) Copyright (c) 2026 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath plain.go */ package main import ( "bufio" "errors" "io" "os" ) // Note: the code is avoiding using the fmt package to save hundreds of // kilobytes on the resulting executable, which is a noticeable difference. const info = ` plain [options...] [file...] Turn potentially ANSI-styled plain-text into actual plain-text. Input is assumed to be UTF-8, and all CRLF byte-pairs are turned into line feeds. All (optional) leading options start with either single or double-dash: -h show this help message -help show this help message ` // errNoMoreOutput is a dummy error whose message is ignored, and which // causes the app to quit immediately and successfully var errNoMoreOutput = errors.New(`no more output`) func main() { buffered := false args := os.Args[1:] if len(args) > 0 { switch args[0] { case `-b`, `--b`, `-buffered`, `--buffered`: buffered = true args = args[1:] case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return } } if len(args) > 0 && args[0] == `--` { args = args[1:] } liveLines := !buffered if !buffered { if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { liveLines = false } } if err := run(os.Stdout, args, liveLines); isActualError(err) { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") os.Exit(1) } } func run(w io.Writer, args []string, live bool) error { bw := bufio.NewWriter(w) defer bw.Flush() if len(args) == 0 { return plain(bw, os.Stdin, live) } for _, name := range args { if err := handleFile(bw, name, live); err != nil { return err } } return nil } func handleFile(w *bufio.Writer, name string, live bool) error { if name == `` || name == `-` { return plain(w, os.Stdin, live) } f, err := os.Open(name) if err != nil { return errors.New(`can't read from file named "` + name + `"`) } defer f.Close() return plain(w, f, live) } // isActualError is to figure out whether not to ignore an error, and thus // show it as an error message func isActualError(err error) bool { return err != nil && err != errNoMoreOutput } // indexEscapeSequence finds the first ANSI-style escape-sequence, which is // the multi-byte sequences starting with ESC[; the result is a pair of slice // indices which can be independently negative when either the start/end of // a sequence isn't found; given their fairly-common use, even the hyperlink // ESC]8 sequences are supported func indexEscapeSequence(s []byte) (int, int) { var prev byte for i, b := range s { if prev == '\x1b' && b == '[' { j := indexLetter(s[i+1:]) if j < 0 { return i, -1 } return i - 1, i + 1 + j + 1 } if prev == '\x1b' && b == ']' && i+1 < len(s) && s[i+1] == '8' { j := indexPair(s[i+1:], '\x1b', '\\') if j < 0 { return i, -1 } return i - 1, i + 1 + j + 2 } prev = b } return -1, -1 } func indexLetter(s []byte) int { for i, b := range s { upper := b &^ 32 if 'A' <= upper && upper <= 'Z' { return i } } return -1 } func indexPair(s []byte, x byte, y byte) int { var prev byte for i, b := range s { if prev == x && b == y { return i } prev = b } return -1 } func plain(w *bufio.Writer, r io.Reader, live bool) error { const gb = 1024 * 1024 * 1024 sc := bufio.NewScanner(r) sc.Buffer(nil, 8*gb) for i := 0; sc.Scan(); i++ { s := sc.Bytes() if i == 0 && len(s) > 2 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf { s = s[3:] } for line := s; len(line) > 0; { i, j := indexEscapeSequence(line) if i < 0 { w.Write(line) break } if j < 0 { j = len(line) } if i > 0 { w.Write(line[:i]) } line = line[j:] } if w.WriteByte('\n') != nil { return errNoMoreOutput } if !live { continue } if err := w.Flush(); err != nil { return errNoMoreOutput } } return sc.Err() }