github.com/nozzle/golangci-lint@v1.49.0-nz3/test/testdata/govet_custom_formatter.go (about) 1 //golangcitest:args -Egovet 2 //golangcitest:expected_exitcode 0 3 package main 4 5 import ( 6 "bytes" 7 "fmt" 8 "os" 9 ) 10 11 const ( 12 escape = "\x1b" 13 reset = escape + "[0m" 14 green = escape + "[32m" 15 16 minValue = 0.0 17 maxValue = 1.0 18 ) 19 20 // Bar is a progress bar. 21 type Bar float64 22 23 var _ fmt.Formatter = Bar(maxValue) 24 25 // Format the progress bar as output 26 func (h Bar) Format(state fmt.State, r rune) { 27 switch r { 28 case 'r': 29 default: 30 panic(fmt.Sprintf("%v: unexpected format character", float64(h))) 31 } 32 33 if h > maxValue { 34 h = maxValue 35 } 36 37 if h < minValue { 38 h = minValue 39 } 40 41 if state.Flag('-') { 42 h = maxValue - h 43 } 44 45 width, ok := state.Width() 46 if !ok { 47 // default width of 40 48 width = 40 49 } 50 51 var pad int 52 53 extra := len([]byte(green)) + len([]byte(reset)) 54 55 p := make([]byte, width+extra) 56 p[0], p[len(p)-1] = '|', '|' 57 pad += 2 58 59 positive := int(Bar(width-pad) * h) 60 negative := width - pad - positive 61 62 n := 1 63 n += copy(p[n:], green) 64 n += copy(p[n:], bytes.Repeat([]byte("+"), positive)) 65 n += copy(p[n:], reset) 66 67 if negative > 0 { 68 copy(p[n:len(p)-1], bytes.Repeat([]byte("-"), negative)) 69 } 70 71 _, _ = state.Write(p) 72 } 73 74 func main() { 75 var b Bar = 0.9 76 _, _ = fmt.Fprintf(os.Stdout, "%r", b) 77 }