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. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. video terminals!

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. . 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 Slide

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

    View Slide

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

    View Slide

  17. line discipline

    View Slide

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

    View Slide

  19. 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 Slide

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

    View Slide

  21. (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 Slide

  22. program dissection

    View Slide

  23. program dissection

    View Slide

  24. program dissection

    View Slide

  25. program dissection

    View Slide

  26. program dissection

    View Slide

  27. program dissection

    View Slide

  28. program dissection

    View Slide

  29. program dissection

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. program dissection

    View Slide

  36. 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 Slide