/* 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 ecoli.go */ package main import ( "bufio" "io" "os" "regexp" ) const info = ` ecoli [options...] [regex/style pairs...] Expressions COloring LInes tries to match each line read from the standard input to the regexes given, coloring/styling with the named-style paired to the first matching regex, if any. Lines not matching any regex stay the same. The options are, available both in single and double-dash versions -h, -help show this help message -i, -ins, -insensitive match the regexes given case-insensitively Some of the colors/styles available are: blue blueback bold gray grayback green greenback inverse magenta magentaback orange orangeback purple purpleback red redback underline Some style aliases are: b blue bb blueback g green gb greenback m magenta mb magentaback o orange ob orangeback p purple pb purpleback r red rb redback i inverse (highlight) u underline ` var styleAliases = map[string]string{ `b`: `blue`, `g`: `green`, `m`: `magenta`, `o`: `orange`, `p`: `purple`, `r`: `red`, `u`: `underline`, `bb`: `blueback`, `bg`: `greenback`, `bm`: `magentaback`, `bo`: `orangeback`, `bp`: `purpleback`, `br`: `redback`, `gb`: `greenback`, `mb`: `magentaback`, `ob`: `orangeback`, `pb`: `purpleback`, `rb`: `redback`, `hi`: `inverse`, `inv`: `inverse`, `mag`: `magenta`, `du`: `doubleunderline`, `flip`: `inverse`, `swap`: `inverse`, `reset`: `plain`, `highlight`: `inverse`, `hilite`: `inverse`, `invert`: `inverse`, `inverted`: `inverse`, `swapped`: `inverse`, `dunderline`: `doubleunderline`, `dunderlined`: `doubleunderline`, `strikethrough`: `strike`, `strikethru`: `strike`, `struck`: `strike`, `underlined`: `underline`, `bblue`: `blueback`, `bgray`: `grayback`, `bgreen`: `greenback`, `bmagenta`: `magentaback`, `borange`: `orangeback`, `bpurple`: `purpleback`, `bred`: `redback`, `bgblue`: `blueback`, `bggray`: `grayback`, `bggreen`: `greenback`, `bgmag`: `magentaback`, `bgmagenta`: `magentaback`, `bgorange`: `orangeback`, `bgpurple`: `purpleback`, `bgred`: `redback`, `bluebg`: `blueback`, `graybg`: `grayback`, `greenbg`: `greenback`, `magbg`: `magentaback`, `magentabg`: `magentaback`, `orangebg`: `orangeback`, `purplebg`: `purpleback`, `redbg`: `redback`, `backblue`: `blueback`, `backgray`: `grayback`, `backgreen`: `greenback`, `backmag`: `magentaback`, `backmagenta`: `magentaback`, `backorange`: `orangeback`, `backpurple`: `purpleback`, `backred`: `redback`, } var styles = map[string]string{ `blue`: "\x1b[38;2;0;95;215m", `bold`: "\x1b[1m", `doubleunderline`: "\x1b[21m", `gray`: "\x1b[38;2;168;168;168m", `green`: "\x1b[38;2;0;135;95m", `inverse`: "\x1b[7m", `magenta`: "\x1b[38;2;215;0;255m", `orange`: "\x1b[38;2;215;95;0m", `plain`: "\x1b[0m", `purple`: "\x1b[38;2;135;95;255m", `red`: "\x1b[38;2;204;0;0m", `strike`: "\x1b[9m", `underline`: "\x1b[4m", `blueback`: "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m", `grayback`: "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m", `greenback`: "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m", `magentaback`: "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m", `orangeback`: "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m", `purpleback`: "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m", `redback`: "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m", } // pair has a regular-expression and its associated ANSI-code style together type pair struct { expr *regexp.Regexp style string } func main() { buffered := false insensitive := false args := os.Args[1:] out: for len(args) > 0 { switch args[0] { case `-b`, `--b`, `-buffered`, `--buffered`: buffered = true args = args[1:] continue case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return case `-i`, `--i`, `-ins`, `--ins`: insensitive = true args = args[1:] continue default: break out } } if len(args) > 0 && args[0] == `--` { args = args[1:] } if len(args)%2 != 0 { const msg = "you forgot the style-name for/after the last regex\n" os.Stderr.WriteString(msg) os.Exit(1) } nerr := 0 pairs := make([]pair, 0, len(args)/2) for len(args) >= 2 { src := args[0] sname := args[1] var err error var exp *regexp.Regexp if insensitive { exp, err = regexp.Compile(`(?i)` + src) } else { exp, err = regexp.Compile(src) } if err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") nerr++ } if alias, ok := styleAliases[sname]; ok { sname = alias } style, ok := styles[sname] if !ok { os.Stderr.WriteString("no style named `") os.Stderr.WriteString(args[1]) os.Stderr.WriteString("`\n") nerr++ } pairs = append(pairs, pair{expr: exp, style: style}) args = args[2:] } if nerr > 0 { os.Exit(1) } liveLines := !buffered if !buffered { if _, err := os.Stdout.Seek(0, io.SeekCurrent); err == nil { liveLines = false } } sc := bufio.NewScanner(os.Stdin) sc.Buffer(nil, 8*1024*1024*1024) bw := bufio.NewWriter(os.Stdout) var plain []byte 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:] } plain = appendPlain(plain[:0], s) if err := handleLine(bw, s, noANSI(plain), pairs); err != nil { return } if !liveLines { continue } if err := bw.Flush(); err != nil { return } } } // appendPlain extends the slice given using the non-ANSI parts of a string func appendPlain(dst []byte, src []byte) []byte { for len(src) > 0 { i, j := indexEscapeSequence(src) if i < 0 { dst = append(dst, src...) break } if j < 0 { j = len(src) } if i > 0 { dst = append(dst, src[:i]...) } src = src[j:] } return dst } // 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 } // noANSI ensures arguments to func handleLine are given in the right order type noANSI []byte // handleLine styles the current line given to it using the first matching // regex, keeping it as given if none of the regexes match; it's given 2 // strings: the first is the original line, the latter is its plain-text // version (with no ANSI codes) and is used for the regex-matching, since // ANSI codes use a mix of numbers and letters, which can themselves match func handleLine(w *bufio.Writer, s []byte, plain noANSI, pairs []pair) error { for _, p := range pairs { if p.expr.Match(plain) { w.WriteString(p.style) w.Write(s) w.WriteString("\x1b[0m") return w.WriteByte('\n') } } w.Write(s) return w.WriteByte('\n') }