yacdt/main.go

165 lines
3.7 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"text/template"
)
/*
%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", "", "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")
var targetYear int
var targetDay int
var targetMonth int
flag.IntVar(&targetYear, "target-year", -1, "")
flag.IntVar(&targetDay, "target-day", -1, "")
flag.IntVar(&targetMonth, "target-month", -1, "")
var targetHour int
var targetMinute int
var targetSecond int
flag.IntVar(&targetHour, "target-hour", -1, "")
2021-07-10 10:22:51 -07:00
flag.IntVar(&targetMinute, "target-minute", 0, "")
flag.IntVar(&targetSecond, "target-second", 0, "")
flag.Parse()
var until time.Time
now := time.Now()
if untilStr != "" {
untilDuration, err := time.ParseDuration(untilStr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
until = time.Now().Add(untilDuration)
} else {
if targetYear == -1 {
targetYear = now.Year()
}
if targetMonth == -1 {
targetMonth = int(now.Month())
}
if targetDay == -1 {
targetDay = now.Day()
}
if targetHour == -1 {
targetHour = now.Hour()
}
if targetMinute == -1 {
targetMinute = now.Minute()
}
if targetSecond == -1 {
targetSecond = now.Second()
}
until = time.Date(
targetYear, time.Month(targetMonth), targetDay,
targetHour, targetMinute, targetSecond,
0, time.Local,
)
}
2021-07-10 10:22:51 -07:00
fmt.Printf("Output file: %s\nCountdown to: %s\n", filename, until)
var err error
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(time.Second)
go func(c <-chan time.Time) {
count := time.Until(until)
fmt.Println(count)
for ; count >= 0; count = time.Until(until) {
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(doneStr), 0644)
if err != nil {
fmt.Println("unable to write file: %v\n", err)
}
fmt.Println("\nDONE")
}