Skip to content

Instantly share code, notes, and snippets.

Created May 25, 2013 16:28
Show Gist options
  • Save anonymous/5649647 to your computer and use it in GitHub Desktop.
Save anonymous/5649647 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
# I've been asked "Why use iso8859-1 encoding" in terminal applications,
for n in range(256):
# all possible 8-bit keyboard input as bytes
byte = bytes([n])
# assert 8-bit keyboard input value is unmodified by iso8859-1
assert chr(n).encode('iso8859-1') == byte
# utf-8 encoding, though, could confuse the utf-8 decoder for 8-bit input,
try:
byte.decode('utf-8')
except UnicodeDecodeError as err:
assert 0x80 <= n <= 0xff, err
# properly, one would simply decode these bits using the preferred session
# encoding. However, for non-utf8 input, 8-bits can still be sent
# when the meta key is depressed, by flipping the 8th bit. If you were to provide
# for example, an emacs interface for binding keys, you would want to know
# only the byte value, and not the codepoint value translated by codepage.
if n > 127:
# non-ascii input: when 'meta is high-bit' is toggled by a client
# application (as opposed to 'meta sends escape'), then these
# characters represent meta
key = chr(n & 0x7f)
if n % 8 == 0:
sys.stdout.write('\n{:3d}: '.format(n))
sys.stdout.write('M-{!r:6} '.format(key))
sys.stdout.write('\n')
# For more information, http://www.leonerd.org.uk/hacks/hints/xterm-8bit.html is nice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment