Skip to main content

toggles cbreak mode on terminal

Project description

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=}")

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

toggle_cbreak-0.0.3.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

toggle_cbreak-0.0.3-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file toggle_cbreak-0.0.3.tar.gz.

File metadata

  • Download URL: toggle_cbreak-0.0.3.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for toggle_cbreak-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fac597781400ffc6ae760dbdfb4e68b1523f7ce6aa065c5089c8341f97b54a35
MD5 f664b4a77b0adb1a4eeca50632e932f4
BLAKE2b-256 7f654a1a90430c45e9d991f7f0a847fc99f0f5c1bed1934f05b7319d7d671150

See more details on using hashes here.

File details

Details for the file toggle_cbreak-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: toggle_cbreak-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for toggle_cbreak-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 71d1c9ab78fc202b5b94551225892268d84886aec1caca6c87d275f6dcf9229d
MD5 70323248ba198bca27bee612cce42214
BLAKE2b-256 00a8385190a0c7c9126e40cfb1961dd2f7991d6c3c7e41b17dba75963d8ee11e

See more details on using hashes here.

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