Skip to content

Commit e3f3b74

Browse files
committed
Implement multi-line clear for streaming output
1 parent 355d8a0 commit e3f3b74

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "autocmd-cli"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
description = "Natural language to shell command."
55
readme = "README.md"
66
requires-python = ">=3.8"

src/autocmd_cli/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,17 @@ def main() -> None:
139139
print(text, end="", flush=True, file=sys.stderr)
140140
full_response += text
141141

142-
# Clear the stderr line so the final command is clean on stdout
143-
# \r goes to start of line, \033[K clears line
142+
# Clear the streamed output
144143
if sys.stderr.isatty():
145-
print("\r\033[K", end="", file=sys.stderr, flush=True)
144+
# Count lines to move up
145+
num_lines = full_response.count('\n')
146+
# Move up num_lines, then clear to end of screen (\033[J)
147+
# \033[2K clears the current line, \033[1G moves to column 1
148+
if num_lines > 0:
149+
print(f"\033[{num_lines}A", end="", file=sys.stderr)
150+
print("\r\033[J", end="", file=sys.stderr, flush=True)
146151
else:
147-
# If not a TTY, just print a newline
152+
# If not a TTY, just print a newline to separate
148153
print("", file=sys.stderr)
149154

150155
cmd = re.sub(r'^```\w*\n?|```$', '', full_response).strip()

0 commit comments

Comments
 (0)