Skip to content

Commit 8d52a5f

Browse files
authored
Fix csv-view path and row display behavior (#57)
* fix: handle Windows paths in csv view * Handle raw CSV paths with spaces * Resolve data test conflict and cover paths with spaces * Cover Windows csv paths with spaces
1 parent 4c6a359 commit 8d52a5f

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

tests/test_data.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,51 @@ def test_run_csv_view_short_rows_are_padded(tmp_path: Path) -> None:
6868
assert "3" in output
6969
# There should be at least one empty/padded cell visible in output
7070
assert " " in output
71+
72+
73+
@pytest.mark.skipif(sys.platform == "win32", reason="POSIX temp paths are used for this regression test")
74+
def test_run_csv_view_handles_unquoted_paths_with_spaces(tmp_path: Path) -> None:
75+
from trushell.commands.data import run_csv_view
76+
77+
directory = tmp_path / "Program Files"
78+
directory.mkdir()
79+
file_path = directory / "users.csv"
80+
file_path.write_text("ID,Name\n1,Ada\n", encoding="utf-8")
81+
82+
output = _strip_ansi(run_csv_view(str(file_path)))
83+
84+
assert "ID" in output
85+
assert "Name" in output
86+
assert "Ada" in output
87+
88+
89+
@pytest.mark.skipif(sys.platform != "win32", reason="Windows path parsing regression")
90+
def test_run_csv_view_handles_unquoted_windows_paths(tmp_path: Path) -> None:
91+
from trushell.commands.data import run_csv_view
92+
93+
file_path = tmp_path / "users.csv"
94+
rows = ["ID,Name"] + [f"{i},User {i}" for i in range(1, 52)]
95+
file_path.write_text("\n".join(rows), encoding="utf-8")
96+
97+
output = _strip_ansi(run_csv_view(str(file_path)))
98+
99+
assert "User 50" in output
100+
assert "User 51" not in output
101+
assert "...and 1 more" in output
102+
assert "rows" in output
103+
104+
105+
@pytest.mark.skipif(sys.platform != "win32", reason="Windows path with spaces regression")
106+
def test_run_csv_view_handles_windows_paths_with_spaces(tmp_path: Path) -> None:
107+
from trushell.commands.data import run_csv_view
108+
109+
directory = tmp_path / "Program Files"
110+
directory.mkdir()
111+
file_path = directory / "users.csv"
112+
file_path.write_text("ID,Name\n1,Ada\n", encoding="utf-8")
113+
114+
output = _strip_ansi(run_csv_view(str(file_path)))
115+
116+
assert "ID" in output
117+
assert "Name" in output
118+
assert "Ada" in output

trushell/commands/data.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ def _render_markup_message(message: str) -> str:
1717

1818
def run_csv_view(args: str) -> str:
1919
"""Render a CSV file as a Rich table."""
20-
arguments = shlex.split(args or "")
20+
raw_args = args or ""
21+
raw_path = raw_args.strip().strip("\"'")
22+
arguments = shlex.split(raw_args)
2123
if not arguments:
2224
return _render_markup_message("[red]Error: No file specified.[/red]")
2325

2426
file_path = Path(arguments[0]).expanduser()
27+
if not file_path.exists():
28+
candidates = []
29+
if raw_path:
30+
candidates.append(Path(raw_path).expanduser())
31+
if "\\" in raw_args:
32+
windows_arguments = shlex.split(raw_args, posix=False)
33+
if windows_arguments:
34+
candidates.append(Path(windows_arguments[0].strip("\"'")).expanduser())
35+
36+
for candidate in candidates:
37+
if candidate.exists():
38+
file_path = candidate
39+
break
40+
2541
if not file_path.exists():
2642
return _render_markup_message(f"[red]Error: File '{file_path}' not found.[/red]")
2743

0 commit comments

Comments
 (0)