Kind of stuck with this. My idea is having a function that prints a two-columns table. The first one is for keys, and it has a fixed width. The second one is for values, which may be very long strings, and its width depends on the current width of the terminal.
An example of what I'd like to have:
Key1 Value1Value1Value1Value1
Key2 Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
Value2Value2Value2Value2Value2Value2
So far, the best I achieved is to have a fixed width for the first columns, using lipgloss.
func PrintMetadata(metadata map[string]string, color string) {
style := lipgloss.NewStyle().Width(32).Foreground(lipgloss.Color(color))
for k, v := range metadata {
fmt.Println(style.Render(k) + v)
}
}
The result of which is something like:
Key1 Value1Value1Value1Value1
Key2 Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
So, how can I format a string in the way I want? I can use both standard and external libraries, so any suggestions are welcome.
I made a function for this. This function has two parameters first for the map variable for columning, second is for how many characters do you want to fill to per line. It simply change key's value contents with spacing into new variable then prints that key, value. But if you have works with unmodified value you can use unmodified variable.
This is a example output:
But please notice this, order of key and values may be different on each execution because map type is unordered when printed in for loop with key, value pair.