github.com/ndau/noms@v1.0.5/go/util/status/status.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 // Package status prints status messages to a console, overwriting previous values. 6 package status 7 8 import ( 9 "fmt" 10 "time" 11 ) 12 13 const ( 14 clearLine = "\x1b[2K\r" 15 Rate = 100 * time.Millisecond 16 ) 17 18 var ( 19 lastTime time.Time 20 lastFormat string 21 lastArgs []interface{} 22 ) 23 24 func Clear() { 25 fmt.Print(clearLine) 26 reset(time.Time{}) 27 } 28 29 func WillPrint() bool { 30 return time.Now().Sub(lastTime) >= Rate 31 } 32 33 func Printf(format string, args ...interface{}) { 34 now := time.Now() 35 if now.Sub(lastTime) < Rate { 36 lastFormat, lastArgs = format, args 37 } else { 38 fmt.Printf(clearLine+format, args...) 39 reset(now) 40 } 41 } 42 43 func Done() { 44 if lastArgs != nil { 45 fmt.Printf(clearLine+lastFormat, lastArgs...) 46 } 47 fmt.Println() 48 reset(time.Time{}) 49 } 50 51 func reset(time time.Time) { 52 lastTime = time 53 lastFormat, lastArgs = "", nil 54 }