parent
7126239574
commit
be4d3f18d6
5
go.mod
5
go.mod
|
@ -1,3 +1,8 @@
|
||||||
module kumoly.io/tools/kclock
|
module kumoly.io/tools/kclock
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
||||||
|
)
|
||||||
|
|
|
@ -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
36
main.go
|
@ -7,18 +7,23 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Version = "0.0.0"
|
Version = "0.0.0"
|
||||||
Build = "alpha"
|
Build = "alpha"
|
||||||
|
|
||||||
|
local string
|
||||||
|
flagTick string
|
||||||
|
flagVer bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var local string
|
|
||||||
var flagVer bool
|
|
||||||
|
|
||||||
flag.StringVar(&local, "local", fmt.Sprint(time.Local), "set local manually")
|
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.BoolVar(&flagVer, "v", false, "show version")
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage: kclock [options]\n")
|
fmt.Fprintf(os.Stderr, "Usage: kclock [options]\n")
|
||||||
|
@ -43,20 +48,37 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func watch() {
|
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)
|
quit := make(chan os.Signal, 1)
|
||||||
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
next := time.Now().Truncate(time.Second).Add(time.Second)
|
<-time.After(time.Until(time.Now().Truncate(time.Second).Add(time.Second)))
|
||||||
<-time.After(time.Until(next))
|
tick := time.Tick(interval)
|
||||||
tick := time.Tick(time.Second)
|
if color {
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
for {
|
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 {
|
select {
|
||||||
case <-tick:
|
case <-tick:
|
||||||
case <-quit:
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue