File: hima.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 hima.
  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 hima.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     n := len(os.Args) - 1
  46     exprs := make([]*regexp.Regexp, 0, n)
  47     for _, s := range os.Args[1:] {
  48         e, err := regexp.Compile(s)
  49         if err != nil {
  50             os.Stderr.WriteString("\x1b[31m")
  51             os.Stderr.WriteString(err.Error())
  52             os.Stderr.WriteString("\x1b[0m\n")
  53             nerr++
  54         }
  55         exprs = append(exprs, e)
  56     }
  57 
  58     if nerr > 0 {
  59         os.Exit(1)
  60     }
  61 
  62     sc := bufio.NewScanner(os.Stdin)
  63     sc.Buffer(nil, 8*1024*1024*1024)
  64     bw := bufio.NewWriter(os.Stdout)
  65 
  66     for sc.Scan() {
  67         handleLine(bw, sc.Bytes(), exprs)
  68         bw.WriteByte('\n')
  69         if err := bw.Flush(); err != nil {
  70             return
  71         }
  72     }
  73 }
  74 
  75 func findANSI(s []byte) (int, int) {
  76     var prev byte
  77 
  78     for i, b := range s {
  79         if prev != '\x1b' {
  80             prev = b
  81             continue
  82         }
  83 
  84         if b == '[' {
  85             for j, b := range s[i+1:] {
  86                 if ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') {
  87                     return i - 1, i + 1 + j + 1
  88                 }
  89             }
  90             return i - 1, len(s)
  91         }
  92 
  93         if b == ']' {
  94             j := bytes.IndexByte(s[i+1:], '\a')
  95             if j < 0 {
  96                 return i - 1, len(s)
  97             }
  98             return i - 1, i + 1 + j + 1
  99         }
 100 
 101         prev = b
 102     }
 103 
 104     return -1, -1
 105 }
 106 
 107 func handleLine(w *bufio.Writer, s []byte, with []*regexp.Regexp) {
 108     for len(s) > 0 {
 109         i, j := findANSI(s)
 110         if i < 0 {
 111             handleLineChunk(w, s, with)
 112             return
 113         }
 114 
 115         handleLineChunk(w, s[:i], with)
 116         w.Write(s[i:j])
 117         s = s[j:]
 118     }
 119 }
 120 
 121 func handleLineChunk(w *bufio.Writer, s []byte, with []*regexp.Regexp) {
 122     start := -1
 123     end := -1
 124 
 125     for len(s) > 0 {
 126         start = -1
 127         for _, e := range with {
 128             span := e.FindIndex(s)
 129             if span != nil && (span[0] < start || start < 0) {
 130                 start = span[0]
 131                 end = span[1]
 132             }
 133         }
 134 
 135         if start < 0 {
 136             w.Write(s)
 137             return
 138         }
 139 
 140         w.Write(s[:start])
 141         w.WriteString("\x1b[7m")
 142         w.Write(s[start:end])
 143         w.WriteString("\x1b[0m")
 144         s = s[end:]
 145     }
 146 }