github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/util/printer.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "io" 6 "text/tabwriter" 7 8 "github.com/fatih/color" 9 ) 10 11 const ( 12 noType = "" 13 okType = "[OK]" 14 errType = "[ERROR]" 15 skippedType = "[SKIPPED]" 16 warnType = "[WARNING]" 17 unreachableType = "[UNREACHABLE]" 18 errIgnoredType = "[ERROR IGNORED]" 19 ) 20 21 var Green = color.New(color.FgGreen) 22 var Red = color.New(color.FgRed) 23 var Orange = color.New(color.FgRed, color.FgYellow) 24 var Blue = color.New(color.FgCyan) 25 var White = color.New(color.FgHiWhite) 26 27 // PrettyPrintOk [OK](Green) with formatted string 28 func PrettyPrintOk(out io.Writer, msg string, a ...interface{}) { 29 print(out, msg, okType, a...) 30 } 31 32 // PrettyPrintErr [ERROR](Red) with formatted string 33 func PrettyPrintErr(out io.Writer, msg string, a ...interface{}) { 34 print(out, msg, errType, a...) 35 } 36 37 // PrettyPrint no type will be displayed, used for just single line printing 38 func PrettyPrint(out io.Writer, msg string, a ...interface{}) { 39 print(out, msg+"\t", noType, a...) 40 } 41 42 // PrettyPrintWarn [WARNING](Orange) with formatted string 43 func PrettyPrintWarn(out io.Writer, msg string, a ...interface{}) { 44 print(out, msg, warnType, a...) 45 } 46 47 // PrettyPrintErrorIgnored [ERROR IGNORED](Red) with formatted string 48 func PrettyPrintErrorIgnored(out io.Writer, msg string, a ...interface{}) { 49 print(out, msg, errIgnoredType, a...) 50 } 51 52 // PrettyPrintUnreachable [UNREACHABLE](Red) with formatted string 53 func PrettyPrintUnreachable(out io.Writer, msg string, a ...interface{}) { 54 print(out, msg, unreachableType, a...) 55 } 56 57 // PrettyPrintSkipped [SKIPPED](blue) with formatted string 58 func PrettyPrintSkipped(out io.Writer, msg string, a ...interface{}) { 59 print(out, msg, skippedType, a...) 60 } 61 62 // PrintOk print whole message in green(Red) format 63 func PrintOk(out io.Writer) { 64 PrintColor(out, Green, okType) 65 } 66 67 // PrintOkln print whole message in green(Red) format 68 func PrintOkln(out io.Writer) { 69 PrintColor(out, Green, okType+"\n") 70 } 71 72 // PrintError print whole message in error(Red) format 73 func PrintError(out io.Writer) { 74 PrintColor(out, Red, errType) 75 } 76 77 // PrintWarn print whole message in warn(Orange) format 78 func PrintWarn(out io.Writer) { 79 PrintColor(out, Orange, warnType) 80 } 81 82 // PrintSkipped print whole message in green(Red) format 83 func PrintSkipped(out io.Writer) { 84 PrintColor(out, Blue, skippedType) 85 } 86 87 // PrintHeader will print header with predifined width 88 func PrintHeader(out io.Writer, msg string, padding byte) { 89 w := tabwriter.NewWriter(out, 84, 0, 0, padding, 0) 90 fmt.Fprintln(w, "") 91 format := msg + "\t\n" 92 fmt.Fprintf(w, format) 93 w.Flush() 94 } 95 96 func PrintTable(out io.Writer, msgMap map[string][]string) { 97 w := tabwriter.NewWriter(out, 84, 0, 0, ' ', 0) 98 for k, v := range msgMap { 99 format := fmt.Sprintf("- %s %s", k, v) + "\t\n" 100 fmt.Fprintf(w, format) 101 } 102 w.Flush() 103 } 104 105 // PrintColor prints text in color 106 func PrintColor(out io.Writer, clr *color.Color, msg string, a ...interface{}) { 107 // Remove any newline, results in only one \n 108 line := fmt.Sprintf("%s", clr.SprintfFunc()(msg, a...)) 109 fmt.Fprint(out, line) 110 } 111 112 func print(out io.Writer, msg, status string, a ...interface{}) { 113 w := tabwriter.NewWriter(out, 80, 0, 0, ' ', 0) 114 // print message 115 format := msg + "\t" 116 fmt.Fprintf(w, format, a...) 117 118 // print status 119 if status != noType { 120 // get correct color 121 var clr *color.Color 122 switch status { 123 case okType: 124 clr = Green 125 case errType, unreachableType: 126 clr = Red 127 case warnType, errIgnoredType: 128 clr = Orange 129 case skippedType: 130 clr = Blue 131 } 132 133 sformat := "%s\n" 134 fmt.Fprintf(w, sformat, clr.SprintFunc()(status)) 135 136 } 137 w.Flush() 138 } 139 140 // PrintValidationErrors loops through the errors 141 func PrintValidationErrors(out io.Writer, errors []error) { 142 for _, err := range errors { 143 PrintColor(out, Red, "- %v\n", err) 144 } 145 }