how to develop a rich terminal ui application

17
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

Upload: masashi-shibata

Post on 23-Jan-2018

2.573 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: How to develop a rich terminal UI application

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

Page 2: How to develop a rich terminal UI application

What is python-prompt-toolkit.

Page 3: How to develop a rich terminal UI application

go-prompt

golang port is published at:https://github.com/c-bata/go-prompt

Page 4: How to develop a rich terminal UI application

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.

Page 5: How to develop a rich terminal UI application

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…

Page 6: How to develop a rich terminal UI application

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, …

Page 7: How to develop a rich terminal UI application

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.

Page 8: How to develop a rich terminal UI application

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

Page 9: How to develop a rich terminal UI application

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)

Page 10: How to develop a rich terminal UI application

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.

Page 11: How to develop a rich terminal UI application

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

Page 12: How to develop a rich terminal UI application

What is “Escape Sequences”

Page 13: How to develop a rich terminal UI application

What is “Escape Sequences”

Show underline Change background color to Cyan

Change text color to Black

http://www.termsys.demon.co.uk/vtansi.htm

Page 14: How to develop a rich terminal UI application

What is “Escape Sequences”

print “Hello”

Page 15: How to develop a rich terminal UI application

What is “Escape Sequences”

Reset all attributes.

Page 16: How to develop a rich terminal UI application

Summary

Raw mode

VT100 escape sequences

Happy terminal hacking ;)

Page 17: How to develop a rich terminal UI application

TERIMA KASIHThank you for listening :)