Skip to content

Commit 18ee603

Browse files
author
Your Name
committed
Add some level of overflow detection for TUI file lists
1 parent 24fdf0f commit 18ee603

1 file changed

Lines changed: 60 additions & 18 deletions

File tree

cecli/tui/widgets/file_list.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,19 @@ def update_files(self, chat_files):
2929
else:
3030
self.remove_class("empty")
3131

32-
# For very large numbers of files, use a summary display
33-
if total_files > 20:
34-
read_only_count = len(rel_read_only_fnames or [])
35-
stub_file_count = len(rel_read_only_stubs_fnames or [])
36-
editable_count = len([f for f in rel_fnames if f not in (rel_read_only_fnames or [])])
32+
# Get available width
33+
try:
34+
available_width = self.app.size.width
35+
except Exception:
36+
available_width = 80
3737

38-
summary = f"{editable_count} editable file(s)"
39-
if read_only_count > 0:
40-
summary += f", {read_only_count} read-only file(s)"
41-
if stub_file_count > 0:
42-
summary += f", {stub_file_count} stub file(s)"
43-
summary += " (use /ls to list all files)"
44-
self.update(summary)
45-
return
46-
47-
renderables = []
38+
# Calculate text width for each section
39+
show_readonly_summary = False
40+
show_editable_summary = False
4841

4942
# Handle read-only files
43+
ro_paths = []
5044
if rel_read_only_fnames or rel_read_only_stubs_fnames:
51-
ro_paths = []
5245
# Regular read-only files
5346
for rel_path in sorted(rel_read_only_fnames or []):
5447
ro_paths.append(rel_path)
@@ -57,16 +50,65 @@ def update_files(self, chat_files):
5750
ro_paths.append(f"{rel_path} (stub)")
5851

5952
if ro_paths:
60-
files_with_label = ["Readonly:"] + ro_paths
61-
renderables.append(Columns(files_with_label))
53+
# Calculate total characters needed for readonly section
54+
# "Readonly:" label + sum of all path lengths
55+
total_chars = len("Readonly:") + sum(len(path) for path in ro_paths)
56+
# Account for padding (12 chars total across 2 rows) and spaces between files (n-1)
57+
# Total available characters across 2 rows = available_width * 2
58+
# If total_chars > available_width * 2 - 12 - (n-1), show summary
59+
total_available = available_width * 1.5 - 12 - (len(ro_paths) - 1)
60+
61+
# If total_available is negative or zero, definitely show summary
62+
if total_available <= 0 or total_chars > total_available:
63+
show_readonly_summary = True
6264

6365
# Handle editable files
6466
editable_files = [
6567
f
6668
for f in sorted(rel_fnames)
6769
if f not in rel_read_only_fnames and f not in rel_read_only_stubs_fnames
6870
]
71+
6972
if editable_files:
73+
# Calculate total characters needed for editable section
74+
show_editable_label = bool(rel_read_only_fnames or rel_read_only_stubs_fnames)
75+
total_chars = sum(len(path) for path in editable_files)
76+
if show_editable_label:
77+
total_chars += len("Editable:")
78+
79+
# Account for padding (12 chars total across 2 rows) and spaces between files (n-1)
80+
# Total available characters across 2 rows = available_width * 2
81+
82+
total_available = available_width * 1.5 - 12 - (len(editable_files) - 1)
83+
84+
# If total_available is negative or zero, definitely show summary
85+
if total_available <= 0 or total_chars > total_available:
86+
show_editable_summary = True
87+
88+
# If either section needs summary, show overall summary
89+
if show_readonly_summary or show_editable_summary or total_files > 20:
90+
read_only_count = len(rel_read_only_fnames or [])
91+
stub_file_count = len(rel_read_only_stubs_fnames or [])
92+
editable_count = len([f for f in rel_fnames if f not in (rel_read_only_fnames or [])])
93+
94+
summary = f"{editable_count} editable file(s)"
95+
if read_only_count > 0:
96+
summary += f", {read_only_count} read-only file(s)"
97+
if stub_file_count > 0:
98+
summary += f", {stub_file_count} stub file(s)"
99+
summary += " (use /ls to list all files)"
100+
self.update(summary)
101+
return
102+
103+
renderables = []
104+
105+
# Handle read-only files
106+
if ro_paths and not show_readonly_summary:
107+
files_with_label = ["Readonly:"] + ro_paths
108+
renderables.append(Columns(files_with_label))
109+
110+
# Handle editable files
111+
if editable_files and not show_editable_summary:
70112
files_with_label = editable_files
71113
if rel_read_only_fnames or rel_read_only_stubs_fnames:
72114
files_with_label = ["Editable:"] + editable_files

0 commit comments

Comments
 (0)