Console label finder

* Pretty print and sort matching entries in a grid
This commit is contained in:
LoafyLemon 2024-05-18 21:05:45 +01:00
parent 5d80a963ae
commit 369dc3f66d

View File

@ -299,8 +299,36 @@ init 1702 python in _console:
partial = l.rest()
labels = renpy.get_all_labels()
matching = find_matching_entries(labels, partial)
raise Exception("Matching labels:\n %s" % matching)
matching_entries = find_matching_entries(labels, partial)
if not matching_entries:
return "No matching labels found."
# Sort the matching entries alphabetically
matching_entries.sort()
# Calculate the number of columns for the grid
num_columns = 3
num_rows = (len(matching_entries) + num_columns - 1) // num_columns
# Fill the grid by columns to preserve alphabetical order
grid = [[] for _ in range(num_rows)]
for i, entry in enumerate(matching_entries):
grid[i % num_rows].append(entry)
# Find the maximum width of each column
column_widths = [max(len(grid[row][col]) if col < len(grid[row]) else 0 for row in range(num_rows)) for col in range(num_columns)]
# Format the rows into a grid layout
formatted_rows = []
for row in grid:
formatted_row = []
for i, item in enumerate(row):
formatted_row.append(item.ljust(column_widths[i]))
formatted_rows.append(" | ".join(formatted_row))
matching = "\n".join(formatted_rows)
return "\n" + matching
renpy.config.at_exit_callbacks.append(console.backup)
renpy.config.stdout_callbacks.append(console.stdout_line)