Last active
November 21, 2017 12:26
-
-
Save joar/4ab6d0f52193b73d9088c7c9239db77b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Terminal utilities.""" | |
# Author: Steen Lumholt. | |
from termios import * | |
__all__ = ["setraw", "setcbreak"] | |
# Indexes for termios list. | |
IFLAG = 0 | |
OFLAG = 1 | |
CFLAG = 2 | |
LFLAG = 3 | |
ISPEED = 4 | |
OSPEED = 5 | |
CC = 6 | |
def setraw(fd, when=TCSAFLUSH): | |
"""Put terminal into a raw mode.""" | |
mode = tcgetattr(fd) | |
mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON) | |
mode[OFLAG] = mode[OFLAG] & ~(OPOST) | |
mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB) | |
mode[CFLAG] = mode[CFLAG] | CS8 | |
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG) | |
mode[CC][VMIN] = 1 | |
mode[CC][VTIME] = 0 | |
tcsetattr(fd, when, mode) | |
def setcbreak(fd, when=TCSAFLUSH): | |
"""Put terminal into a cbreak mode.""" | |
mode = tcgetattr(fd) | |
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON) | |
mode[CC][VMIN] = 1 | |
mode[CC][VTIME] = 0 | |
tcsetattr(fd, when, mode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IFLAG ICRNL | |
OFLAG OPOST | |
CFLAG CSIZE | |
CFLAG CS8 | |
LFLAG ECHO | |
LFLAG ICANON | |
LFLAG IEXTEN | |
LFLAG ISIG |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAG CSIZE | |
CFLAG CS8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ttyattrs-default.txt b/ttyattrs-raw.txt | |
index 63294d6..fcf2255 100644 | |
--- a/ttyattrs-default.txt | |
+++ b/ttyattrs-raw.txt | |
@@ -1,8 +1,2 @@ | |
-IFLAG ICRNL | |
-OFLAG OPOST | |
CFLAG CSIZE | |
CFLAG CS8 | |
-LFLAG ECHO | |
-LFLAG ICANON | |
-LFLAG IEXTEN | |
-LFLAG ISIG |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tty, sys, termios, io | |
import difflib | |
# Indexes for termios list. | |
IFLAG = 0 | |
OFLAG = 1 | |
CFLAG = 2 | |
LFLAG = 3 | |
ISPEED = 4 | |
OSPEED = 5 | |
CC = 6 | |
checks = { | |
IFLAG: ('BRKINT', 'ICRNL', 'INPCK', 'ISTRIP', 'IXON'), | |
OFLAG: ('OPOST',), | |
CFLAG: ('CSIZE', 'PARENB', 'CS8'), | |
LFLAG: ('ECHO', 'ICANON', 'IEXTEN', 'ISIG') | |
} | |
attr_name = { | |
IFLAG: 'IFLAG', | |
OFLAG: 'OFLAG', | |
CFLAG: 'CFLAG', | |
LFLAG: 'LFLAG', | |
ISPEED: 'ISPEED', | |
OSPEED: 'OSPEED', | |
CC: 'CC', | |
} | |
def print_flags(tcattrs, fd=sys.stdout): | |
for attr_i, flag_names in checks.items(): | |
for flag_name in flag_names: | |
result = tcattrs[attr_i] & getattr(termios, flag_name) | |
is_set = result > 0 | |
# message = f'{attr_name[attr_i]} {flag_name} = {is_set} ({result})' | |
message = None | |
if is_set: | |
message = f'{attr_name[attr_i]} {flag_name}' | |
if message is not None: | |
print(message, file=fd) | |
default = termios.tcgetattr(sys.stdout) | |
tty.setraw(sys.stdout) | |
raw = termios.tcgetattr(sys.stdout) | |
# termios.tcsetattr(sys.stdout, termios.TCSAFLUSH, default) | |
print_flags(default, open('ttyattrs-default.txt', 'w')) | |
print_flags(raw, open('ttyattrs-raw.txt', 'w')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import termios | |
import time | |
import tty | |
import sys | |
from termios import * | |
# Indexes for termios list. | |
IFLAG = 0 | |
OFLAG = 1 | |
CFLAG = 2 | |
LFLAG = 3 | |
ISPEED = 4 | |
OSPEED = 5 | |
CC = 6 | |
def print_stuff(): | |
for i in range(5): | |
print(f'stuff {i}') | |
def modify_attrs(i, func): | |
attrs = termios.tcgetattr(sys.stdout) | |
attrs[i] = func(attrs[i]) | |
termios.tcsetattr(sys.stdout, TCSAFLUSH, attrs) | |
print_stuff() | |
#modify_attrs(OFLAG, lambda x: x & ~OPOST) | |
tty.setraw(sys.stdout) | |
# print_stuff() | |
# modify_attrs(OFLAG, lambda x: x | OPOST) | |
# modify_attrs(LFLAG, lambda x: x | ICANON | ECHO) | |
# print_stuff() | |
import pdb; pdb.set_trace() | |
# Observed attr changes by tty.setraw | |
# diff --git a/ttyattrs-default.txt b/ttyattrs-raw.txt | |
# index 63294d6..fcf2255 100644 | |
# --- a/ttyattrs-default.txt | |
# +++ b/ttyattrs-raw.txt | |
# @@ -1,8 +1,2 @@ | |
# -IFLAG ICRNL | |
# -OFLAG OPOST | |
# CFLAG CSIZE | |
# CFLAG CS8 | |
# -LFLAG ECHO | |
# -LFLAG ICANON | |
# -LFLAG IEXTEN | |
# -LFLAG ISIG | |
# Full | |
mode[IFLAG] = mode[IFLAG] | (BRKINT | ICRNL | INPCK | ISTRIP | IXON) | |
mode[OFLAG] = mode[OFLAG] | (OPOST) | |
mode[CFLAG] = mode[CFLAG] | (CSIZE | PARENB) | |
mode[LFLAG] = mode[LFLAG] | (ECHO | ICANON | IEXTEN | ISIG) | |
# Based on observed | |
mode[IFLAG] = mode[IFLAG] | ICRNL | |
mode[OFLAG] = mode[OFLAG] | (OPOST) | |
mode[LFLAG] = mode[LFLAG] | (ECHO | ICANON | IEXTEN | ISIG) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment