File: erase.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 erase.
  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 erase.go
  32 */
  33 
  34 package main
  35 
  36 import (
  37     "bufio"
  38     "bytes"
  39     "os"
  40     "regexp"
  41 )
  42 
  43 func main() {
  44     nerr := 0
  45     args := os.Args[1:]
  46     if len(args) == 0 {
  47         args = []string{`[^\r]`}
  48     }
  49 
  50     exprs := make([]*regexp.Regexp, 0, len(args))
  51     for _, s := range args {
  52         e, err := regexp.Compile(s)
  53         if err != nil {
  54             os.Stderr.WriteString("\x1b[31m")
  55             os.Stderr.WriteString(err.Error())
  56             os.Stderr.WriteString("\x1b[0m\n")
  57             nerr++
  58         }
  59         exprs = append(exprs, e)
  60     }
  61 
  62     if nerr > 0 {
  63         os.Exit(1)
  64     }
  65 
  66     sc := bufio.NewScanner(os.Stdin)
  67     sc.Buffer(nil, 8*1024*1024*1024)
  68     bw := bufio.NewWriter(os.Stdout)
  69 
  70     var srcbuf []byte
  71     var dstbuf []byte
  72     src := srcbuf[:0]
  73     dst := dstbuf[:0]
  74 
  75     for sc.Scan() {
  76         src = append(src[:0], sc.Bytes()...)
  77         for _, e := range exprs {
  78             dst = dst[:0]
  79             dst = erase(&dst, src, e)
  80             src = append(src[:0], dst...)
  81         }
  82 
  83         bw.Write(dst)
  84         bw.WriteByte('\n')
  85         if err := bw.Flush(); err != nil {
  86             return
  87         }
  88     }
  89 }
  90 
  91 // indexEscapeSequence finds the first ANSI-style escape-sequence, which is
  92 // either the alert/bell byte, or the multi-byte sequences starting either
  93 // with ESC[ or ESC]; either returned index can be negative
  94 func indexEscapeSequence(s []byte) (int, int) {
  95     var prev byte
  96 
  97     for i, b := range s {
  98         if b == '\a' {
  99             return i, i + 1
 100         }
 101 
 102         if prev == '\x1b' && b == '[' {
 103             j := indexLetter(s[i+1:])
 104             if j < 0 {
 105                 return i, -1
 106             }
 107             return i - 1, i + 1 + j + 1
 108         }
 109 
 110         if prev == '\x1b' && b == ']' {
 111             j := bytes.IndexByte(s[i+1:], ':')
 112             if j < 0 {
 113                 return i, -1
 114             }
 115             return i - 1, i + 1 + j + 1
 116         }
 117 
 118         if prev == '\x1b' && b == '\\' {
 119             return i - 1, i + 1
 120         }
 121 
 122         prev = b
 123     }
 124 
 125     return -1, -1
 126 }
 127 
 128 func indexLetter(s []byte) int {
 129     for i, b := range s {
 130         if 'A' <= b && b <= 'Z' {
 131             return i
 132         }
 133         if 'a' <= b && b <= 'z' {
 134             return i
 135         }
 136     }
 137 
 138     return -1
 139 }
 140 
 141 func erase(dst *[]byte, src []byte, with *regexp.Regexp) []byte {
 142     for len(src) > 0 {
 143         i, j := indexEscapeSequence(src)
 144         if i < 0 {
 145             return handleLineChunk(*dst, src, with)
 146         }
 147 
 148         *dst = handleLineChunk(*dst, src[:i], with)
 149         if j < 0 {
 150             break
 151         }
 152 
 153         *dst = append(*dst, src[i:j]...)
 154         src = src[j:]
 155     }
 156 
 157     return *dst
 158 }
 159 
 160 func handleLineChunk(dst []byte, src []byte, with *regexp.Regexp) []byte {
 161     for len(src) > 0 {
 162         span := with.FindIndex(src)
 163         if span == nil {
 164             return append(dst, src...)
 165         }
 166 
 167         start := span[0]
 168         end := span[1]
 169         dst = append(dst, src[:start]...)
 170         src = src[end:]
 171     }
 172 
 173     return dst
 174 }