github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/ui/ui_styled.go (about) 1 package ui 2 3 import ( 4 "bufio" 5 "bytes" 6 "strings" 7 8 "github.com/mitchellh/colorstring" 9 ) 10 11 // Styled is a wrapper around an existing UI that automatically 12 // adds formatting around the UI text. 13 type Styled struct { 14 Ui 15 } 16 17 func (u *Styled) Header(msg string) { 18 u.Ui.Header(u.prefix("[bold]==> ", msg)) 19 } 20 21 func (u *Styled) Message(msg string) { 22 u.Ui.Message(u.prefix(" ", msg)) 23 } 24 25 func (u *Styled) prefix(prefix, msg string) string { 26 var buf bytes.Buffer 27 28 // We first write the color sequence (if any) of our message. 29 // This makes it so that our prefix inherits the color property 30 // of any message. 31 buf.WriteString(colorstring.ColorPrefix(msg)) 32 33 scan := bufio.NewScanner(strings.NewReader(msg)) 34 for scan.Scan() { 35 buf.WriteString(prefix) 36 buf.WriteString(scan.Text() + "\n") 37 } 38 39 str := buf.String() 40 if msg != "" { 41 str = str[:len(str)-1] 42 } 43 44 return str 45 }