Skip to content

Instantly share code, notes, and snippets.

@vfig
Created August 11, 2023 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfig/91440164687fa9675f648d392296358e to your computer and use it in GitHub Desktop.
Save vfig/91440164687fa9675f648d392296358e to your computer and use it in GitHub Desktop.
func print_tabular(data:Array):
# 'data' should be: [name:String, values:Array, ...]
var col_count:int = data.size()/2
var col_titles := PackedStringArray()
var col_strings:Array = []
var col_widths := PackedInt32Array()
var row_count:int = 0
for j in col_count:
var title:String = str(data[2*j])
var width:int = title.length()
col_titles.append(title)
var values:Array = data[2*j+1]
row_count = max(row_count, values.size())
var str_values := PackedStringArray()
for v in values:
var s:String = str(v)
width = max(width, s.length())
str_values.append(s)
col_strings.append(str_values)
col_widths.append(width)
var output := PackedStringArray()
for j in col_count:
output.append("%*s"%[col_widths[j], col_titles[j]])
if j<col_count-1:
output.append(" ")
else:
output.append("\n")
for i in row_count:
for j in col_count:
output.append("%*s"%[col_widths[j], col_strings[j][i]])
if j<col_count-1:
output.append(" ")
else:
output.append("\n")
print("".join(output))
@vfig
Copy link
Author

vfig commented Aug 11, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment