Some polish, i guess
- Add a format string that uses text.Template. It's complex AF but it works really nicely. - Add a bunch of command line arguments to control the behaviour of the countdown: - Countdown duration - Output format - Output filename - Text to write when done - Fix countdown not starting until ~2 seconds after start. - Replace unhelpful start up text with something a bit more useful (parsed duration and output file name).
This commit is contained in:
parent
5893b1fa3d
commit
5c31d22c11
92
main.go
92
main.go
|
@ -1,45 +1,117 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
const filename string = "countdown.txt"
|
||||
var until time.Time = time.Now().Add(time.Minute)
|
||||
/*
|
||||
|
||||
%H:%M:%S
|
||||
H{%:}M{%:}%S
|
||||
|
||||
{{if .Hour}}{{.Hours}}h {{end}}{{if .Minute}}{{.Minutes}}m {{end}}{{.Seconds}}s
|
||||
|
||||
*/
|
||||
|
||||
func main() {
|
||||
|
||||
var filename string
|
||||
var untilStr string
|
||||
var targetTimeStr string
|
||||
var doneStr string
|
||||
var format string
|
||||
|
||||
flag.StringVar(&filename, "o", "/tmp/countdown.txt", "Output file to write the countdown to.")
|
||||
flag.StringVar(&untilStr, "u", "10m", "Countdown duration")
|
||||
flag.StringVar(&targetTimeStr, "t", "", "Countdown to time")
|
||||
flag.StringVar(&doneStr, "d", "00:00:00", "String to display when done")
|
||||
//flag.StringVar(&format, "f", "%H:%M:%S", "Format for the countdown")
|
||||
flag.StringVar(&format, "f", "{{if .Hours}}{{.Hours}}h {{end}}{{if or .Minutes .Hours}}{{.Minutes}}m {{end}}{{.Seconds}}s", "Format for the countdown")
|
||||
flag.Parse()
|
||||
|
||||
untilDuration, err := time.ParseDuration(untilStr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var until time.Time = time.Now().Add(untilDuration)
|
||||
|
||||
d, err := time.ParseDuration("1s")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("until: %s\n", until)
|
||||
fmt.Printf("Output file: %s\nCountdown duration: %s\n", filename, untilDuration)
|
||||
|
||||
tp := template.New("time")
|
||||
tp, err = tp.Parse(format)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
ticker := time.NewTicker(d)
|
||||
go func(c <-chan time.Time) {
|
||||
fmt.Println("XX")
|
||||
_ = <-c
|
||||
count := time.Until(until)
|
||||
for ; count >= 0; count = time.Until(until) {
|
||||
_ = <-c
|
||||
str := fmt.Sprintf("%02.0f:%02.0f:%02.0f\n", count.Hours(), count.Minutes(), count.Seconds())
|
||||
err = os.WriteFile(filename, []byte(str), 0777)
|
||||
h := int(count.Hours())
|
||||
m := int(int(count.Minutes()) % 60)
|
||||
s := int(int(count.Seconds()) % 60)
|
||||
data := struct {
|
||||
Hours int
|
||||
Minutes int
|
||||
Seconds int
|
||||
}{
|
||||
Hours: h,
|
||||
Minutes: m,
|
||||
Seconds: s,
|
||||
}
|
||||
|
||||
// Do I want to use this format as well as the complicated AF one?I want to use this format as well as the complicated AF template one?
|
||||
//str := format
|
||||
//if hide && h == 0 {
|
||||
// str = strings.ReplaceAll(str, "%H", "")
|
||||
// str = strings.ReplaceAll(str, "%h", "")
|
||||
//} else {
|
||||
// str = strings.ReplaceAll(str, "%H", fmt.Sprintf("%02d", h))
|
||||
// str = strings.ReplaceAll(str, "%h", fmt.Sprintf("%d", h))
|
||||
//}
|
||||
//if !(hide && h == 0 && m == 0) {
|
||||
// str = strings.ReplaceAll(str, "%M", fmt.Sprintf("%02d", m))
|
||||
// str = strings.ReplaceAll(str, "%m", fmt.Sprintf("%d", m))
|
||||
//}
|
||||
//str = strings.ReplaceAll(str, "%S", fmt.Sprintf("%02d", s))
|
||||
//str = strings.ReplaceAll(str, "%s", fmt.Sprintf("%d", s))
|
||||
|
||||
sb := strings.Builder{}
|
||||
err = tp.Execute(&sb, &data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
err = os.WriteFile(filename, []byte(sb.String()), 0644)
|
||||
if err != nil {
|
||||
fmt.Println("unable to write file: %v\n", err)
|
||||
}
|
||||
_ = <-c
|
||||
}
|
||||
wg.Done()
|
||||
}(ticker.C)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
err = os.WriteFile(filename, []byte("00:00:00"), 0777)
|
||||
err = os.WriteFile(filename, []byte(doneStr), 0644)
|
||||
if err != nil {
|
||||
fmt.Println("unable to write file: %v\n", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue