summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 21:26:58 -0500
committerCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 21:26:58 -0500
commit8cbd88aba3809eb48db665cb76e212960da96cbb (patch)
treefd164035f28feaf08670c0d3b8542bb4e50e841a
parentd32d8e9111d0df88ad119dc6372f9eea093688f0 (diff)
downloadonetime-8cbd88aba3809eb48db665cb76e212960da96cbb.tar.gz
onetime-8cbd88aba3809eb48db665cb76e212960da96cbb.tar.bz2
onetime-8cbd88aba3809eb48db665cb76e212960da96cbb.zip
only update time. avoids reprinting whole code every secondHEADmaster
-rw-r--r--main.go31
1 files changed, 18 insertions, 13 deletions
diff --git a/main.go b/main.go
index 517772c..070d5ad 100644
--- a/main.go
+++ b/main.go
@@ -41,6 +41,7 @@ func main() {
d.format.prefix = "%-"
d.format.suffix = "s %s (%ds) "
d.format.line = d.format.prefix + strconv.Itoa(namelen) + d.format.suffix + "\n"
+ d.format.skipLength = len(d.format.prefix) + namelen + 6
})
sigChan := make(chan os.Signal, 1)
@@ -96,9 +97,10 @@ func parseStdin() ([]provider, int) {
type display struct {
ps []provider
format struct {
- prefix string
- suffix string
- line string
+ prefix string
+ suffix string
+ line string
+ skipLength int
}
}
@@ -111,9 +113,18 @@ func newDisplay(opts ...func(*display)) *display {
}
func (d *display) update() {
+ for j := 0; j < len(d.ps); j++ {
+ os.Stdout.Write(up)
+ }
for _, p := range d.ps {
+ fmt.Fprintf(os.Stdout, "\r")
c, t := Code(p.secret)
- fmt.Fprintf(os.Stdout, d.format.line, p.name, c, t)
+ if t == period {
+ fmt.Fprintf(os.Stdout, d.format.line, p.name, c, t)
+ } else {
+ fmt.Fprintf(os.Stdout, "%s[%dC(%ds) ", esc, d.format.skipLength, t)
+ os.Stdout.Write(down)
+ }
}
}
@@ -123,16 +134,11 @@ const (
var (
up = []byte{keyEscape, '[', 'A'}
+ down = []byte{keyEscape, '[', 'B'}
clear = []byte{keyEscape, '[', '2', 'K'}
+ esc = []byte{keyEscape}
)
-func (d *display) clear() {
- for j := 0; j < len(d.ps); j++ {
- os.Stdout.Write(clear)
- os.Stdout.Write(up)
- }
-}
-
func (d *display) fullRender() {
for _, p := range d.ps {
c, t := Code(p.secret)
@@ -144,8 +150,7 @@ func (d *display) run() {
d.fullRender()
go func() {
for {
- time.Sleep(500 * time.Millisecond)
- d.clear()
+ time.Sleep(1000 * time.Millisecond)
d.update()
}
}()