feat: nocolor
continuous-integration/drone/tag Build is passing Details

master v0.1.1
Evan Chen 2021-11-30 13:06:07 +08:00
parent 7126239574
commit be4d3f18d6
3 changed files with 39 additions and 8 deletions

5
go.mod
View File

@ -1,3 +1,8 @@
module kumoly.io/tools/kclock
go 1.17
require (
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

36
main.go
View File

@ -7,18 +7,23 @@ import (
"os/signal"
"syscall"
"time"
"github.com/mattn/go-isatty"
)
var (
Version = "0.0.0"
Build = "alpha"
local string
flagTick string
flagVer bool
)
func main() {
var local string
var flagVer bool
flag.StringVar(&local, "local", fmt.Sprint(time.Local), "set local manually")
flag.StringVar(&flagTick, "tick", "1s", "ticking interval")
flag.BoolVar(&flagVer, "v", false, "show version")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: kclock [options]\n")
@ -43,20 +48,37 @@ func main() {
}
func watch() {
interval, err := time.ParseDuration(flagTick)
if err != nil {
panic(err)
}
color := isatty.IsTerminal(os.Stdout.Fd())
if !color {
color = isatty.IsCygwinTerminal(os.Stdout.Fd())
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
start := time.Now()
next := time.Now().Truncate(time.Second).Add(time.Second)
<-time.After(time.Until(next))
tick := time.Tick(time.Second)
<-time.After(time.Until(time.Now().Truncate(time.Second).Add(time.Second)))
tick := time.Tick(interval)
if color {
fmt.Printf("\n")
}
for {
fmt.Printf("\r\033[1A\033[0K\033[92m%v\033[0m\n", time.Now().Truncate(time.Second))
if color {
fmt.Printf("\r\033[1A\033[0K\033[32m%v\033[0m\n", time.Now().Truncate(time.Second))
} else {
fmt.Printf("%v\n", time.Now().Truncate(time.Second))
}
select {
case <-tick:
case <-quit:
fmt.Printf("\r\033[0Ktime elapsed: \033[94m%v\033[0m\n", time.Since(start))
if color {
fmt.Printf("\r\033[0Ktime elapsed: \033[34m%v\033[0m\n", time.Since(start))
} else {
fmt.Printf("time elapsed: %v\n", time.Since(start))
}
return
}