Edit printed terminal text using ANSI escape codes.
Traditional print() writes output that cannot be changed once printed.
mutable-print fixes that you can modify, replace, append, clear, or transform text after it’s been printed.
Perfect for:
- Dynamic CLI output: progress bars, live status updates, etc.
- Retroactive edits: fix or adjust lines even after more output follows.
- Readable terminals: no messy reprints or repeated lines.
- Rich text operations: prepend, append, regex replacement, upper/lower transforms.
All achieved using ANSI escape sequences to move the cursor and redraw content.
pip install mutable-printmutable_print(*args: Any, sep: str = " ", end: str = "\n", file: Optional[TextIO] = None, flush: bool = False) -> mutable_print
Creates a mutable print object, prints the initial content immediately, and returns a handle to modify it later.
from mutable_print import mutable_print
line = mutable_print("Loading...", flush=True)
line("Done!") # Update the same printed line| Name | Type | Default | Description |
|---|---|---|---|
*args |
Any |
— | Values to print |
sep |
str |
" " |
Separator between values |
end |
str |
"\n" |
String appended at the end |
file |
TextIO |
sys.stdout |
Output stream |
flush |
bool |
False |
Whether to flush immediately |
A mutable_print instance that can be updated or transformed.
Update the content and reprint from this line onward.
line("Processing...", sep=" ", end="\n")| Name | Type | Default | Description |
|---|---|---|---|
*args |
Any |
— | New values to print |
sep |
str |
" " |
Separator |
end |
str |
"\n" |
End string |
Replace all (or a limited number of) occurrences of a substring in the current content.
line.replace("fail", "success", 1)| Name | Type | Default | Description |
|---|---|---|---|
old |
str |
— | Substring to replace |
new |
str |
— | Replacement text |
count |
int |
-1 |
Max occurrences (-1 for all) |
Append text to the end of the content.
line.append("...done")| Name | Type | Description |
|---|---|---|
*text |
str |
Text strings to append |
Prepend text to the beginning of the content.
line.prepend("[INFO]")| Name | Type | Description |
|---|---|---|
*text |
str |
Text strings to prepend |
Clear the current content (sets it to an empty string).
line.clear()Replace the entire content with new text.
line.set("New content")| Name | Type | Description |
|---|---|---|
*text |
str |
Text strings to set as new content |
Convert the current content to uppercase.
line.upper()Convert the current content to lowercase.
line.lower()Perform a regular expression substitution on the content.
line.regex_replace(r"\d+", "42")| Name | Type | Default | Description |
|---|---|---|---|
pattern |
str | re.Pattern |
— | Regex pattern |
replacement |
str |
— | Replacement string (supports backrefs) |
flags |
int |
0 |
Regex flags (e.g. re.IGNORECASE) |
Retrieve the current content.
text = line.get()String representation of the current content. Called automatically by str(line).
Developer-friendly representation of the object:
mutable_print('Your content here')- Basic Update
- Clean and Efficient Loading
- Replace Text
- Append / Prepend
- Clearing and Retrieving Content
- Colored Text
Internally, mutable-print:
- Keeps a global list of all printed lines.
- Calculates the cursor position relative to past prints.
- Moves the cursor up using
\033[Aand clears lines with\033[2K. - Reprints from the updated index to maintain correct order.
This enables retroactive edits even after printing additional lines.
Developed with ❤️ by PcoiDev
Uses only standard Python modules (sys, re, typing).
This project is licensed under the MIT License.