busted first stab at python version of simple_attr example

This commit is contained in:
Dan Buch 2009-11-21 11:35:38 -05:00
parent e396c88fd1
commit 2cb096e27e

48
basics/simple_attr.py Normal file
View File

@ -0,0 +1,48 @@
from __future__ import print_function
import sys
import curses
FILE = {'fp': None}
def simple_attr(stdscr):
row, col = stdscr.getmaxyx()
prev = ''
for ch in FILE['fp'].read():
y, x = stdscr.getyx()
if y == (row - 1):
stdscr.addstr(row - 1, 0, "<-Press Any Key->")
stdscr.getch()
stdscr.clear()
stdscr.move(0, 0)
if prev == '/' and ch == '*':
stdscr.attron(curses.A_BOLD)
y, x = stdscr.getyx()
stdscr.addstr(y, x - 1, '/{0}'.format(ch))
else:
stdscr.addstr(y, x, ch)
stdscr.refresh()
if prev == '*' and ch == '/':
stdscr.attroff(curses.A_BOLD)
prev = ch
def main(sysargs=sys.argv[:]):
if not sysargs[1:]:
print('Usage: {0} <a c file name>'.format(sysargs[0]))
return 1
try:
FILE['fp'] = open(sysargs[1], 'r')
except (OSError, IOError):
print('Cannot open input file', file=sys.stderr)
return 1
curses.wrapper(simple_attr)
return 0
if __name__ == '__main__':
sys.exit(main())
# vim:filetype=python