forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
54 lines (45 loc) · 969 Bytes
/
status.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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
// Package status prints status messages to a console, overwriting previous values.
package status
import (
"fmt"
"time"
)
const (
clearLine = "\x1b[2K\r"
Rate = 100 * time.Millisecond
)
var (
lastTime time.Time
lastFormat string
lastArgs []interface{}
)
func Clear() {
fmt.Print(clearLine)
reset(time.Time{})
}
func WillPrint() bool {
return time.Now().Sub(lastTime) >= Rate
}
func Printf(format string, args ...interface{}) {
now := time.Now()
if now.Sub(lastTime) < Rate {
lastFormat, lastArgs = format, args
} else {
fmt.Printf(clearLine+format, args...)
reset(now)
}
}
func Done() {
if lastArgs != nil {
fmt.Printf(clearLine+lastFormat, lastArgs...)
}
fmt.Println()
reset(time.Time{})
}
func reset(time time.Time) {
lastTime = time
lastFormat, lastArgs = "", nil
}