/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for countdown. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath countdown.go */ package main import ( "os" "os/signal" "strconv" "time" ) // Note: the code is avoiding using the fmt package to save hundreds of // kilobytes on the resulting executable, which is a noticeable difference. const ( spaces = ` ` // clear has enough spaces in it to cover any chronograph output clear = "\r" + spaces + spaces + spaces + "\r" ) const info = ` countdown [period] Run a live countdown timer on stderr, until the time-period given ends, or the app is force-quit. The time-period is either a simple integer number (of seconds), or an integer followed by any of - "s" (for seconds) - "m" (for minutes) - "h" (for hours) without spaces, or a combination of those time-units without spaces. ` func main() { if len(os.Args) == 1 { os.Stderr.WriteString(info[1:]) return } if len(os.Args) == 2 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } period, err := parseDuration(os.Args[1]) if err != nil { os.Stderr.WriteString("\x1b[31m") os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") os.Exit(1) } os.Stderr.WriteString("Countdown lasting ") os.Stderr.WriteString(time.Time{}.Add(period).Format(`15:04:05`)) os.Stderr.WriteString(" started\n") countdown(period) return } os.Stderr.WriteString(info[1:]) os.Exit(1) } func parseDuration(s string) (time.Duration, error) { if n, err := strconv.Atoi(s); err == nil { return time.Duration(n) * time.Second, err } return time.ParseDuration(s) } func countdown(period time.Duration) { start := time.Now() end := start.Add(period) t := time.NewTicker(100 * time.Millisecond) startChronoLine(end, start) stopped := make(chan os.Signal, 1) defer close(stopped) signal.Notify(stopped, os.Interrupt) for { select { case now := <-t.C: os.Stderr.WriteString(clear) startChronoLine(end, now) if now.Sub(end) >= 0 { t.Stop() endChronoLine(start) return } case <-stopped: t.Stop() endChronoLine(start) return } } } // func startChronoLine(end, now time.Time) { // var buf [64]byte // dt := end.Sub(now) // // os.Stderr.Write(time.Time{}.Add(dt).AppendFormat(buf[:0], `15:04:05.0`)) // os.Stderr.WriteString(` `) // os.Stderr.Write(now.AppendFormat(buf[:0], `2006-01-02 15:04:05 Jan Mon`)) // } func startChronoLine(end, now time.Time) { var buf [64]byte dt := end.Sub(now) s := buf[:0] s = time.Time{}.Add(dt).AppendFormat(s, `15:04:05.0`) s = append(s, ` `...) s = now.AppendFormat(s, `2006-01-02 15:04:05 Jan Mon`) os.Stderr.Write(s) } func endChronoLine(start time.Time) { var buf [64]byte secs := time.Since(start).Seconds() os.Stderr.WriteString(` `) os.Stderr.Write(strconv.AppendFloat(buf[:0], secs, 'f', 4, 64)) os.Stderr.WriteString(" seconds\n") }