demos/02_curses.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 19:13:37 +0200
changeset 110 cf3d49cdd6e2
parent 95 05392e369ede
child 117 8680c2333546
permissions -rwxr-xr-x
Add cursesw driver, using curses get_wch() for unicode input. It alse has enabled keypad() to let curses interpret control keys and mouse input.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
83
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
92
b97c4e25ed6d Configure logging for demos.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
     3
import demobase
83
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
from tuikit.core.buffer import Buffer
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
from tuikit.driver.curses import CursesDriver
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
     9
colors = ('blue', 'green', 'cyan', 'red',
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    10
          'magenta', 'brown', 'lightgray', 'gray',
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    11
          'lightblue', 'lightgreen', 'lightcyan', 'lightred',
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    12
          'lightmagenta', 'yellow', 'white')
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    13
83
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
buffer = Buffer(16, 8)
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    15
buffer.fill('#')
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    16
buffer.hline('-', 1, 1, 14)
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    17
buffer.hline('-', 1, 6, 14)
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    18
buffer.vline('|', 7, 2, 4)
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    19
buffer.puts('Hello!', 8, 4)
83
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
buffer.frame()
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    22
buffer2 = Buffer(30, 10)
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    23
buffer2.setattr('white on black')
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    24
buffer2.frame()
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    25
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    26
for idx, color in enumerate(colors):
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    27
    buffer2.setattr(color)
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    28
    buffer2.puts(color, 3 + 12 * (idx // 8), 1 + idx % 8)
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    29
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    30
buffer3 = Buffer(30, 10)
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    31
buffer3.setattr('white on black')
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    32
buffer3.frame()
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    33
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    34
for idx, color in enumerate(colors[:8]):
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    35
    buffer3.setattr('black on {}'.format(color))
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    36
    buffer3.puts(' '+color.ljust(10), 2, 1 + idx)
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    37
    buffer3.setattr('{}, underline, bold'.format(color))
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    38
    buffer3.puts(' '+color.ljust(10), 15, 1 + idx)
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    39
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    40
buffer3.setattr('lightgray, blink')
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    41
buffer3.puts('blink', 3, 8)
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    42
83
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
driver = CursesDriver()
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
with driver:
ebe732b9ef19 Refactor Driver, CursesDriver: init, close, draw.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
    driver.draw(buffer)
85
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    46
    driver.draw(buffer2, 20, 00)
6828c5b16087 Add colors to curses demo.
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
    47
    driver.draw(buffer3, 20, 10)
84
04dfb5ddf031 Refactor Driver, CursesDriver: draw, setattr, input.
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
    48
    driver.flush()
04dfb5ddf031 Refactor Driver, CursesDriver: draw, setattr, input.
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
    49
    driver.getevents()