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