by shigemk2

当面は技術的なことしか書かない

ターミナルの復権:隠れた名ライブラリcursesを使いこなす

Playing with curses

curses
端末に依存しない制御
もっと抽象化されたインターフェースを提供してくれる
基本はCのライブラリ

import curses
import locale

def main(stdscr):
    stdscr.clear()
    stdscr.border()
    stdscr.addstr(1, 1, "Hello World", curses.A_BOLD)
    stdscr.refresh()

    while True:
        if stdscr.getch() == 0x1b:
            break

        curses.flash()

if __name__ == "__main__":
    locale.setlocale(locale.LC_ALL, "")
    curses.wrapper(main)

locale.setlocal(locale.LC_ALL, "")
非ASCIIなバイトシーケンスをどう扱うか
LC_ALLを空文字列にセットするとシステムのデフォルトが使われる

curses.wrapper()
端末の初期化とか色々やってくれる

チュートリアル
Python で Curses プログラミング — Python 2.7ja1 documentation