Skip to content

Commit ab024ca

Browse files
committed
Refactor md2term.py to remove --stream option and unify input processing
Eliminate the --stream flag from the CLI, simplifying the input handling logic. Update the main function to use a unified renderer for both file and stdin inputs, ensuring consistent character-by-character processing with intelligent backtracking for incomplete markdown syntax. Adjust documentation to reflect these changes.
1 parent b142cfe commit ab024ca

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

md2term.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,8 @@ def process_character_stream(input_stream: TextIO, width: Optional[int] = None)
424424
@click.command()
425425
@click.argument("input_file", type=click.File("r"), required=False)
426426
@click.option("--width", "-w", type=int, help="Override terminal width")
427-
@click.option("--stream", "-s", is_flag=True, help="Enable character-by-character streaming mode with backtracking")
428427
@click.version_option(version=__version__)
429-
def main(input_file: Optional[TextIO], width: Optional[int], stream: bool) -> None:
428+
def main(input_file: Optional[TextIO], width: Optional[int]) -> None:
430429
"""
431430
Parse Markdown and turn it into nicely-formatted text for terminal display.
432431
@@ -437,36 +436,26 @@ def main(input_file: Optional[TextIO], width: Optional[int], stream: bool) -> No
437436
- Syntax highlighting for code blocks
438437
- Proper word wrapping based on terminal width
439438
- Support for all standard markdown elements
440-
- Unified streaming renderer for consistent output
441-
- Character-based streaming with backtracking (--stream mode)
439+
- Streaming renderer with backtracking for LLM output
440+
- Character-by-character processing with minimal flickering
442441
443-
The --stream mode is designed for LLM output where text arrives in small
444-
chunks and markdown syntax may be incomplete until more text arrives.
445-
All modes now use the same renderer for consistent beautiful output.
442+
The renderer automatically handles both complete files and streaming input,
443+
with intelligent backtracking when markdown syntax is incomplete.
446444
"""
447445
try:
448446
# Read the input
449447
if input_file is None:
450-
# For stdin, choose processing mode
451-
if stream:
452-
# Character-by-character streaming mode
448+
# For stdin, detect if it's a real terminal or pipe/redirect
449+
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
450+
# Real terminal - use character streaming for interactive input
453451
process_character_stream(sys.stdin, width)
454-
elif hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
455-
# Real terminal - use line-based streaming processing
456-
process_stream(sys.stdin, width)
457452
else:
458-
# Test input or pipe - read all at once
459-
content = sys.stdin.read()
460-
if content.strip():
461-
convert(content, width)
453+
# Pipe or redirect - use character streaming for LLM-style streaming
454+
process_character_stream(sys.stdin, width)
462455
else:
463-
# For files, choose processing mode
464-
if stream:
465-
# Character-by-character streaming mode
466-
process_character_stream(input_file, width)
467-
else:
468-
# Read the entire content at once
469-
content = input_file.read()
456+
# For files, read all at once and use the unified renderer
457+
content = input_file.read()
458+
if content.strip():
470459
convert(content, width)
471460

472461
except KeyboardInterrupt:

tests/__snapshots__/test_md2term.ambr

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99

1010
Features: - 256-color support with different shades for headers - Syntax
1111
highlighting for code blocks - Proper word wrapping based on terminal width -
12-
Support for all standard markdown elements - Streaming input processing
12+
Support for all standard markdown elements - Streaming renderer with
13+
backtracking for LLM output - Character-by-character processing with minimal
14+
flickering
15+
16+
The renderer automatically handles both complete files and streaming input,
17+
with intelligent backtracking when markdown syntax is incomplete.
1318

1419
Options:
1520
-w, --width INTEGER Override terminal width
@@ -37,16 +42,56 @@
3742
# name: TestCLIInterface.test_cli_with_stdin
3843
'''
3944
────────────────────────────────────────────────────────────────────────────────
45+
────────────────────────────────────────────────────────────────────────────────
46+
────────────────────────────────────────────────────────────────────────────────
47+
 Test 
48+
────────────────────────────────────────────────────────────────────────────────
49+
────────────────────────────────────────────────────────────────────────────────
50+
 Test Header 
51+
────────────────────────────────────────────────────────────────────────────────
52+
────────────────────────────────────────────────────────────────────────────────
53+
 Test Header 
54+
────────────────────────────────────────────────────────────────────────────────
55+
56+
────────────────────────────────────────────────────────────────────────────────
57+
 Test Header 
58+
────────────────────────────────────────────────────────────────────────────────
59+
60+
This is a test
61+
────────────────────────────────────────────────────────────────────────────────
62+
 Test Header 
63+
────────────────────────────────────────────────────────────────────────────────
64+
65+
This is a test paragraph
66+
────────────────────────────────────────────────────────────────────────────────
67+
 Test Header 
68+
────────────────────────────────────────────────────────────────────────────────
69+
70+
This is a test paragraph with
71+
────────────────────────────────────────────────────────────────────────────────
4072
 Test Header 
4173
────────────────────────────────────────────────────────────────────────────────
4274

4375
This is a test paragraph with code.
4476

77+
4578
'''
4679
# ---
4780
# name: TestCLIInterface.test_cli_with_width_option
4881
'''
4982
────────────────────────────────────────
83+
────────────────────────────────────────
84+
────────────────────────────────────────
85+
 Test 
86+
────────────────────────────────────────
87+
────────────────────────────────────────
88+
 Test Header 
89+
────────────────────────────────────────
90+
────────────────────────────────────────
91+
 Test Header 
92+
────────────────────────────────────────
93+
94+
────────────────────────────────────────
5095
 Test Header 
5196
────────────────────────────────────────
5297

@@ -55,6 +100,7 @@
55100
terminal width is changed to a smaller
56101
value.
57102

103+
58104
'''
59105
# ---
60106
# name: TestEdgeCases.test_cli_nonexistent_file

0 commit comments

Comments
 (0)