2021-02-27 21:35:02 -08:00
package main
import (
"fmt"
"os"
2021-02-28 17:55:52 -08:00
"strings"
2021-02-27 21:35:02 -08:00
"sync"
2021-02-28 17:55:52 -08:00
"time"
"text/template"
2022-12-02 18:25:26 -08:00
"github.com/alexflint/go-arg"
2021-02-27 21:35:02 -08:00
)
2021-02-28 17:55:52 -08:00
/ *
% H : % M : % S
H { % : } M { % : } % S
{ { if . Hour } } { { . Hours } } h { { end } } { { if . Minute } } { { . Minutes } } m { { end } } { { . Seconds } } s
* /
2021-02-27 21:35:02 -08:00
2022-12-02 18:25:26 -08:00
type Arguments struct {
Hour int ` arg:"--hour,-h" help:"Target hour" default:"-1" `
Minute int ` arg:"--minute,-m" help:"Target minute" default:"-1" `
Year int ` arg:"--year" help:"Target year" default:"-1" `
Day int ` arg:"--day" help:"Target day" default:"-1" `
Month int ` arg:"--month" help:"Target month" default:"-1" `
2021-02-28 17:55:52 -08:00
2022-12-02 18:25:26 -08:00
Output string ` arg:"--output,-o" help:"Output filename" default:"/tmp/countdown.txt" `
Done string ` arg:"--done,-d" help:"String to display when done" default:"MOVIE SIGN" `
Format string ` arg:"--format,-f" help:"Format string for the countdown" default:" {{ if .Hours }} {{ .Hours }} h {{ end }} {{ if or .Minutes .Hours }} {{ .Minutes }} m {{ end }} {{ .Seconds }} s" `
}
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
func main ( ) {
args := & Arguments { }
arg . MustParse ( args )
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
now := time . Now ( )
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
if args . Hour == - 1 {
args . Hour = now . Hour ( )
}
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
if args . Minute == - 1 {
args . Minute = now . Minute ( )
}
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
if args . Year == - 1 {
args . Year = now . Year ( )
}
2021-02-28 17:55:52 -08:00
2022-12-02 18:25:26 -08:00
if args . Month == - 1 {
args . Month = int ( now . Month ( ) )
}
2021-02-28 17:55:52 -08:00
2022-12-02 18:25:26 -08:00
if args . Day == - 1 {
args . Day = now . Day ( )
}
2021-02-28 17:55:52 -08:00
2022-12-02 18:25:26 -08:00
until := time . Date (
args . Year , time . Month ( args . Month ) , args . Day ,
args . Hour , args . Minute , 0 , 0 , time . Local ,
)
2021-02-28 18:59:06 -08:00
2022-12-02 18:25:26 -08:00
if now . After ( until ) {
fmt . Println ( "time is in the past!" )
return
2021-02-27 21:35:02 -08:00
}
2022-12-02 18:25:26 -08:00
fmt . Printf ( "Output file: %s\nCountdown to: %s\n" , args . Output , until )
2021-02-28 17:55:52 -08:00
2021-02-28 18:59:06 -08:00
var err error
2021-02-28 17:55:52 -08:00
tp := template . New ( "time" )
2022-12-02 18:25:26 -08:00
tp , err = tp . Parse ( args . Format )
2021-02-28 17:55:52 -08:00
if err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
2021-02-27 21:35:02 -08:00
wg := sync . WaitGroup { }
wg . Add ( 1 )
2021-02-28 18:59:06 -08:00
ticker := time . NewTicker ( time . Second )
2021-02-27 21:35:02 -08:00
go func ( c <- chan time . Time ) {
count := time . Until ( until )
2021-02-28 18:59:06 -08:00
fmt . Println ( count )
2021-02-27 21:35:02 -08:00
for ; count >= 0 ; count = time . Until ( until ) {
2021-02-28 17:55:52 -08:00
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 ,
}
2022-12-02 18:25:26 -08:00
// Do I want to use this format as well as the complicated AF one?
2021-02-28 17:55:52 -08:00
//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
}
2022-12-02 18:25:26 -08:00
err = os . WriteFile ( args . Output , [ ] byte ( sb . String ( ) ) , 0644 )
2021-02-27 21:35:02 -08:00
if err != nil {
fmt . Println ( "unable to write file: %v\n" , err )
}
2021-02-28 17:55:52 -08:00
_ = <- c
2021-02-27 21:35:02 -08:00
}
wg . Done ( )
} ( ticker . C )
wg . Wait ( )
2022-12-02 18:25:26 -08:00
err = os . WriteFile ( args . Output , [ ] byte ( args . Done ) , 0644 )
2021-02-27 21:35:02 -08:00
if err != nil {
fmt . Println ( "unable to write file: %v\n" , err )
}
fmt . Println ( "\nDONE" )
}