From d8bc11d3ec2187ffb4909a93eeec4aa05119c4be Mon Sep 17 00:00:00 2001 From: Alexander Luck Date: Sat, 26 Apr 2025 15:41:00 +0200 Subject: [PATCH] format_path_for_display, clamp max size of file names in a way that prioritizes file names --- aider/gui.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/aider/gui.py b/aider/gui.py index 7fa90bc38..51d6fb2a5 100755 --- a/aider/gui.py +++ b/aider/gui.py @@ -48,6 +48,52 @@ def search(text=None): return results +def format_path_for_display(path, max_len=00, separators=("/","\\")): + """ + Formats a path for display, prioritizing filename and parent directory. + Tries to show: + 1. Full path if it fits. + 2. .../parent/filename if it fits. + 3. .../filename if it fits. + 4. Simple left truncation as a fallback. + """ + if len(path) <= max_len: + return path + + sep = os.sep # Use the OS-specific separator + + try: + filename = os.path.basename(path) + dirname = os.path.dirname(path) + parent_dir = os.path.basename(dirname) + + # Try .../parent/filename + if parent_dir: + short_path = os.path.join("...", parent_dir, filename) + else: + short_path = os.path.join("...", filename) + + + if len(short_path) <= max_len: + simple_truncated = "..." + path[-(max_len - 3):] + if len(short_path) <= len(simple_truncated): + return short_path + else: + return simple_truncated + + # Try .../filename if .../parent/filename was too long or parent didn't exist + short_path_fname_only = os.path.join("...", filename) + if len(short_path_fname_only) <= max_len: + return short_path_fname_only + + # Fallback: Simple left truncation if nothing else fits + return "..." + path[-(max_len - 3):] + + except Exception: + # Safety fallback in case of weird paths + return "..." + path[-(max_len - 3):] + + # Keep state as a resource, which survives browser reloads (since Coder does too) class State: keys = set() @@ -187,10 +233,11 @@ class GUI: def do_add_files(self): fnames = st.multiselect( "Add files to the chat", - self.coder.get_all_relative_files(), + options=self.coder.get_all_relative_files(), default=self.state.initial_inchat_files, placeholder="Files to edit", disabled=self.prompt_pending(), + format_func=lambda path: format_path_for_display(path, max_len=60), help=( "Only add the files that need to be *edited* for the task you are working" " on. Aider will pull in other relevant code to provide context to the LLM." @@ -533,6 +580,38 @@ def gui_main(): }, ) + # --- Inject CSS for wider multiselect tags --- + # NOTE: These selectors target internal Streamlit/BaseWeb structures and might + # break in future Streamlit versions. Inspect element if styles don't apply. + st.markdown(""" + + """, unsafe_allow_html=True) + # config_options = st.config._config_options # for key, value in config_options.items(): # print(f"{key}: {value.value}")