File: timestamp.go 1 /* 2 The MIT License (MIT) 3 4 Copyright © 2026 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 timestamp.go 30 */ 31 32 package main 33 34 import ( 35 "bufio" 36 "errors" 37 "io" 38 "os" 39 "time" 40 ) 41 42 // Note: the code is avoiding using the fmt package to save hundreds of 43 // kilobytes on the resulting executable, which is a noticeable difference. 44 45 const info = ` 46 timestamp [options...] [file...] 47 48 49 Start each line with a timestamp, followed by a tab, and then the line. 50 51 Input is assumed to be UTF-8, and all CRLF byte-pairs are turned into line 52 feeds. Leading BOM (byte-order marks) on first lines are also ignored. 53 54 All (optional) leading options start with either single or double-dash: 55 56 -h show this help message 57 -help show this help message 58 ` 59 60 // errNoMoreOutput is a dummy error whose message is ignored, and which 61 // causes the app to quit immediately and successfully 62 var errNoMoreOutput = errors.New(`no more output`) 63 64 func main() { 65 if len(os.Args) > 1 { 66 switch os.Args[1] { 67 case `-h`, `--h`, `-help`, `--help`: 68 os.Stdout.WriteString(info[1:]) 69 return 70 } 71 } 72 73 if err := run(os.Stdout, os.Args[1:]); isActualError(err) { 74 os.Stderr.WriteString(err.Error()) 75 os.Stderr.WriteString("\n") 76 os.Exit(1) 77 } 78 } 79 80 func run(w io.Writer, args []string) error { 81 bw := bufio.NewWriter(w) 82 defer bw.Flush() 83 84 if len(args) == 0 { 85 return timestamp(bw, os.Stdin) 86 } 87 88 for _, name := range args { 89 if err := handleFile(bw, name); err != nil { 90 return err 91 } 92 } 93 return nil 94 } 95 96 func handleFile(w *bufio.Writer, name string) error { 97 if name == `` || name == `-` { 98 return timestamp(w, os.Stdin) 99 } 100 101 f, err := os.Open(name) 102 if err != nil { 103 return errors.New(`can't read from file named "` + name + `"`) 104 } 105 defer f.Close() 106 107 return timestamp(w, f) 108 } 109 110 // isActualError is to figure out whether not to ignore an error, and thus 111 // show it as an error message 112 func isActualError(err error) bool { 113 return err != nil && err != io.EOF && err != errNoMoreOutput 114 } 115 116 func timestamp(w *bufio.Writer, r io.Reader) error { 117 const gb = 1024 * 1024 * 1024 118 sc := bufio.NewScanner(r) 119 sc.Buffer(nil, 8*gb) 120 121 var buf [64]byte 122 123 for sc.Scan() { 124 now := time.Now() 125 w.WriteString("\x1b[48;2;218;218;218m\x1b[38;2;0;95;153m") 126 w.Write(now.AppendFormat(buf[:0], `2006-01-02 15:04:05`)) 127 w.WriteString("\x1b[0m\t") 128 // w.Write(now.AppendFormat(buf[:0], `2006-01-02 15:04:05`)) 129 // w.Write([]byte{'\t'}) 130 131 w.Write(sc.Bytes()) 132 w.WriteByte('\n') 133 if err := w.Flush(); err != nil { 134 // a write error may be the consequence of stdout being closed, 135 // perhaps by another app along a pipe 136 return errNoMoreOutput 137 } 138 } 139 140 return sc.Err() 141 }