Skip to main content

Edit printed terminal text using ANSI escape codes.

Project description

mutable-print

Edit printed terminal text using ANSI escape codes.

Why Mutable Print?

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.

Installation

pip install mutable-print

Usage (API Overview)

mutable_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

Parameters

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

Returns

A mutable_print instance that can be updated or transformed.


__call__(*args: Any, sep: str = " ", end: str = "\n") -> None

Update the content and reprint from this line onward.

line("Processing...", sep=" ", end="\n")

Parameters

Name Type Default Description
*args Any New values to print
sep str " " Separator
end str "\n" End string

replace(old: str, new: str, count: int = -1) -> mutable_print

Replace all (or a limited number of) occurrences of a substring in the current content.

line.replace("fail", "success", 1)

Parameters

Name Type Default Description
old str Substring to replace
new str Replacement text
count int -1 Max occurrences (-1 for all)

append(*text: str) -> mutable_print

Append text to the end of the content.

line.append("...done")

Parameters

Name Type Description
*text str Text strings to append

prepend(*text: str) -> mutable_print

Prepend text to the beginning of the content.

line.prepend("[INFO]")

Parameters

Name Type Description
*text str Text strings to prepend

clear() -> mutable_print

Clear the current content (sets it to an empty string).

line.clear()

set(*text: str) -> mutable_print

Replace the entire content with new text.

line.set("New content")

Parameters

Name Type Description
*text str Text strings to set as new content

upper() -> mutable_print

Convert the current content to uppercase.

line.upper()

lower() -> mutable_print

Convert the current content to lowercase.

line.lower()

regex_replace(pattern: str | re.Pattern[str], replacement: str, flags: int = 0) -> mutable_print

Perform a regular expression substitution on the content.

line.regex_replace(r"\d+", "42")

Parameters

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)

get() -> str

Retrieve the current content.

text = line.get()

__str__() -> str

String representation of the current content. Called automatically by str(line).


__repr__() -> str

Developer-friendly representation of the object:

mutable_print('Your content here')

Examples

  1. Basic Update
  2. Clean and Efficient Loading
  3. Replace Text
  4. Append / Prepend
  5. Clearing and Retrieving Content
  6. Colored Text

How it works?

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[A and clears lines with \033[2K.
  • Reprints from the updated index to maintain correct order.

This enables retroactive edits even after printing additional lines.

Credits

Developed with ❤️ by PcoiDev Uses only standard Python modules (sys, re, typing).

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mutable_print-1.0.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mutable_print-1.0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file mutable_print-1.0.2.tar.gz.

File metadata

  • Download URL: mutable_print-1.0.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mutable_print-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a0beb50eff6181a9f56ced51f756572328063586b9002d36d65d34c5027993c9
MD5 331695984a0e0f4d36c16c872bf28223
BLAKE2b-256 03c62e85cb47d56e028366c793b291066a0fb286e6e6d8673142d83f39932272

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutable_print-1.0.2.tar.gz:

Publisher: release.yaml on PcoiDev/mutable-print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mutable_print-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: mutable_print-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mutable_print-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3192b9cb3febe211a6947981627de828e8e36fc53a74354750d1a7a98e1bf74e
MD5 568278d89eb5c0daf511f830c61321d3
BLAKE2b-256 9a84648dbb146337758e8e7db6e37201c858974275f67c284accb83a9e1a9aab

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutable_print-1.0.2-py3-none-any.whl:

Publisher: release.yaml on PcoiDev/mutable-print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page