summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 20:36:49 -0500
committerCasey Robinson <casey.robinson@distilnetworks.com>2014-12-18 20:37:12 -0500
commitb86980e5641f9568b3abc29423241c7c09b8e2cf (patch)
tree7730a0793018230837c12ca6182ec18c74c47ba3
parent2c64190b19f9c139e26dbc3600a48e02749bc80d (diff)
downloadonetime-b86980e5641f9568b3abc29423241c7c09b8e2cf.tar.gz
onetime-b86980e5641f9568b3abc29423241c7c09b8e2cf.tar.bz2
onetime-b86980e5641f9568b3abc29423241c7c09b8e2cf.zip
use functional option to create new display
-rw-r--r--main.go39
1 files changed, 28 insertions, 11 deletions
diff --git a/main.go b/main.go
index 9edce04..7d0c583 100644
--- a/main.go
+++ b/main.go
@@ -32,23 +32,26 @@ func newProvider(line string) *provider {
return p
}
-var nameLength int
var format string
func main() {
- d := &display{ps: parseStdin()}
-
- format = "%-" + strconv.Itoa(nameLength) + "s %s (%ds) \n"
+ disp := newDisplay(func(d *display) {
+ var namelen int
+ d.ps, namelen = parseStdin()
+ d.format.prefix = "%-"
+ d.format.suffix = "s %s (%ds) "
+ d.format.line = d.format.prefix + strconv.Itoa(namelen) + d.format.suffix + "\n"
+ d.lines = make([][]byte, namelen + len(d.format.prefix) + len(d.format.suffix))
+ })
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
-
go func() {
for {
- d.update()
+ disp.update()
time.Sleep(500 * time.Millisecond)
- d.clear()
+ disp.clear()
}
}()
@@ -64,8 +67,8 @@ const (
keyEscape = 27
)
-func parseStdin() []provider {
- nameLength = 0
+func parseStdin() ([]provider, int) {
+ nameLength := 0
bio := bufio.NewReader(os.Stdin)
var ps []provider
@@ -98,17 +101,31 @@ func parseStdin() []provider {
nameLength++
- return ps
+ return ps, nameLength
}
type display struct {
ps []provider
+ lines [][]byte
+ format struct {
+ prefix string
+ suffix string
+ line string
+ }
+}
+
+func newDisplay(opts ...func(*display)) *display {
+ d := display{}
+ for _, opt := range opts {
+ opt(&d)
+ }
+ return &d
}
func (d *display) update() {
for _, p := range d.ps {
c, t := Code(p.secret)
- fmt.Fprintf(os.Stdout, format, p.name, c, t)
+ fmt.Fprintf(os.Stdout, d.format.line, p.name, c, t)
}
}