github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/genall/help/pretty/help.go (about) 1 package pretty 2 3 import ( 4 "fmt" 5 "io" 6 7 "sigs.k8s.io/controller-tools/pkg/genall/help" 8 9 "github.com/fatih/color" 10 ) 11 12 var ( 13 headingStyle = Decoration(*color.New(color.Bold, color.Underline)) 14 markerNameStyle = Decoration(*color.New(color.Bold)) 15 fieldSummaryStyle = Decoration(*color.New(color.FgGreen, color.Italic)) 16 markerTargetStyle = Decoration(*color.New(color.Faint)) 17 fieldDetailStyle = Decoration(*color.New(color.Italic, color.FgGreen)) 18 deprecatedStyle = Decoration(*color.New(color.CrossedOut)) 19 ) 20 21 // MarkersSummary returns a condensed summary of help for the given markers. 22 func MarkersSummary(groupName string, markers []help.MarkerDoc) Span { 23 out := new(SpanWriter) 24 25 out.Print(Text("\n")) 26 out.Print(headingStyle.Containing(Text(groupName))) 27 out.Print(Text("\n\n")) 28 29 table := &Table{Sizing: &TableCalculator{Padding: 2}} 30 for _, marker := range markers { 31 table.StartRow() 32 table.Column(MarkerSyntaxHelp(marker)) 33 table.Column(markerTargetStyle.Containing(Text(marker.Target))) 34 35 summary := new(SpanWriter) 36 if marker.DeprecatedInFavorOf != nil && len(*marker.DeprecatedInFavorOf) > 0 { 37 summary.Print(markerNameStyle.Containing(Text("(use "))) 38 summary.Print(markerNameStyle.Containing(Text(*marker.DeprecatedInFavorOf))) 39 summary.Print(markerNameStyle.Containing(Text(") "))) 40 } 41 summary.Print(Text(marker.Summary)) 42 table.Column(summary) 43 44 table.EndRow() 45 } 46 out.Print(table) 47 48 out.Print(Text("\n")) 49 50 return out 51 } 52 53 // MarkersDetails returns detailed help for the given markers, including detailed field help. 54 func MarkersDetails(fullDetail bool, groupName string, markers []help.MarkerDoc) Span { 55 out := new(SpanWriter) 56 57 out.Print(Line(headingStyle.Containing(Text(groupName)))) 58 out.Print(Newlines(2)) 59 60 for _, marker := range markers { 61 out.Print(Line(markerName(marker))) 62 out.Print(Text(" ")) 63 out.Print(markerTargetStyle.Containing(Text(marker.Target))) 64 65 summary := new(SpanWriter) 66 if marker.DeprecatedInFavorOf != nil && len(*marker.DeprecatedInFavorOf) > 0 { 67 summary.Print(markerNameStyle.Containing(Text("(use "))) 68 summary.Print(markerNameStyle.Containing(Text(*marker.DeprecatedInFavorOf))) 69 summary.Print(markerNameStyle.Containing(Text(") "))) 70 } 71 summary.Print(Text(marker.Summary)) 72 73 if !marker.AnonymousField() { 74 out.Print(Indented(1, Line(summary))) 75 if len(marker.Details) > 0 && fullDetail { 76 out.Print(Indented(1, Line(Text(marker.Details)))) 77 } 78 } 79 80 if marker.AnonymousField() { 81 out.Print(Indented(1, Line(fieldDetailStyle.Containing(FieldSyntaxHelp(marker.Fields[0]))))) 82 out.Print(Text(" ")) 83 out.Print(summary) 84 if len(marker.Details) > 0 && fullDetail { 85 out.Print(Indented(2, Line(Text(marker.Details)))) 86 } 87 out.Print(Newlines(1)) 88 } else if !marker.Empty() { 89 out.Print(Newlines(1)) 90 if fullDetail { 91 for _, arg := range marker.Fields { 92 out.Print(Indented(1, Line(fieldDetailStyle.Containing(FieldSyntaxHelp(arg))))) 93 out.Print(Indented(2, Line(Text(arg.Summary)))) 94 if len(arg.Details) > 0 && fullDetail { 95 out.Print(Indented(2, Line(Text(arg.Details)))) 96 out.Print(Newlines(1)) 97 } 98 } 99 out.Print(Newlines(1)) 100 } else { 101 table := &Table{Sizing: &TableCalculator{Padding: 2}} 102 for _, arg := range marker.Fields { 103 table.StartRow() 104 table.Column(fieldDetailStyle.Containing(FieldSyntaxHelp(arg))) 105 table.Column(Text(arg.Summary)) 106 table.EndRow() 107 } 108 109 out.Print(Indented(1, table)) 110 } 111 } else { 112 out.Print(Newlines(1)) 113 } 114 } 115 116 return out 117 } 118 119 func FieldSyntaxHelp(arg help.FieldHelp) Span { 120 return fieldSyntaxHelp(arg, "") 121 } 122 123 // fieldSyntaxHelp prints the syntax help for a particular marker argument. 124 func fieldSyntaxHelp(arg help.FieldHelp, sep string) Span { 125 if arg.Optional { 126 return FromWriter(func(out io.Writer) error { 127 _, err := fmt.Fprintf(out, "[%s%s=<%s>]", sep, arg.Name, arg.TypeString()) 128 return err 129 }) 130 } 131 return FromWriter(func(out io.Writer) error { 132 _, err := fmt.Fprintf(out, "%s%s=<%s>", sep, arg.Name, arg.TypeString()) 133 return err 134 }) 135 } 136 137 // markerName returns a span containing just the appropriately-formatted marker name. 138 func markerName(def help.MarkerDoc) Span { 139 if def.DeprecatedInFavorOf != nil { 140 return deprecatedStyle.Containing(Text("+" + def.Name)) 141 } 142 return markerNameStyle.Containing(Text("+" + def.Name)) 143 } 144 145 // MarkerSyntaxHelp assembles syntax help for a given marker. 146 func MarkerSyntaxHelp(def help.MarkerDoc) Span { 147 out := new(SpanWriter) 148 149 out.Print(markerName(def)) 150 151 if def.Empty() { 152 return out 153 } 154 155 sep := ":" 156 if def.AnonymousField() { 157 sep = "" 158 } 159 160 fieldStyle := fieldSummaryStyle 161 if def.DeprecatedInFavorOf != nil { 162 fieldStyle = deprecatedStyle 163 } 164 165 for _, arg := range def.Fields { 166 out.Print(fieldStyle.Containing(fieldSyntaxHelp(arg, sep))) 167 sep = "," 168 } 169 170 return out 171 }