forked from gmelodie/cclock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cclock.go
142 lines (118 loc) · 2.88 KB
/
cclock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"log"
"math"
"os"
"strconv"
"time"
"github.com/k0kubun/go-ansi"
"github.com/urfave/cli"
)
type normalTime struct {
nanoseconds int
seconds int
minutes int
hours int
}
type centhTime struct {
centhconds int
centhutes int
centhours int
}
// Default value of cs (in seconds)
const secToCSVal = 2.777778
const csToNanoSec = 359999970
const csToSecVal = csToNanoSec / 1e9
func toCenth(n normalTime) centhTime {
totalsecs := float64(n.hours*3600+n.minutes*60+n.seconds) + float64(n.nanoseconds)/1e9
totalcs := int(math.Round(totalsecs * secToCSVal))
res := centhTime{}
res.centhours = totalcs / 10000
totalcs = totalcs - 10000*res.centhours
res.centhutes = totalcs / 100
totalcs = totalcs - 100*res.centhutes
res.centhconds = totalcs
return res
}
func toNormal(c centhTime) normalTime {
totalcs := c.centhours*10000 + c.centhutes*100 + c.centhconds
totalsecs := int(math.Round(float64(totalcs) * csToSecVal))
res := normalTime{}
res.hours = totalsecs / 3600
totalsecs = totalsecs - 3600*res.hours
res.minutes = totalsecs / 60
totalsecs = totalsecs - 60*res.minutes
res.seconds = totalsecs
return res
}
func gotimeToNormalTime(t time.Time) normalTime {
return normalTime{
nanoseconds: t.Nanosecond(),
seconds: t.Second(),
minutes: t.Minute(),
hours: t.Hour(),
}
}
func convertAndPrintSummary(normal normalTime) {
centh := toCenth(normal)
fmt.Printf("Normal Time:\t%2dh\t%02dmin\t%02ds\n", normal.hours, normal.minutes, normal.seconds)
fmt.Printf("CTime:\t\t%2dch\t%02dct\t%02dcs\n", centh.centhours, centh.centhutes, centh.centhconds)
}
func moveCursorUp(lines uint) {
ansi.Printf("\u001b[%dA", lines)
}
func main() {
var app = cli.NewApp()
app.Name = "cclock"
app.Usage = "cclock's command-line interface"
app.Version = "0.4"
app.Commands = []cli.Command{
{
Name: "clock",
Action: func(c *cli.Context) error {
fmt.Print("\n\n") // Ensure that cursor does not move into previous text in first iteration
timeChannel := time.Tick(csToNanoSec)
for {
now := <-timeChannel
moveCursorUp(2)
n := gotimeToNormalTime(now)
convertAndPrintSummary(n)
}
return nil
},
},
{
Name: "now",
Usage: "Show current time (decimal and normal)",
Action: func(c *cli.Context) error {
n := gotimeToNormalTime(time.Now())
convertAndPrintSummary(n)
return nil
},
},
}
app.Action = func(c *cli.Context) error {
n := normalTime{}
// Print usage on wrong arguments
if c.NArg() == 0 || c.NArg() > 3 {
cli.ShowAppHelp(c)
return nil
}
if c.NArg() >= 1 {
n.hours, _ = strconv.Atoi(c.Args().Get(0))
}
if c.NArg() >= 2 {
n.minutes, _ = strconv.Atoi(c.Args().Get(1))
}
if c.NArg() == 3 {
n.seconds, _ = strconv.Atoi(c.Args().Get(2))
}
convertAndPrintSummary(n)
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}