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.3.tar.gz (9.0 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.3-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mutable_print-1.0.3.tar.gz
  • Upload date:
  • Size: 9.0 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.3.tar.gz
Algorithm Hash digest
SHA256 b4e696f3c3e8cd92032cd37f5fc8f38d8e425d9b30a0e0bc498a475a572c296b
MD5 2c37981d39a5f68f3fa4799a60bd7895
BLAKE2b-256 d5de4d65e1f185c67af2b876537fe6993e9e683ebe93cee48bf7ecc12347e2b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutable_print-1.0.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: mutable_print-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4a6b664988c598b1dfe053db1582e8dd98c34a1b41973348d0ef95c879c6e09c
MD5 c8cd86d9b65a22cff35d730f3c86f15c
BLAKE2b-256 05fabe86aed3666ddb81768bc7b545435e7aa4a4a2e8bcf554466e43211476d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mutable_print-1.0.3-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