how to develop a rich terminal ui application
TRANSCRIPT
Hello, I’m MASASHI.
Live Broadcasting Engineer in Japan.
c-bata c_bata_! "
I love Django, Pandas and prompt-toolkit ❤
How to develop a rich terminal UI application.
#pyconmy #pyconapac2017
What is python-prompt-toolkit.
go-prompt
golang port is published at:https://github.com/c-bata/go-prompt
1. How to receive a keyboard input
2. How to control the terminal output
Two things you need to know when you develop a rich terminal UI app.
How to receive a keyboard input
$ cat hello.py x = input() print(x)
$ python3 hello.py Hello World Hello World
input() function
Most basic way but some special characters are interpretedex) Ctrl+A, Ctrl+E, F1, F2, etc…
How to receive a keyboard input
$ cat hello.py x = input() print(x)
$ python3 hello.py Hello World Hello World
input() function
Most basic way but some special characters are interpretedex) Ctrl+A, Ctrl+E, F1, F2, etc…
Your input interpreted some special characters.
e.g. Ctrl+A, Ctrl+E, F1, F2, …
How to receive a keyboard input
$ cat hello.py x = input() print(x)
$ python3 hello.py Hello World Hello World
input() function
Most basic way but some special characters are interpretedex) Ctrl+A, Ctrl+E, F1, F2, etc…
And this way cannot receive immediately when user press a something key.
How to receive a keyboard input
passes the data as-is to the program without interpretingany of the special characters.
Raw mode input
See https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/terminal/vt100_input.py
How to change the Raw Mode.
UNIX system calls tcgetattr and tcsetattr is availablefor manipulating termios structure provided by termios
package in standard library.
import termios
def tty_raw(fd): # put terminal into a raw mode buf = termios.tcgetattr(fd) buf[3] &= ~(termios.ECHO|termios.ICANON|termios.IEXTEN| termios.ISIG) buf[0] &= ~(termios.BRKINT|termios.ICRNL|termios.INPCK| termios.ISTRIP|termios.IXON) buf[3] &= ~(termios.CSIZE|termios.PARENB) buf[3] |= termios.CS8 buf[1] &= ~(termios.OPOST) buf[6][termios.VMIN] = 1 buf[6][termios.VTIME] = 0 termios.tcsetattr(fd, termios.TCSAFLUSH, buf)
1. How to receive a keyboard input
2. How to control the terminal output
Two things you need to know when you develop a rich terminal UI app.
VT100 was the first of digital terminal.It can export bold text, italic text,
blinking text and underline in specific words.
What is “VT100 Escape Sequences”
https://en.wikipedia.org/wiki/VT100
What is “Escape Sequences”
What is “Escape Sequences”
Show underline Change background color to Cyan
Change text color to Black
http://www.termsys.demon.co.uk/vtansi.htm
What is “Escape Sequences”
print “Hello”
What is “Escape Sequences”
Reset all attributes.
Summary
Raw mode
VT100 escape sequences
Happy terminal hacking ;)
TERIMA KASIHThank you for listening :)