Skip to content

Commit

Permalink
fix: respect whitespace in shell output
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 27, 2023
1 parent 3e6d4fb commit 7f5c01a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
3 changes: 0 additions & 3 deletions gptme/tools/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ def execute_python(code: str, ask: bool) -> Generator[Message, None, None]:
else:
print("Skipping confirmation")

# remove blank lines
code = "\n".join([line for line in code.split("\n") if line.strip()])

exc = None
with redirect_stdout(io.StringIO()) as out, redirect_stderr(io.StringIO()) as err:
try:
Expand Down
25 changes: 14 additions & 11 deletions gptme/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ def run_command(self, command: str) -> tuple[int | None, str, str]:
if self.delimiter in line:
read_delimiter = True
continue
if line:
if fd == self.stdout_fd:
stdout.append(line)
elif fd == self.stderr_fd:
stderr.append(line)
if fd == self.stdout_fd:
stdout.append(line)
elif fd == self.stderr_fd:
stderr.append(line)
if read_delimiter:
break
return (
Expand Down Expand Up @@ -94,6 +93,8 @@ def execute_shell(cmd: str, ask=True) -> Generator[Message, None, None]:
cmd = cmd.strip()
if cmd.startswith("$ "):
cmd = cmd[len("$ ") :]

confirm = True
if ask:
print_preview(f"$ {cmd}", "bash")
confirm = ask_execute()
Expand Down Expand Up @@ -136,21 +137,23 @@ def _shorten_stdout(stdout: str, pre_lines=None, post_lines=None) -> str:
]
# strip dates like "2017-08-02 08:48:43 +0000 UTC"
lines = [
re.sub(
r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}( [+]\d{4})?( UTC)?", "", line
).strip()
re.sub(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}( [+]\d{4})?( UTC)?", "", line)
for line in lines
]

# strip common prefixes, useful for things like `gh runs view`
if len(lines) > 5:
prefix = os.path.commonprefix(lines)
if len(lines) >= 5:
prefix = os.path.commonprefix([line.rstrip() for line in lines])
if prefix:
lines = [line[len(prefix) :] for line in lines]

# check that if pre_lines is set, so is post_lines, and vice versa
assert (pre_lines is None) == (post_lines is None)
if pre_lines is not None and len(lines) > pre_lines + post_lines:
if (
pre_lines is not None
and post_lines is not None
and len(lines) > pre_lines + post_lines
):
lines = (
lines[:pre_lines]
+ [f"... ({len(lines) - pre_lines - post_lines} truncated) ..."]
Expand Down
33 changes: 33 additions & 0 deletions tests/test_tools_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from gptme.tools.shell import _shorten_stdout


def test_shorten_stdout_timestamp():
s = """2021-09-02T08:48:43.123Z
2021-09-02T08:48:43.123Z
"""
assert _shorten_stdout(s) == "\n\n"


def test_shorten_stdout_common_prefix():
s = """foo 1
foo 2
foo 3
foo 4
foo 5"""
assert _shorten_stdout(s) == "1\n2\n3\n4\n5"


def test_shorten_stdout_indent():
# check that indentation is preserved
s = """
l1 without indent
l2 with indent
""".strip()
assert _shorten_stdout(s) == s


def test_shorten_stdout_blanklines():
s = """l1
l2"""
assert _shorten_stdout(s) == s

0 comments on commit 7f5c01a

Please sign in to comment.