Snippets

Sacha Ligthert Prints the progress of the current year in percentage allong with a small graph.

Created by Sacha Ligthert
package main

import (
        "fmt"
        "time"
)

func main() {

        // Start loop
        for {
                // Clear the screen using ANSI escape codes
                fmt.Print("\033[H\033[2J")

                // Print current time for clarity and context
                fmt.Print(time.Now(), "\n\n")

                // Calculate the percentage how many seconds this year has progressed
                percentage := calcPercentage()

                // Convert percentage number into a human readable graph
                printGraphic(percentage)

                // Calculate required delay so this updates once every second on the second
                delay := calcDelay()

                // Delay
                time.Sleep(delay)

        }

}

func calcPercentage() float64 {
        // Current Time
        ct := time.Now()
        t1 := Date(ct.Year(), 1, 1)

        // Next year
        ny := ct.AddDate(1, 0, 0)
        t2 := Date(ny.Year(), 1, 1)

        // Get all the seconds in a year
        seconds := t2.Sub(t1).Seconds()

        // Calculate the difference in seconds between ct and ny
        tdiff := ct.Sub(t1).Seconds()

        // Return the percentage
        return calcPercentageFloat64(seconds, tdiff)
}

func calcPercentageFloat64(full float64, part float64) float64 {
        return part / (full / 100)
}

func Date(year, month, day int) time.Time {
        return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}

func printGraphic(percentage float64) {

        counter := 0
        workPerc := percentage

        fmt.Print(" |")
        for workPerc > 2 {
                fmt.Print("▓")
                workPerc = workPerc - 2
                counter++
        }
        if workPerc < 2 {
                fmt.Print("▒")
                counter++
        }

        for counter < 50 {
                fmt.Print("░")
                counter++
        }
        fmt.Print("| ", percentage, "%\n")
}

func calcDelay() time.Duration {
        // Current Time
        ct := time.Now()

        // Future Time
        ft := ct.Add(1 * time.Second)

        // Required time
        rt := time.Date(ft.Year(), ft.Month(), ft.Day(), ft.Hour(), ft.Minute(), ft.Second(), int(0), time.UTC)

        // Diff time
        return rt.Sub(ct)
}

Comments (1)

  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.

    It looks like this.

    2021-04-21 22:51:26.000484618 +0000 UTC m=+2.535979398
    
     |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| 30.397913497503023%
    

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.