packagemainimport("fmt""time")funcmain(){// Start loopfor{// Clear the screen using ANSI escape codesfmt.Print("\033[H\033[2J")// Print current time for clarity and contextfmt.Print(time.Now(),"\n\n")// Calculate the percentage how many seconds this year has progressedpercentage:=calcPercentage()// Convert percentage number into a human readable graphprintGraphic(percentage)// Calculate required delay so this updates once every second on the seconddelay:=calcDelay()// Delaytime.Sleep(delay)}}funccalcPercentage()float64{// Current Timect:=time.Now()t1:=Date(ct.Year(),1,1)// Next yearny:=ct.AddDate(1,0,0)t2:=Date(ny.Year(),1,1)// Get all the seconds in a yearseconds:=t2.Sub(t1).Seconds()// Calculate the difference in seconds between ct and nytdiff:=ct.Sub(t1).Seconds()// Return the percentagereturncalcPercentageFloat64(seconds,tdiff)}funccalcPercentageFloat64(fullfloat64,partfloat64)float64{returnpart/(full/100)}funcDate(year,month,dayint)time.Time{returntime.Date(year,time.Month(month),day,0,0,0,0,time.UTC)}funcprintGraphic(percentagefloat64){counter:=0workPerc:=percentagefmt.Print(" |")forworkPerc>2{fmt.Print("▓")workPerc=workPerc-2counter++}ifworkPerc<2{fmt.Print("▒")counter++}forcounter<50{fmt.Print("░")counter++}fmt.Print("| ",percentage,"%\n")}funccalcDelay()time.Duration{// Current Timect:=time.Now()// Future Timeft:=ct.Add(1*time.Second)// Required timert:=time.Date(ft.Year(),ft.Month(),ft.Day(),ft.Hour(),ft.Minute(),ft.Second(),int(0),time.UTC)// Diff timereturnrt.Sub(ct)}
Comments (1)
Sacha Ligthert
I didn’t waste time creating this, it was actually a great learning experience. Yet when I use it and stare at the output I get the feeling I am wasting my precious life.
I didn’t waste time creating this, it was actually a great learning experience. Yet when I use it and stare at the output I get the feeling I am wasting my precious life.
It looks like this.