mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 12 #14

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Create Animated Clock

Create an animated clock. See the program below, which is very similar

This program prints and animated spinner. Mutex isn't needed; but, makes it makes it thread safe.

repl.it

package main

import (
    "fmt"
    "sync"
    "time"
)

type MF struct {
    l int
    sync.Mutex
}

func (mf *MF)mfmt(txt string,timeSleep int) {
    mf.Mutex.Lock()
    defer mf.Mutex.Unlock()

    for i:=0;i<mf.l;i++ {
        fmt.Printf("\b")  // backspace
    }

    mf.l = len(txt)
    fmt.Printf("%s",txt)
    time.Sleep(time.Duration(timeSleep) * time.Millisecond)
}

func main() {
    msg := []string{"\\ ","| ","/ ","- ","+ ","- ","* "}

    m := &MF{}
    for i :=0; i < 100 ; i++ {
        for _, v := range msg {
            m.mfmt(v, 50)
        }
    }

}

Helper

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Printf("%02d:%02d:%02d\n",now.Hour(),now.Minute(),now.Second())
}
tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

is the spinning per second or should it go with the hour:min

mchirico commented 3 years ago

@ZoeChiri Your choice...