Created
March 25, 2014 18:08
-
-
Save canibanoglu/9767656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=UTF-8 | |
import curses | |
import locale | |
import time | |
locale.setlocale(locale.LC_ALL, '') | |
code = locale.getpreferredencoding() | |
class AddCharCommand(object): | |
def __init__(self, window, line_start, y, x, character): | |
""" | |
Command class for adding the specified character, to the specified | |
window, at the specified coordinates. | |
""" | |
self.window = window | |
self.line_start = line_start | |
self.x = x | |
self.y = y | |
self.character = character | |
def write(self): | |
if self.character > 127: | |
# curses somehow returns a keycode that is 64 lower than what it | |
# should be, this takes care of the problem. | |
self.character += 64 | |
self.string = unichr(self.character).encode(code) | |
self.window.addstr(self.y, self.x, self.string) | |
else: | |
self.window.addch(self.y, self.x, self.character) | |
def delete(self): | |
""" | |
Erase characters usually print two characters to the curses window. | |
As such both the character at these coordinates and the one next to it | |
(that is the one self.x + 1) must be replaced with the a blank space. | |
Move to cursor the original coordinates when done. | |
""" | |
for i in xrange(2): | |
self.window.addch(self.y, self.x + i, ord(' ')) | |
self.window.move(self.y, self.x) | |
def main(screen): | |
maxy, maxx = screen.getmaxyx() | |
q = 0 | |
commands = list() | |
x = 0 | |
erase = ord(curses.erasechar()) | |
while q != 27: | |
q = screen.getch() | |
if q == erase: | |
command = commands.pop(-1).delete() | |
x -= 1 | |
continue | |
command = AddCharCommand(screen, 0, maxy/2, x, q) | |
commands.append(command) | |
command.write() | |
x += 1 | |
curses.wrapper(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment