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         return
  71     }
  72 }
  73 
  74 func run(w io.Writer, args []string) error {
  75     bw := bufio.NewWriter(w)
  76     defer bw.Flush()
  77 
  78     if len(args) == 0 {
  79         return timestamp(bw, os.Stdin)
  80     }
  81 
  82     for _, name := range args {
  83         if err := handleFile(bw, name); err != nil {
  84             return err
  85         }
  86     }
  87     return nil
  88 }
  89 
  90 func handleFile(w *bufio.Writer, name string) error {
  91     if name == `` || name == `-` {
  92         return timestamp(w, os.Stdin)
  93     }
  94 
  95     f, err := os.Open(name)
  96     if err != nil {
  97         return errors.New(`can't read from file named "` + name + `"`)
  98     }
  99     defer f.Close()
 100 
 101     return timestamp(w, f)
 102 }
 103 
 104 func timestamp(w *bufio.Writer, r io.Reader) error {
 105     const gb = 1024 * 1024 * 1024
 106     sc := bufio.NewScanner(r)
 107     sc.Buffer(nil, 8*gb)
 108 
 109     var buf [64]byte
 110 
 111     for i := 0; sc.Scan(); i++ {
 112         s := sc.Bytes()
 113         if i == 0 && bytes.HasPrefix(s, []byte{0xef, 0xbb, 0xbf}) {
 114             s = s[3:]
 115         }
 116 
 117         now := time.Now()
 118         w.WriteString("\x1b[48;2;218;218;218m\x1b[38;2;0;95;153m")
 119         w.Write(now.AppendFormat(buf[:0], `2006-01-02 15:04:05`))
 120         w.WriteString("\x1b[0m\t")
 121         // w.Write(now.AppendFormat(buf[:0], `2006-01-02 15:04:05`))
 122         // w.Write([]byte{'\t'})
 123 
 124         w.Write(s)
 125         w.WriteByte('\n')
 126         if err := w.Flush(); err != nil {
 127             // a write error may be the consequence of stdout being closed,
 128             // perhaps by another app along a pipe
 129             return io.EOF
 130         }
 131     }
 132 
 133     return sc.Err()
 134 }