Skip to content

Commit 69b83fd

Browse files
committed
feat(cli): Regroup path lists by common prefix
This is not perfect, since one outlier will prevent any common prefix from being extracted - but doing anything more complicated starts involving heavy and complex math which is not worth it for just nicely displaying things.
1 parent 98bf60c commit 69b83fd

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/dda/cli/info/go/tags/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,29 @@ def cmd(app: Application, *, repo: Path, json: bool, map: bool, exclude: list[st
6767

6868
# Order the tags by most-used first (by "count"), descending
6969
result_human = OrderedDict([
70-
(tag, {"count": len(result_map[tag]), "paths": ", ".join(result_map[tag])}) for tag in result_list
70+
(tag, {"count": len(result_map[tag]), "paths": _regroup_paths_by_longest_common_prefix(result_map[tag])})
71+
for tag in result_list
7172
])
7273
app.display_table(result_human)
7374
return
75+
76+
77+
def _regroup_paths_by_longest_common_prefix(paths: list[str]) -> str:
78+
if len(paths) == 0:
79+
return ""
80+
81+
if len(paths) == 1:
82+
return paths[0]
83+
84+
import os
85+
86+
# Use commonpath to get the deepest common directory
87+
common_dir = os.path.commonpath(paths)
88+
if not common_dir:
89+
return ", ".join(paths)
90+
91+
# Extract just the filenames (or remaining path parts) after the common directory
92+
remaining_parts = [os.path.relpath(path, common_dir) for path in paths]
93+
94+
# Return `common_dir/{file1, file2, ...}`
95+
return f"{common_dir}/{{{', '.join(remaining_parts)}}}"

0 commit comments

Comments
 (0)