Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Use curses, don't swear by Sean Zicari

PyCon 2013
March 18, 2013

Use curses, don't swear by Sean Zicari

PyCon 2013

March 18, 2013
Tweet

More Decks by PyCon 2013

Other Decks in Technology

Transcript

  1. Use curses...
    don't swear
    an overview of terminal control in python

    View full-size slide

  2. . Web Developer
    . Love all things low level
    . Love computer history
    whoami

    View full-size slide

  3. . Web Developer
    . Love all things low level
    . Love computer history
    whoami

    View full-size slide

  4. . Web Developer
    . Love all things low level
    . Love computer history
    whoami

    View full-size slide

  5. . Cars.com
    . PyCon sponsor
    . Python/Postgres shop
    . Awesome place to work!
    where do I work

    View full-size slide

  6. . Cars.com
    . PyCon sponsor
    . Python/Postgres shop
    . Awesome place to work!
    where do I work

    View full-size slide

  7. . Cars.com
    . PyCon sponsor
    . Python/Postgres shop
    . Awesome place to work!
    where do I work

    View full-size slide

  8. . Cars.com
    . PyCon sponsor
    . Python/Postgres shop
    . Awesome place to work!
    where do I work

    View full-size slide

  9. video terminals!

    View full-size slide

  10. . First VT ~1969
    . Basic line/screen control
    . ASCII character set
    . ~300 baud (characters/second)
    video terminals!

    View full-size slide

  11. . First VT ~1970
    . Basic line control
    . ASCII character set
    . ~300 baud (characters/second)
    video terminals!
    VT-100

    View full-size slide

  12. . Kernel handled data transfer
    . Terminal handled display
    (cursor, color, line handling...)
    . Terminal control through escape
    sequences
    talking to a terminal

    View full-size slide

  13. . Vendor-specific escape sequences
    . ECMA-48 adopted in 1976
    . termcap released in 1978
    (TERMinal CAPability)
    standards? nope.
    Esc[20h Set new line mode LMN
    Esc[?1h Set cursor key to
    application
    DECCKM
    none Set ANSI (versus
    VT52)
    DECANM
    Esc[?3h Set number of
    columns to 132
    DECCOLM
    Esc[?4h Set smooth scrolling DECSCLM
    Esc[?5h Set reverse video on
    screen
    DECSCNM
    Esc[?6h Set origin to relative DECOM
    Esc[?7h Set auto-wrap mode DECAWM
    Esc[?8h Set auto-repeat mode DECARM
    Esc[?9h Set interlacing mode DECINLM

    View full-size slide

  14. . Vendor-specific escape sequences
    . ECMA-48 adopted in 1976
    . termcap released in 1978
    (TERMinal CAPability)
    standards? nope.

    View full-size slide

  15. . Vendor-specific escape sequences
    . ECMA-48 adopted in 1976
    . termcap released in 1978
    (TERMinal CAPability)
    standards? nope.

    View full-size slide

  16. line discipline

    View full-size slide

  17. line discipline
    . Glue code
    . No execution context
    . line modes: "raw" or "cooked"

    View full-size slide

  18. introducing (n)curses
    . Developed for BSD
    . Code borrowed from vi
    . Originally used termcap
    . Unix System III / V
    . Gained additional features
    . Curses became pcurses
    . pcurses became n(ew)curses

    View full-size slide

  19. why (n)curses?
    . Plethora of terminals
    . Plethora of escape sequence variations
    . Portable
    . Convenient abstractions
    . Standardized API

    View full-size slide

  20. (n)curses philosophy
    . Tools provided...
    . ...but you're on your own
    . One primary object: stdscr
    . Full cursor/screen control
    . Forms, windows, colors, cbreak mode
    . Manual Screen refresh

    View full-size slide

  21. program dissection

    View full-size slide

  22. program dissection

    View full-size slide

  23. program dissection

    View full-size slide

  24. program dissection

    View full-size slide

  25. program dissection

    View full-size slide

  26. program dissection

    View full-size slide

  27. program dissection

    View full-size slide

  28. program dissection

    View full-size slide

  29. . Pronounced "oor'-wid"
    . Layered architecture
    . Display modules
    . Widget layout
    . Event loops
    introducing urwid

    View full-size slide

  30. . Pronounced "oor'-wid"
    . Layered architecture
    . Display modules
    . Widget layout
    . Event loops
    introducing urwid

    View full-size slide

  31. . Pronounced "oor'-wid"
    . Layered architecture
    . Display modules
    . Widget layout
    . Event loops
    introducing urwid

    View full-size slide

  32. . Pronounced "oor'-wid"
    . Layered architecture
    . Display modules
    . Widget layout
    . Event loops
    introducing urwid

    View full-size slide

  33. . Pronounced "oor'-wid"
    . Layered architecture
    . Display modules
    . Widget layout
    . Event loops
    introducing urwid

    View full-size slide

  34. program dissection

    View full-size slide

  35. resources
    Python Curses
    -------------
    . Documentation
    http://docs.python.org/2/library/curses.html
    . Tutorial
    http://docs.python.org/2/howto/curses.html
    Urwid
    -----
    . Documentation
    http://excess.org/urwid/docs/
    . Tutorials
    http://excess.org/urwid/docs/tutorial/index.html#urwid-tutorial
    . Manual
    http://excess.org/urwid/docs/manual/index.html#urwid-manual

    View full-size slide