Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to develop a rich terminal UI application

5,131 views

Published on

talked at #pyconmy #pyconapac2017

Published in: Technology
  • Be the first to comment

  • Be the first to like this

How to develop a rich terminal UI application

  1. 1. 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
  2. 2. What is python-prompt-toolkit.
  3. 3. go-prompt golang port is published at: https://github.com/c-bata/go-prompt
  4. 4. 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.
  5. 5. 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 interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc…
  6. 6. 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 interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc… Your input interpreted some special characters. e.g. Ctrl+A, Ctrl+E, F1, F2, …
  7. 7. 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 interpreted ex) Ctrl+A, Ctrl+E, F1, F2, etc… And this way cannot receive immediately when user press a something key.
  8. 8. How to receive a keyboard input passes the data as-is to the program without interpreting any of the special characters. Raw mode input See https://github.com/jonathanslenders/python-prompt- toolkit/blob/master/prompt_toolkit/terminal/vt100_input.py
  9. 9. How to change the Raw Mode. UNIX system calls tcgetattr and tcsetattr is available for 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)
  10. 10. 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.
  11. 11. 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
  12. 12. What is “Escape Sequences”
  13. 13. What is “Escape Sequences” Show underline Change background color to Cyan Change text color to Black http://www.termsys.demon.co.uk/vtansi.htm
  14. 14. What is “Escape Sequences” print “Hello”
  15. 15. What is “Escape Sequences” Reset all attributes.
  16. 16. Summary Raw mode VT100 escape sequences Happy terminal hacking ;)
  17. 17. TERIMA KASIH Thank you for listening :)

×