-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_editor.py
126 lines (95 loc) · 3.22 KB
/
text_editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import curses
import curses.ascii
import os
import sys
from audio_app import AudioApp
class TextEditor(AudioApp):
def __init__(self, args):
self.buffer = ''
self.cursor = 0
if len(args) > 1:
path = args[1]
if os.path.exists(path):
f = open(path)
self.buffer = f.read()
f.close()
self.fd = open(path, 'w')
super(TextEditor, self).__init__(args)
def move_left(self):
if self.cursor > 0:
self.cursor -= 1
self.speak_char(self.buffer[self.cursor])
return True
else:
self.play_interval(1, 0.1)
return False
def move_right(self):
if self.cursor < len(self.buffer):
self.speak_char(self.buffer[self.cursor])
self.cursor += 1
return True
else:
self.play_interval(1, 0.1)
return False
def insert_char(self, char):
self.buffer = self.buffer[:self.cursor] + char + self.buffer[self.cursor:]
self.cursor += 1
def remove_char(self):
if self.cursor == 0:
self.play_interval(1, 0.1)
return False
old_char = self.buffer[self.cursor - 1]
self.buffer = self.buffer[:self.cursor - 1] + self.buffer[self.cursor:]
self.cursor -= 1
return old_char
def backspace(self):
deleted = self.remove_char()
if deleted:
self.speak_char(deleted)
def last_word(self, ending_at=None):
if ending_at is None:
ending_at = self.cursor - 1
if self.cursor < 2:
return self.buffer[0]
start = ending_at - 1
while self.buffer[start].isalpha() and start > 0:
start -= 1
return self.buffer[start:ending_at + 1].strip()
def last_sentence(self, ending_at=None):
if ending_at is None:
ending_at = self.cursor - 1
if self.cursor < 2:
return self.buffer[0]
start = ending_at - 1
while self.buffer[start] != '.' and start > 0:
start -= 1
return self.buffer[start:ending_at + 1].strip('. ')
def close(self):
if hasattr(self, 'fd'):
self.fd.write(self.buffer) # write changes to disk
self.fd.close()
def handle_key(self, key):
#sys.stderr.write(str(key))
if key == curses.ascii.ESC:
self.close()
return False # exit
elif key == curses.KEY_LEFT:
self.move_left()
elif key == curses.KEY_RIGHT:
self.move_right()
elif key == curses.ascii.DEL: #curses.KEY_BACKSPACE:
self.backspace()
elif curses.ascii.isalpha(key):
self.insert_char(chr(key))
elif curses.ascii.isprint(key) or curses.ascii.isspace(key):
self.insert_char(chr(key))
if chr(key) == '.':
self.speak(self.last_sentence())
else:
self.speak(self.last_word())
#sys.stderr.write(self.buffer + '\r')
return True # keep reading keystrokes
if __name__ == '__main__':
app = TextEditor(sys.argv)
app.run()