Created
August 11, 2023 03:29
-
-
Save vfig/91440164687fa9675f648d392296358e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: https://i.imgur.com/K88hdB8.png