summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 20:08:26 -0500
committerCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 20:08:26 -0500
commit2c64190b19f9c139e26dbc3600a48e02749bc80d (patch)
tree80369217e816da514f30e716a3532163f6de9919
parent3b5d462058022c43a3d8afe09c1ba7f2d8c62e93 (diff)
downloadonetime-2c64190b19f9c139e26dbc3600a48e02749bc80d.tar.gz
onetime-2c64190b19f9c139e26dbc3600a48e02749bc80d.tar.bz2
onetime-2c64190b19f9c139e26dbc3600a48e02749bc80d.zip
create display type to handle writing to stdout
-rw-r--r--main.go43
1 files changed, 24 insertions, 19 deletions
diff --git a/main.go b/main.go
index 340624e..9edce04 100644
--- a/main.go
+++ b/main.go
@@ -36,18 +36,19 @@ var nameLength int
var format string
func main() {
- ps := parseStdin()
+ d := &display{ps: parseStdin()}
format = "%-" + strconv.Itoa(nameLength) + "s %s (%ds) \n"
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
+
go func() {
for {
- displayCodes(ps)
+ d.update()
time.Sleep(500 * time.Millisecond)
- clearOutput(len(ps))
+ d.clear()
}
}()
@@ -63,22 +64,6 @@ const (
keyEscape = 27
)
-func clearOutput(rows int) {
- up := []byte{keyEscape, '[', 'A'}
- clear := []byte{keyEscape, '[', '2', 'K'}
- for j := 0; j < rows; j++ {
- os.Stdout.Write(clear)
- os.Stdout.Write(up)
- }
-}
-
-func displayCodes(ps []provider) {
- for _, p := range ps {
- c, t := Code(p.secret)
- fmt.Fprintf(os.Stdout, format, p.name, c, t)
- }
-}
-
func parseStdin() []provider {
nameLength = 0
bio := bufio.NewReader(os.Stdin)
@@ -115,3 +100,23 @@ func parseStdin() []provider {
return ps
}
+
+type display struct {
+ ps []provider
+}
+
+func (d *display) update() {
+ for _, p := range d.ps {
+ c, t := Code(p.secret)
+ fmt.Fprintf(os.Stdout, format, p.name, c, t)
+ }
+}
+
+func (d *display) clear() {
+ up := []byte{keyEscape, '[', 'A'}
+ clear := []byte{keyEscape, '[', '2', 'K'}
+ for j := 0; j < len(d.ps); j++ {
+ os.Stdout.Write(clear)
+ os.Stdout.Write(up)
+ }
+}