stream-cruise 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stream_cruise-0.1.0/LICENSE +21 -0
- stream_cruise-0.1.0/PKG-INFO +76 -0
- stream_cruise-0.1.0/README.md +52 -0
- stream_cruise-0.1.0/pyproject.toml +39 -0
- stream_cruise-0.1.0/setup.cfg +4 -0
- stream_cruise-0.1.0/stream_cruise/__init__.py +38 -0
- stream_cruise-0.1.0/stream_cruise/core.py +441 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/PKG-INFO +76 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/SOURCES.txt +12 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/dependency_links.txt +1 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/entry_points.txt +3 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/requires.txt +1 -0
- stream_cruise-0.1.0/stream_cruise.egg-info/top_level.txt +1 -0
- stream_cruise-0.1.0/tests/test_core.py +147 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Suvi Tiwary
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stream-cruise
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stream Rich-styled text, print output, and system logs character-by-character in the terminal.
|
|
5
|
+
Author-email: Suvi Tiwary <suvikumari345@gmail.com>
|
|
6
|
+
Keywords: cli,rich,streaming,terminal,logging
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Terminals
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: rich>=13.0.0
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# stream-cruise
|
|
26
|
+
|
|
27
|
+
Stream Rich-styled text, `print()` output, and Python system logs character-by-character in the terminal.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install stream-cruise
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Zero-Integration Mode (Stream All Logs & `print()`)
|
|
36
|
+
|
|
37
|
+
Add a single line at the start of your app—every `print()` call and Python `logging` message streams character-by-character automatically with color-coded log levels:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import logging
|
|
41
|
+
from stream_cruise import enable_streaming
|
|
42
|
+
|
|
43
|
+
enable_streaming(delay=0.02)
|
|
44
|
+
|
|
45
|
+
print("Normal print() statements stream automatically!")
|
|
46
|
+
logging.info("System startup complete")
|
|
47
|
+
logging.warning("Low fuel warning")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or scope it to a `with` block:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from stream_cruise import stream_output
|
|
54
|
+
|
|
55
|
+
with stream_output(delay=0.02):
|
|
56
|
+
print("Streamed inside this block")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Explicit Streaming & Interactive Prompt
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from stream_cruise import antigravity_prompt, stream_text
|
|
63
|
+
|
|
64
|
+
stream_text("[bold cyan]Hello[/bold cyan] [italic magenta]World![/italic magenta]", delay=0.03)
|
|
65
|
+
antigravity_prompt()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## CLI Usage
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# Run the interactive blast-off prompt
|
|
72
|
+
stream-text
|
|
73
|
+
|
|
74
|
+
# Stream a styled message from the command line
|
|
75
|
+
stream-text "[bold green]Deployment complete![/bold green]" --delay 0.02
|
|
76
|
+
```
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# stream-cruise
|
|
2
|
+
|
|
3
|
+
Stream Rich-styled text, `print()` output, and Python system logs character-by-character in the terminal.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install stream-cruise
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Zero-Integration Mode (Stream All Logs & `print()`)
|
|
12
|
+
|
|
13
|
+
Add a single line at the start of your app—every `print()` call and Python `logging` message streams character-by-character automatically with color-coded log levels:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import logging
|
|
17
|
+
from stream_cruise import enable_streaming
|
|
18
|
+
|
|
19
|
+
enable_streaming(delay=0.02)
|
|
20
|
+
|
|
21
|
+
print("Normal print() statements stream automatically!")
|
|
22
|
+
logging.info("System startup complete")
|
|
23
|
+
logging.warning("Low fuel warning")
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or scope it to a `with` block:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from stream_cruise import stream_output
|
|
30
|
+
|
|
31
|
+
with stream_output(delay=0.02):
|
|
32
|
+
print("Streamed inside this block")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Explicit Streaming & Interactive Prompt
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from stream_cruise import antigravity_prompt, stream_text
|
|
39
|
+
|
|
40
|
+
stream_text("[bold cyan]Hello[/bold cyan] [italic magenta]World![/italic magenta]", delay=0.03)
|
|
41
|
+
antigravity_prompt()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## CLI Usage
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# Run the interactive blast-off prompt
|
|
48
|
+
stream-text
|
|
49
|
+
|
|
50
|
+
# Stream a styled message from the command line
|
|
51
|
+
stream-text "[bold green]Deployment complete![/bold green]" --delay 0.02
|
|
52
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "stream-cruise"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Stream Rich-styled text, print output, and system logs character-by-character in the terminal."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Suvi Tiwary", email = "suvikumari345@gmail.com" },
|
|
12
|
+
]
|
|
13
|
+
keywords = ["cli", "rich", "streaming", "terminal", "logging"]
|
|
14
|
+
requires-python = ">=3.9"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"rich>=13.0.0",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Environment :: Console",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"License :: OSI Approved :: MIT License",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.9",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Programming Language :: Python :: 3.13",
|
|
30
|
+
"Topic :: Terminals",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
stream-cruise = "stream_cruise.core:main"
|
|
35
|
+
stream-text = "stream_cruise.core:main"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
include = ["stream_cruise*"]
|
|
39
|
+
exclude = ["tests*"]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""
|
|
2
|
+
stream_cruise
|
|
3
|
+
=============
|
|
4
|
+
|
|
5
|
+
Stream Rich-styled text, print output, and system logs character-by-character.
|
|
6
|
+
|
|
7
|
+
Quick example (explicit call):
|
|
8
|
+
from stream_cruise import stream_text, antigravity_prompt
|
|
9
|
+
|
|
10
|
+
stream_text("[bold cyan]Hello, world![/bold cyan]", delay=0.03)
|
|
11
|
+
antigravity_prompt()
|
|
12
|
+
|
|
13
|
+
Zero-integration example (streams all print() and logging output automatically):
|
|
14
|
+
import logging
|
|
15
|
+
from stream_cruise import enable_streaming
|
|
16
|
+
|
|
17
|
+
enable_streaming()
|
|
18
|
+
print("This print statement streams automatically!")
|
|
19
|
+
logging.warning("System logs stream automatically too!")
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from stream_cruise.core import (
|
|
23
|
+
antigravity_prompt,
|
|
24
|
+
disable_streaming,
|
|
25
|
+
enable_streaming,
|
|
26
|
+
stream_output,
|
|
27
|
+
stream_text,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"stream_text",
|
|
32
|
+
"antigravity_prompt",
|
|
33
|
+
"enable_streaming",
|
|
34
|
+
"disable_streaming",
|
|
35
|
+
"stream_output",
|
|
36
|
+
"__version__",
|
|
37
|
+
]
|
|
38
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Core functions for character-by-character terminal streaming and global log capture.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import io
|
|
9
|
+
import logging
|
|
10
|
+
import sys
|
|
11
|
+
import threading
|
|
12
|
+
import time
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Iterator, Optional, TextIO
|
|
16
|
+
|
|
17
|
+
from rich.console import Console
|
|
18
|
+
from rich.errors import MarkupError
|
|
19
|
+
from rich.panel import Panel
|
|
20
|
+
from rich.prompt import Prompt
|
|
21
|
+
from rich.text import Text
|
|
22
|
+
|
|
23
|
+
_LOG_LEVEL_STYLES = {
|
|
24
|
+
logging.DEBUG: "dim cyan",
|
|
25
|
+
logging.INFO: "bold green",
|
|
26
|
+
logging.WARNING: "bold yellow",
|
|
27
|
+
logging.ERROR: "bold red",
|
|
28
|
+
logging.CRITICAL: "bold reverse bright_red",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class _StreamingSession:
|
|
34
|
+
"""Tracks active stdout and logging hooks so disable_streaming() can restore them."""
|
|
35
|
+
|
|
36
|
+
console: Console
|
|
37
|
+
original_stdout: TextIO
|
|
38
|
+
original_log_level: int
|
|
39
|
+
replaced_handlers: list[logging.Handler] = field(default_factory=list)
|
|
40
|
+
added_handler: Optional[logging.Handler] = None
|
|
41
|
+
capture_stdout: bool = False
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
_session_lock = threading.RLock()
|
|
45
|
+
_active_session: Optional[_StreamingSession] = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_output_console(console: Optional[Console]) -> Console:
|
|
49
|
+
"""Return the caller's console, the active session's raw console, or a fresh Console."""
|
|
50
|
+
if console is not None:
|
|
51
|
+
return console
|
|
52
|
+
if _active_session is not None:
|
|
53
|
+
return _active_session.console
|
|
54
|
+
return Console()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _supports_utf8(console: Console) -> bool:
|
|
58
|
+
"""Return True when the output stream supports UTF-8 characters."""
|
|
59
|
+
encoding = getattr(console.file, "encoding", None) or sys.stdout.encoding or ""
|
|
60
|
+
return "utf" in encoding.lower()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _stream_rich_text(
|
|
64
|
+
rich_text: Text,
|
|
65
|
+
delay: float,
|
|
66
|
+
end: str,
|
|
67
|
+
console: Console,
|
|
68
|
+
) -> None:
|
|
69
|
+
"""Write a Rich Text object one styled character at a time."""
|
|
70
|
+
for index in range(len(rich_text)):
|
|
71
|
+
char_slice = rich_text[index : index + 1]
|
|
72
|
+
console.print(char_slice, end="", highlight=False, soft_wrap=True)
|
|
73
|
+
console.file.flush()
|
|
74
|
+
if delay > 0:
|
|
75
|
+
time.sleep(delay)
|
|
76
|
+
|
|
77
|
+
if end:
|
|
78
|
+
console.file.write(end)
|
|
79
|
+
console.file.flush()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def stream_text(
|
|
83
|
+
text: str,
|
|
84
|
+
delay: float = 0.03,
|
|
85
|
+
style: Optional[str] = None,
|
|
86
|
+
*,
|
|
87
|
+
markup: bool = True,
|
|
88
|
+
end: str = "\n",
|
|
89
|
+
console: Optional[Console] = None,
|
|
90
|
+
) -> None:
|
|
91
|
+
"""
|
|
92
|
+
Stream text character-by-character in the terminal using Rich colors and fonts.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
text: The text to print. Supports Rich markup like ``[bold cyan]hi[/bold cyan]``.
|
|
96
|
+
delay: Pause in seconds between each character (must be 0 or greater).
|
|
97
|
+
style: Optional base Rich style applied to the whole message (e.g. ``"italic green"``).
|
|
98
|
+
markup: If True, interpret Rich ``[...]`` tags; if False, print brackets literally.
|
|
99
|
+
end: Text printed after streaming finishes (defaults to a newline).
|
|
100
|
+
console: Optional Rich ``Console`` instance (useful for custom output or tests).
|
|
101
|
+
|
|
102
|
+
Example:
|
|
103
|
+
>>> stream_text("[bold magenta]Streaming live![/bold magenta]", delay=0.02)
|
|
104
|
+
"""
|
|
105
|
+
if delay < 0:
|
|
106
|
+
raise ValueError("delay must be non-negative")
|
|
107
|
+
|
|
108
|
+
target_console = _get_output_console(console)
|
|
109
|
+
base_style = style or ""
|
|
110
|
+
|
|
111
|
+
if markup:
|
|
112
|
+
try:
|
|
113
|
+
rich_text = Text.from_markup(text, style=base_style)
|
|
114
|
+
except MarkupError:
|
|
115
|
+
rich_text = Text(text, style=base_style)
|
|
116
|
+
else:
|
|
117
|
+
rich_text = Text(text, style=base_style)
|
|
118
|
+
|
|
119
|
+
_stream_rich_text(rich_text, delay=delay, end=end, console=target_console)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class _StreamingStdout(io.TextIOBase):
|
|
123
|
+
"""Wraps sys.stdout so ordinary print() calls stream character-by-character."""
|
|
124
|
+
|
|
125
|
+
def __init__(
|
|
126
|
+
self,
|
|
127
|
+
target: TextIO,
|
|
128
|
+
console: Console,
|
|
129
|
+
delay: float,
|
|
130
|
+
style: Optional[str],
|
|
131
|
+
markup: bool,
|
|
132
|
+
) -> None:
|
|
133
|
+
self._target = target
|
|
134
|
+
self._console = console
|
|
135
|
+
self._delay = delay
|
|
136
|
+
self._style = style
|
|
137
|
+
self._markup = markup
|
|
138
|
+
self._guard = threading.local()
|
|
139
|
+
|
|
140
|
+
@property
|
|
141
|
+
def encoding(self) -> str:
|
|
142
|
+
return getattr(self._target, "encoding", "utf-8")
|
|
143
|
+
|
|
144
|
+
def isatty(self) -> bool:
|
|
145
|
+
return bool(getattr(self._target, "isatty", lambda: False)())
|
|
146
|
+
|
|
147
|
+
def writable(self) -> bool:
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
def flush(self) -> None:
|
|
151
|
+
self._target.flush()
|
|
152
|
+
|
|
153
|
+
def write(self, s: str) -> int:
|
|
154
|
+
if not s:
|
|
155
|
+
return 0
|
|
156
|
+
if getattr(self._guard, "active", False):
|
|
157
|
+
self._target.write(s)
|
|
158
|
+
self._target.flush()
|
|
159
|
+
return len(s)
|
|
160
|
+
|
|
161
|
+
self._guard.active = True
|
|
162
|
+
try:
|
|
163
|
+
stream_text(
|
|
164
|
+
s,
|
|
165
|
+
delay=self._delay,
|
|
166
|
+
style=self._style,
|
|
167
|
+
markup=self._markup,
|
|
168
|
+
end="",
|
|
169
|
+
console=self._console,
|
|
170
|
+
)
|
|
171
|
+
finally:
|
|
172
|
+
self._guard.active = False
|
|
173
|
+
return len(s)
|
|
174
|
+
|
|
175
|
+
def __getattr__(self, name: str):
|
|
176
|
+
return getattr(self._target, name)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class _StreamingLogHandler(logging.Handler):
|
|
180
|
+
"""Python logging handler that streams system log records with Rich colors."""
|
|
181
|
+
|
|
182
|
+
def __init__(self, console: Console, delay: float) -> None:
|
|
183
|
+
super().__init__()
|
|
184
|
+
self._console = console
|
|
185
|
+
self._delay = delay
|
|
186
|
+
|
|
187
|
+
def emit(self, record: logging.LogRecord) -> None:
|
|
188
|
+
try:
|
|
189
|
+
level_style = _LOG_LEVEL_STYLES.get(record.levelno, "bold white")
|
|
190
|
+
message = self.format(record) if self.formatter else record.getMessage()
|
|
191
|
+
if record.exc_info and not (self.formatter and record.exc_text):
|
|
192
|
+
message = f"{message}\n{logging.Formatter().formatException(record.exc_info)}"
|
|
193
|
+
|
|
194
|
+
rich_line = Text.assemble(
|
|
195
|
+
(f"[{record.levelname}]", level_style),
|
|
196
|
+
(" ", ""),
|
|
197
|
+
(f"{record.name}: ", "dim cyan"),
|
|
198
|
+
(message, ""),
|
|
199
|
+
)
|
|
200
|
+
_stream_rich_text(rich_line, delay=self._delay, end="\n", console=self._console)
|
|
201
|
+
except Exception:
|
|
202
|
+
self.handleError(record)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def enable_streaming(
|
|
206
|
+
delay: float = 0.02,
|
|
207
|
+
style: Optional[str] = None,
|
|
208
|
+
*,
|
|
209
|
+
capture_stdout: bool = True,
|
|
210
|
+
capture_logs: bool = True,
|
|
211
|
+
log_level: int = logging.INFO,
|
|
212
|
+
markup: bool = False,
|
|
213
|
+
) -> None:
|
|
214
|
+
"""
|
|
215
|
+
Enable automatic character-by-character streaming for all print() and system logs.
|
|
216
|
+
|
|
217
|
+
Call this once at startup so the rest of your app does not need to replace
|
|
218
|
+
``print()`` or ``logging`` calls with ``stream_text()``.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
delay: Pause in seconds between each streamed character.
|
|
222
|
+
style: Optional base Rich style for captured ``print()`` output.
|
|
223
|
+
capture_stdout: If True, wrap ``sys.stdout`` so ``print()`` streams automatically.
|
|
224
|
+
capture_logs: If True, stream Python ``logging`` records with color-coded levels.
|
|
225
|
+
log_level: Minimum logging level to display when ``capture_logs`` is True.
|
|
226
|
+
markup: If True, parse Rich markup inside ``print()`` output (default False so
|
|
227
|
+
brackets like ``[0]`` or ``[INFO]`` in ordinary output print safely).
|
|
228
|
+
"""
|
|
229
|
+
global _active_session
|
|
230
|
+
if delay < 0:
|
|
231
|
+
raise ValueError("delay must be non-negative")
|
|
232
|
+
|
|
233
|
+
with _session_lock:
|
|
234
|
+
if _active_session is not None:
|
|
235
|
+
disable_streaming()
|
|
236
|
+
|
|
237
|
+
original_stdout = sys.stdout
|
|
238
|
+
raw_console = Console(file=original_stdout)
|
|
239
|
+
|
|
240
|
+
root_logger = logging.getLogger()
|
|
241
|
+
original_log_level = root_logger.level
|
|
242
|
+
replaced_handlers: list[logging.Handler] = []
|
|
243
|
+
added_handler: Optional[logging.Handler] = None
|
|
244
|
+
|
|
245
|
+
if capture_logs:
|
|
246
|
+
# Temporarily detach existing console StreamHandlers to avoid duplicate log lines.
|
|
247
|
+
for handler in list(root_logger.handlers):
|
|
248
|
+
if isinstance(handler, logging.StreamHandler) and not isinstance(
|
|
249
|
+
handler, logging.FileHandler
|
|
250
|
+
):
|
|
251
|
+
root_logger.removeHandler(handler)
|
|
252
|
+
replaced_handlers.append(handler)
|
|
253
|
+
|
|
254
|
+
if root_logger.level == logging.NOTSET or root_logger.level > log_level:
|
|
255
|
+
root_logger.setLevel(log_level)
|
|
256
|
+
|
|
257
|
+
added_handler = _StreamingLogHandler(console=raw_console, delay=delay)
|
|
258
|
+
added_handler.setLevel(log_level)
|
|
259
|
+
root_logger.addHandler(added_handler)
|
|
260
|
+
|
|
261
|
+
if capture_stdout:
|
|
262
|
+
sys.stdout = _StreamingStdout(
|
|
263
|
+
target=original_stdout,
|
|
264
|
+
console=raw_console,
|
|
265
|
+
delay=delay,
|
|
266
|
+
style=style,
|
|
267
|
+
markup=markup,
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
_active_session = _StreamingSession(
|
|
271
|
+
console=raw_console,
|
|
272
|
+
original_stdout=original_stdout,
|
|
273
|
+
original_log_level=original_log_level,
|
|
274
|
+
replaced_handlers=replaced_handlers,
|
|
275
|
+
added_handler=added_handler,
|
|
276
|
+
capture_stdout=capture_stdout,
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def disable_streaming() -> None:
|
|
281
|
+
"""
|
|
282
|
+
Restore normal ``sys.stdout`` and Python ``logging`` handlers after ``enable_streaming()``.
|
|
283
|
+
"""
|
|
284
|
+
global _active_session
|
|
285
|
+
with _session_lock:
|
|
286
|
+
if _active_session is None:
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
session = _active_session
|
|
290
|
+
_active_session = None
|
|
291
|
+
|
|
292
|
+
if session.capture_stdout:
|
|
293
|
+
sys.stdout = session.original_stdout
|
|
294
|
+
|
|
295
|
+
root_logger = logging.getLogger()
|
|
296
|
+
if session.added_handler is not None:
|
|
297
|
+
root_logger.removeHandler(session.added_handler)
|
|
298
|
+
for handler in session.replaced_handlers:
|
|
299
|
+
root_logger.addHandler(handler)
|
|
300
|
+
root_logger.setLevel(session.original_log_level)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
@contextmanager
|
|
304
|
+
def stream_output(
|
|
305
|
+
delay: float = 0.02,
|
|
306
|
+
style: Optional[str] = None,
|
|
307
|
+
*,
|
|
308
|
+
capture_stdout: bool = True,
|
|
309
|
+
capture_logs: bool = True,
|
|
310
|
+
log_level: int = logging.INFO,
|
|
311
|
+
markup: bool = False,
|
|
312
|
+
) -> Iterator[None]:
|
|
313
|
+
"""
|
|
314
|
+
Context manager that streams all ``print()`` output and system logs inside a ``with`` block.
|
|
315
|
+
|
|
316
|
+
Example:
|
|
317
|
+
>>> with stream_output(delay=0.01):
|
|
318
|
+
... print("Every print here streams!")
|
|
319
|
+
"""
|
|
320
|
+
enable_streaming(
|
|
321
|
+
delay=delay,
|
|
322
|
+
style=style,
|
|
323
|
+
capture_stdout=capture_stdout,
|
|
324
|
+
capture_logs=capture_logs,
|
|
325
|
+
log_level=log_level,
|
|
326
|
+
markup=markup,
|
|
327
|
+
)
|
|
328
|
+
try:
|
|
329
|
+
yield
|
|
330
|
+
finally:
|
|
331
|
+
disable_streaming()
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def antigravity_prompt(
|
|
335
|
+
delay: float = 0.03,
|
|
336
|
+
console: Optional[Console] = None,
|
|
337
|
+
) -> str:
|
|
338
|
+
"""
|
|
339
|
+
Run an interactive CLI greeting that asks for the user's name and streams a blast-off message.
|
|
340
|
+
|
|
341
|
+
Args:
|
|
342
|
+
delay: Pause in seconds between each streamed character.
|
|
343
|
+
console: Optional Rich ``Console`` instance for reading input and writing output.
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
The user's entered name, or ``"Explorer"`` if left empty.
|
|
347
|
+
"""
|
|
348
|
+
target_console = _get_output_console(console)
|
|
349
|
+
use_emoji = _supports_utf8(target_console)
|
|
350
|
+
rocket = "🚀 " if use_emoji else ""
|
|
351
|
+
sparkles = " ✨" if use_emoji else ""
|
|
352
|
+
|
|
353
|
+
target_console.print(
|
|
354
|
+
Panel.fit(
|
|
355
|
+
"""[bold cyan]Welcome to Stream Cruise[/bold cyan]
|
|
356
|
+
[dim]Preparing terminal thrusters...[/dim]""",
|
|
357
|
+
border_style="bright_blue",
|
|
358
|
+
)
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
raw_name = Prompt.ask(
|
|
362
|
+
"[bold magenta]What is your name, astronaut?[/bold magenta]",
|
|
363
|
+
default="Explorer",
|
|
364
|
+
console=target_console,
|
|
365
|
+
)
|
|
366
|
+
name = raw_name.strip() or "Explorer"
|
|
367
|
+
|
|
368
|
+
stream_text(
|
|
369
|
+
f"""
|
|
370
|
+
[bold green]Copy that,[/bold green] [bold underline cyan]{name}[/bold underline cyan]! [italic white]All systems are nominal.[/italic white]""",
|
|
371
|
+
delay=delay,
|
|
372
|
+
console=target_console,
|
|
373
|
+
)
|
|
374
|
+
stream_text(
|
|
375
|
+
"[bold yellow]Ignition sequence start:[/bold yellow] "
|
|
376
|
+
"[bold bright_yellow]3...[/bold bright_yellow] "
|
|
377
|
+
"[bold dark_orange]2...[/bold dark_orange] "
|
|
378
|
+
"[bold red]1...[/bold red]",
|
|
379
|
+
delay=max(delay, 0.04) if delay > 0 else 0.0,
|
|
380
|
+
console=target_console,
|
|
381
|
+
)
|
|
382
|
+
stream_text(
|
|
383
|
+
f"{rocket}[bold reverse bright_red] BLAST OFF! [/bold reverse bright_red] "
|
|
384
|
+
f"[bold bright_magenta]Have a stellar flight into orbit, {name}!{sparkles}[/bold bright_magenta]",
|
|
385
|
+
delay=delay,
|
|
386
|
+
console=target_console,
|
|
387
|
+
)
|
|
388
|
+
return name
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def main(argv: Optional[list[str]] = None) -> int:
|
|
392
|
+
"""
|
|
393
|
+
CLI entry point for the ``stream-cruise`` and ``stream-text`` commands.
|
|
394
|
+
|
|
395
|
+
Pass text as an argument to stream it, or run with no arguments for ``antigravity_prompt()``.
|
|
396
|
+
"""
|
|
397
|
+
parser = argparse.ArgumentParser(
|
|
398
|
+
prog="stream-text",
|
|
399
|
+
description="Stream Rich-styled text character-by-character in the terminal.",
|
|
400
|
+
)
|
|
401
|
+
parser.add_argument(
|
|
402
|
+
"text",
|
|
403
|
+
nargs="?",
|
|
404
|
+
help="Text (with optional Rich markup) to stream. Omit to run the interactive prompt.",
|
|
405
|
+
)
|
|
406
|
+
parser.add_argument(
|
|
407
|
+
"-d",
|
|
408
|
+
"--delay",
|
|
409
|
+
type=float,
|
|
410
|
+
default=0.03,
|
|
411
|
+
help="Delay in seconds between characters (default: 0.03).",
|
|
412
|
+
)
|
|
413
|
+
parser.add_argument(
|
|
414
|
+
"-s",
|
|
415
|
+
"--style",
|
|
416
|
+
default=None,
|
|
417
|
+
help="Base Rich style, e.g. 'bold cyan' or 'italic magenta'.",
|
|
418
|
+
)
|
|
419
|
+
parser.add_argument(
|
|
420
|
+
"--no-markup",
|
|
421
|
+
action="store_true",
|
|
422
|
+
help="Disable Rich markup parsing and print brackets literally.",
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
args = parser.parse_args(argv)
|
|
426
|
+
|
|
427
|
+
if args.text is not None:
|
|
428
|
+
stream_text(
|
|
429
|
+
args.text,
|
|
430
|
+
delay=args.delay,
|
|
431
|
+
style=args.style,
|
|
432
|
+
markup=not args.no_markup,
|
|
433
|
+
)
|
|
434
|
+
else:
|
|
435
|
+
antigravity_prompt(delay=args.delay)
|
|
436
|
+
|
|
437
|
+
return 0
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
if __name__ == "__main__":
|
|
441
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stream-cruise
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stream Rich-styled text, print output, and system logs character-by-character in the terminal.
|
|
5
|
+
Author-email: Suvi Tiwary <suvikumari345@gmail.com>
|
|
6
|
+
Keywords: cli,rich,streaming,terminal,logging
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Terminals
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: rich>=13.0.0
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# stream-cruise
|
|
26
|
+
|
|
27
|
+
Stream Rich-styled text, `print()` output, and Python system logs character-by-character in the terminal.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install stream-cruise
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Zero-Integration Mode (Stream All Logs & `print()`)
|
|
36
|
+
|
|
37
|
+
Add a single line at the start of your app—every `print()` call and Python `logging` message streams character-by-character automatically with color-coded log levels:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import logging
|
|
41
|
+
from stream_cruise import enable_streaming
|
|
42
|
+
|
|
43
|
+
enable_streaming(delay=0.02)
|
|
44
|
+
|
|
45
|
+
print("Normal print() statements stream automatically!")
|
|
46
|
+
logging.info("System startup complete")
|
|
47
|
+
logging.warning("Low fuel warning")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or scope it to a `with` block:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from stream_cruise import stream_output
|
|
54
|
+
|
|
55
|
+
with stream_output(delay=0.02):
|
|
56
|
+
print("Streamed inside this block")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Explicit Streaming & Interactive Prompt
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from stream_cruise import antigravity_prompt, stream_text
|
|
63
|
+
|
|
64
|
+
stream_text("[bold cyan]Hello[/bold cyan] [italic magenta]World![/italic magenta]", delay=0.03)
|
|
65
|
+
antigravity_prompt()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## CLI Usage
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# Run the interactive blast-off prompt
|
|
72
|
+
stream-text
|
|
73
|
+
|
|
74
|
+
# Stream a styled message from the command line
|
|
75
|
+
stream-text "[bold green]Deployment complete![/bold green]" --delay 0.02
|
|
76
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
stream_cruise/__init__.py
|
|
5
|
+
stream_cruise/core.py
|
|
6
|
+
stream_cruise.egg-info/PKG-INFO
|
|
7
|
+
stream_cruise.egg-info/SOURCES.txt
|
|
8
|
+
stream_cruise.egg-info/dependency_links.txt
|
|
9
|
+
stream_cruise.egg-info/entry_points.txt
|
|
10
|
+
stream_cruise.egg-info/requires.txt
|
|
11
|
+
stream_cruise.egg-info/top_level.txt
|
|
12
|
+
tests/test_core.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rich>=13.0.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
stream_cruise
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Automated unit tests for the stream_cruise package.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import io
|
|
8
|
+
import logging
|
|
9
|
+
import sys
|
|
10
|
+
import unittest
|
|
11
|
+
from contextlib import redirect_stdout
|
|
12
|
+
from unittest.mock import patch
|
|
13
|
+
|
|
14
|
+
from rich.console import Console
|
|
15
|
+
|
|
16
|
+
from stream_cruise import (
|
|
17
|
+
__version__,
|
|
18
|
+
antigravity_prompt,
|
|
19
|
+
disable_streaming,
|
|
20
|
+
enable_streaming,
|
|
21
|
+
stream_output,
|
|
22
|
+
stream_text,
|
|
23
|
+
)
|
|
24
|
+
from stream_cruise.core import main
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class TestStreamText(unittest.TestCase):
|
|
28
|
+
"""Tests for character-by-character text streaming."""
|
|
29
|
+
|
|
30
|
+
def test_version_is_defined(self) -> None:
|
|
31
|
+
self.assertEqual(__version__, "0.1.0")
|
|
32
|
+
|
|
33
|
+
def test_streams_plain_and_markup_text(self) -> None:
|
|
34
|
+
buffer = io.StringIO()
|
|
35
|
+
console = Console(file=buffer, force_terminal=False, color_system=None)
|
|
36
|
+
|
|
37
|
+
stream_text(
|
|
38
|
+
"[bold cyan]Hello[/bold cyan] [italic]World[/italic]",
|
|
39
|
+
delay=0,
|
|
40
|
+
console=console,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
self.assertEqual(buffer.getvalue(), "Hello World\n")
|
|
44
|
+
|
|
45
|
+
def test_no_markup_prints_literal_brackets(self) -> None:
|
|
46
|
+
buffer = io.StringIO()
|
|
47
|
+
console = Console(file=buffer, force_terminal=False, color_system=None)
|
|
48
|
+
|
|
49
|
+
stream_text("[bold]Literal[/bold]", delay=0, markup=False, console=console)
|
|
50
|
+
|
|
51
|
+
self.assertEqual(buffer.getvalue(), "[bold]Literal[/bold]\n")
|
|
52
|
+
|
|
53
|
+
def test_invalid_markup_falls_back_gracefully(self) -> None:
|
|
54
|
+
buffer = io.StringIO()
|
|
55
|
+
console = Console(file=buffer, force_terminal=False, color_system=None)
|
|
56
|
+
|
|
57
|
+
stream_text("Path [/invalid/tag] works", delay=0, console=console)
|
|
58
|
+
|
|
59
|
+
self.assertEqual(buffer.getvalue(), "Path [/invalid/tag] works\n")
|
|
60
|
+
|
|
61
|
+
def test_negative_delay_raises_value_error(self) -> None:
|
|
62
|
+
with self.assertRaises(ValueError):
|
|
63
|
+
stream_text("Hello", delay=-0.1)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TestGlobalStreamingAndLogs(unittest.TestCase):
|
|
67
|
+
"""Tests for zero-integration stdout and system logging capture."""
|
|
68
|
+
|
|
69
|
+
def tearDown(self) -> None:
|
|
70
|
+
disable_streaming()
|
|
71
|
+
|
|
72
|
+
def test_stream_output_captures_print_and_system_logs(self) -> None:
|
|
73
|
+
buffer = io.StringIO()
|
|
74
|
+
logger = logging.getLogger("demo.worker")
|
|
75
|
+
|
|
76
|
+
with redirect_stdout(buffer):
|
|
77
|
+
with stream_output(delay=0, log_level=logging.INFO):
|
|
78
|
+
print("Regular print works [0]")
|
|
79
|
+
logger.info("System startup complete")
|
|
80
|
+
logger.warning("Low fuel warning")
|
|
81
|
+
|
|
82
|
+
output = buffer.getvalue()
|
|
83
|
+
self.assertIn("Regular print works [0]", output)
|
|
84
|
+
self.assertIn("[INFO] demo.worker: System startup complete", output)
|
|
85
|
+
self.assertIn("[WARNING] demo.worker: Low fuel warning", output)
|
|
86
|
+
|
|
87
|
+
def test_disable_streaming_restores_stdout(self) -> None:
|
|
88
|
+
original = sys.stdout
|
|
89
|
+
buffer = io.StringIO()
|
|
90
|
+
with redirect_stdout(buffer):
|
|
91
|
+
enable_streaming(delay=0)
|
|
92
|
+
self.assertIsNot(sys.stdout, original)
|
|
93
|
+
disable_streaming()
|
|
94
|
+
self.assertIs(sys.stdout, original)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class TestAntigravityPrompt(unittest.TestCase):
|
|
98
|
+
"""Tests for the interactive blast-off prompt."""
|
|
99
|
+
|
|
100
|
+
def test_prompt_streams_blast_off_for_entered_name(self) -> None:
|
|
101
|
+
buffer = io.StringIO()
|
|
102
|
+
console = Console(file=buffer, force_terminal=False, color_system=None)
|
|
103
|
+
|
|
104
|
+
with patch("stream_cruise.core.Prompt.ask", return_value="Ada"):
|
|
105
|
+
returned_name = antigravity_prompt(delay=0, console=console)
|
|
106
|
+
|
|
107
|
+
output = buffer.getvalue()
|
|
108
|
+
self.assertEqual(returned_name, "Ada")
|
|
109
|
+
self.assertIn("Copy that, Ada!", output)
|
|
110
|
+
self.assertIn("BLAST OFF!", output)
|
|
111
|
+
|
|
112
|
+
def test_blank_name_defaults_to_explorer(self) -> None:
|
|
113
|
+
buffer = io.StringIO()
|
|
114
|
+
console = Console(file=buffer, force_terminal=False, color_system=None)
|
|
115
|
+
|
|
116
|
+
with patch("stream_cruise.core.Prompt.ask", return_value=" "):
|
|
117
|
+
returned_name = antigravity_prompt(delay=0, console=console)
|
|
118
|
+
|
|
119
|
+
self.assertEqual(returned_name, "Explorer")
|
|
120
|
+
self.assertIn("Explorer", buffer.getvalue())
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class TestCLI(unittest.TestCase):
|
|
124
|
+
"""Tests for the CLI entry point."""
|
|
125
|
+
|
|
126
|
+
def test_cli_with_text_argument(self) -> None:
|
|
127
|
+
with patch("stream_cruise.core.stream_text") as mock_stream:
|
|
128
|
+
exit_code = main(["Hello CLI", "--delay", "0", "--style", "bold green"])
|
|
129
|
+
|
|
130
|
+
self.assertEqual(exit_code, 0)
|
|
131
|
+
mock_stream.assert_called_once_with(
|
|
132
|
+
"Hello CLI",
|
|
133
|
+
delay=0.0,
|
|
134
|
+
style="bold green",
|
|
135
|
+
markup=True,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
def test_cli_without_text_runs_prompt(self) -> None:
|
|
139
|
+
with patch("stream_cruise.core.antigravity_prompt") as mock_prompt:
|
|
140
|
+
exit_code = main(["--delay", "0"])
|
|
141
|
+
|
|
142
|
+
self.assertEqual(exit_code, 0)
|
|
143
|
+
mock_prompt.assert_called_once_with(delay=0.0)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
if __name__ == "__main__":
|
|
147
|
+
unittest.main()
|