| 1 | package formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/colorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/color`/" |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/" |
| 9 | ) |
| 10 | |
| 11 | // ConsoleTreeRenderer renders a ContainerNode tree into an ansi-colored console string. |
| 12 | type ConsoleTreeRendererscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer# struct { |
| 13 | WithValuesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#WithValues. bool |
| 14 | } |
| 15 | |
| 16 | // Constants for console rendering. |
| 17 | const ( |
| 18 | DescriptionColumnWidthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DescriptionColumnWidth. = 60 // Width of the description column in console output |
| 19 | ItemSeparatorLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ItemSeparatorLength. = 2 // Length of the ", " separator between container items |
| 20 | DefaultValueOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DefaultValueOffset. = 16 // Minimum offset before description when no values are shown |
| 21 | ValueOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueOffset. = 30 // Offset before description when values are shown |
| 22 | ContainerBracketLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerBracketLength. = 4 // Length of container delimiters (e.g., "{ }" or "[ ]") |
| 23 | KeySeparatorLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/KeySeparatorLength. = 2 // Length of the ": " separator after a key in containers |
| 24 | ) |
| 25 | |
| 26 | // RenderTree renders a ContainerNode tree into an ansi-colored console string. |
| 27 | func (r ConsoleTreeRendererscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#) RenderTreescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#RenderTree().(root *ContainerNodescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#, _ string) string { |
| 28 | stringBuilder := stringsscip-go gomod github.com/golang/go/src . strings/.Builderscip-go gomod github.com/golang/go/src . strings/Builder#{} |
| 29 | |
| 30 | for _, node := range root.Itemsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#Items. { |
| 31 | fieldKey := node.Fieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/Node#Field.().Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Name. |
| 32 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(fieldKey) |
| 33 | |
| 34 | for i := len(fieldKey); i <= root.MaxKeyLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#MaxKeyLength.; i++ { |
| 35 | stringBuilder.WriteRunescip-go gomod github.com/golang/go/src . strings/Builder#WriteRune().(' ') |
| 36 | } |
| 37 | |
| 38 | var valueLen int // Initialize without assignment; set later |
| 39 | |
| 40 | preLen := DefaultValueOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DefaultValueOffset. // Default spacing before the description when no values are rendered |
| 41 | |
| 42 | field := node.Fieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/Node#Field.() |
| 43 | |
| 44 | if r.WithValuesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#WithValues. { |
| 45 | preLen = ValueOffsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueOffset. // Adjusts the spacing when values are included |
| 46 | valueLen = r.writeNodeValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#writeNodeValue().(&stringBuilder, node) |
| 47 | } else { |
| 48 | // Since no values were supplied, substitute the value with the type |
| 49 | typeName := field.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Type..Stringscip-go gomod github.com/golang/go/src . reflect/Type#String().() |
| 50 | |
| 51 | // If the value is an enum type, providing the name is a bit pointless |
| 52 | // Instead, use a common string "option" to signify the type |
| 53 | if field.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. != nil { |
| 54 | typeName = "option" |
| 55 | } |
| 56 | |
| 57 | valueLen = len(typeName) |
| 58 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(colorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/color`/.CyanStringscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/color`/CyanString().(typeName)) |
| 59 | } |
| 60 | |
| 61 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(stringsscip-go gomod github.com/golang/go/src . strings/.Repeatscip-go gomod github.com/golang/go/src . strings/Repeat().(" ", utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/.Maxscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/Max().(preLen-valueLen, 1))) |
| 62 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeDescscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeDesc.(field.Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Description.)) |
| 63 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().( |
| 64 | stringsscip-go gomod github.com/golang/go/src . strings/.Repeatscip-go gomod github.com/golang/go/src . strings/Repeat().(" ", utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/.Maxscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/Max().(DescriptionColumnWidthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/DescriptionColumnWidth.-len(field.Descriptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Description.), 1)), |
| 65 | ) |
| 66 | |
| 67 | if len(field.URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts.) > 0 && field.URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts.[0] != URLQueryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/URLQuery. { |
| 68 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(" <URL: ") |
| 69 | |
| 70 | for i, part := range field.URLPartsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#URLParts. { |
| 71 | if i > 0 { |
| 72 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(", ") |
| 73 | } |
| 74 | |
| 75 | if part > URLPathscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/URLPath. { |
| 76 | part = URLPathscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/URLPath. |
| 77 | } |
| 78 | |
| 79 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeEnumscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeEnum.(part)) |
| 80 | } |
| 81 | |
| 82 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(">") |
| 83 | } |
| 84 | |
| 85 | if field.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Template. != "" { |
| 86 | fmtscip-go gomod github.com/golang/go/src . fmt/.Fprintfscip-go gomod github.com/golang/go/src . fmt/Fprintf().(&stringBuilder, " <Template: %s>", ColorizeStringscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeString.(field.Templatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Template.)) |
| 87 | } |
| 88 | |
| 89 | if field.DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue. != "" { |
| 90 | fmtscip-go gomod github.com/golang/go/src . fmt/.Fprintfscip-go gomod github.com/golang/go/src . fmt/Fprintf().(&stringBuilder, " <Default: %s>", |
| 91 | ColorizeValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeValue().(field.DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue., field.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. != nil)) |
| 92 | } |
| 93 | |
| 94 | if field.Requiredscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Required. { |
| 95 | fmtscip-go gomod github.com/golang/go/src . fmt/.Fprintfscip-go gomod github.com/golang/go/src . fmt/Fprintf().(&stringBuilder, " <%s>", ColorizeFalsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeFalse.("Required")) |
| 96 | } |
| 97 | |
| 98 | if len(field.Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys.) > 1 { |
| 99 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(" <Aliases: ") |
| 100 | |
| 101 | for i, key := range field.Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys. { |
| 102 | if i == 0 { |
| 103 | // Skip primary alias (as it's the same as the field name) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | if i > 1 { |
| 108 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(", ") |
| 109 | } |
| 110 | |
| 111 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeStringscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeString.(key)) |
| 112 | } |
| 113 | |
| 114 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(">") |
| 115 | } |
| 116 | |
| 117 | if field.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter. != nil { |
| 118 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeContainerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeContainer.(" [")) |
| 119 | |
| 120 | for i, name := range field.EnumFormatterscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#EnumFormatter..Namesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/EnumFormatter#Names.() { |
| 121 | if i != 0 { |
| 122 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(", ") |
| 123 | } |
| 124 | |
| 125 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeEnumscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeEnum.(name)) |
| 126 | } |
| 127 | |
| 128 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeContainerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeContainer.("]")) |
| 129 | } |
| 130 | |
| 131 | stringBuilder.WriteRunescip-go gomod github.com/golang/go/src . strings/Builder#WriteRune().('\n') |
| 132 | } |
| 133 | |
| 134 | return stringBuilder.Stringscip-go gomod github.com/golang/go/src . strings/Builder#String().() |
| 135 | } |
| 136 | |
| 137 | func (r ConsoleTreeRendererscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#) writeContainerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#writeContainer().( |
| 138 | stringBuilder *stringsscip-go gomod github.com/golang/go/src . strings/.Builderscip-go gomod github.com/golang/go/src . strings/Builder#, |
| 139 | node *ContainerNodescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#, |
| 140 | ) int { |
| 141 | kind := node.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Type..Kindscip-go gomod github.com/golang/go/src . reflect/Type#Kind().() |
| 142 | hasKeys := !utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/.IsCollectionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/IsCollection().(kind) |
| 143 | |
| 144 | totalLen := ContainerBracketLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerBracketLength. // Length of the opening and closing brackets ({ } or [ ]) |
| 145 | |
| 146 | if hasKeys { |
| 147 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("{ ") |
| 148 | } else { |
| 149 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().("[ ") |
| 150 | } |
| 151 | |
| 152 | for i, itemNode := range node.Itemsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#Items. { |
| 153 | if i != 0 { |
| 154 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(", ") |
| 155 | |
| 156 | totalLen += KeySeparatorLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/KeySeparatorLength. // Accounts for the : separator between keys and values in containers |
| 157 | } |
| 158 | |
| 159 | if hasKeys { |
| 160 | itemKey := itemNode.Fieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/Node#Field.().Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Name. |
| 161 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(itemKey) |
| 162 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(": ") |
| 163 | |
| 164 | totalLen += len(itemKey) + ItemSeparatorLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ItemSeparatorLength. |
| 165 | } |
| 166 | |
| 167 | valLen := r.writeNodeValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#writeNodeValue().(stringBuilder, itemNode) |
| 168 | totalLen += valLen |
| 169 | } |
| 170 | |
| 171 | if hasKeys { |
| 172 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(" }") |
| 173 | } else { |
| 174 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(" ]") |
| 175 | } |
| 176 | |
| 177 | return totalLen |
| 178 | } |
| 179 | |
| 180 | func (r ConsoleTreeRendererscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#) writeNodeValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#writeNodeValue().(stringBuilder *stringsscip-go gomod github.com/golang/go/src . strings/.Builderscip-go gomod github.com/golang/go/src . strings/Builder#, node Nodescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/Node#) int { |
| 181 | if contNode, isContainer := node.(*ContainerNodescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#); isContainer { |
| 182 | return r.writeContainerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ConsoleTreeRenderer#writeContainer().(stringBuilder, contNode) |
| 183 | } |
| 184 | |
| 185 | if valNode, isValue := node.(*ValueNodescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueNode#); isValue { |
| 186 | stringBuilder.WriteStringscip-go gomod github.com/golang/go/src . strings/Builder#WriteString().(ColorizeTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ColorizeToken().(valNode.Valuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueNode#Value., valNode.tokenTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueNode#tokenType.)) |
| 187 | |
| 188 | return len(valNode.Valuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ValueNode#Value.) |
| 189 | } |
| 190 | |
| 191 | stringBuilder.WriteBytescip-go gomod github.com/golang/go/src . strings/Builder#WriteByte().('?') |
| 192 | |
| 193 | return 1 |
| 194 | } |