Skip to main content

toggle_cbreak

Simple lib to turn cbreak mode on and off

Installation

pip install toggle_cbreak

Quickstart

from toggle_cbreak import cbreak_off, cbreak_on

# by default, our terminal is now in cooked mode

cbreak_on() # turns on cbreak mode

# now, we are in cbreak mode

cbreak_off()

# now, we are in cooked mode again

Some context on terminal modes

Our terminal has 3 modes:

  • cooked mode (default)
  • raw mode
  • cbreak mode

Cooked mode (default)

  • data is preprocessed before being given to program
  • system intercepts special characters, and gives special behaviour to them
    • eg. Backspace, Control-C, etc
  • Let's say you type "ABC<Backspace>D"
    • in cooked mode, this is processed as "ABD"
    • the <Backspace> in a way, deletes our C

Raw mode

  • data is passed as it is to program
  • system does not interpret special characters as special characters
  • Let's say you type "ABC<Backspace>D"
    • "ABC<Backspace>D" itself is given to our program
    • no processing done

Cbreak mode

  • a mode between cooked mode and raw mode
  • only certain characters eg. Control-C will be processed as special characters

Simple example (copy paste this and try it out)

from toggle_cbreak import cbreak_off, cbreak_on

firstname = input("(cooked mode) Enter first name: ")

cbreak_on()

print(
    "(cbreak mode) Enter some text. "
    "Whatever you type will be repeated multiple times as you type. "
    "Hit Control-C to escape this mode."
)

try:
    while True:
        char = sys.stdin.read(1)
        print(char * random.randrange(1, 4), end="", flush=True)
except KeyboardInterrupt:
    pass

cbreak_off()
print("\nBack to normal cooked mode.\n")

lastname = input("(cooked mode) Enter last name: ")

print(f"{firstname=} {lastname=}")

Release files for toggle-cbreak 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toggle-cbreak 0.0.3
File Size Uploaded
toggle_cbreak-0.0.3.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for toggle-cbreak 0.0.3
File Interpreter ABI Platform
toggle_cbreak-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 5.5 kB

Release files / toggle_cbreak-0.0.3.tar.gz

Download URL toggle_cbreak-0.0.3.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fac597781400ffc6ae760dbdfb4e68b1523f7ce6aa065c5089c8341f97b54a35
BLAKE2b-256 checksum
How to use checksums
7f654a1a90430c45e9d991f7f0a847fc99f0f5c1bed1934f05b7319d7d671150
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / toggle_cbreak-0.0.3-py3-none-any.whl

Download URL toggle_cbreak-0.0.3-py3-none-any.whl
Size 2.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71d1c9ab78fc202b5b94551225892268d84886aec1caca6c87d275f6dcf9229d
BLAKE2b-256 checksum
How to use checksums
00a8385190a0c7c9126e40cfb1961dd2f7991d6c3c7e41b17dba75963d8ee11e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page