-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheditor.py
75 lines (60 loc) · 1.9 KB
/
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
#!/usr/bin/python
# editor.py
# Saito 2017
"""Creates a simple text editor
"""
import curses
from curses.textpad import Textbox
import locale
def emacs_textbox(stdscr, initial_text):
stdscr.clear()
instructions = """
To Save and Exit hit Control-G
This editing buffer uses Emacs commands (No Control-Y though)
*** A command Control-G is == Control + g (don't capitalize) ***
---------------------------------------------------------------
Movement:
Use arrow keys
OR:
Start of line: Control-A
End of line: Control-E
Back Control-B
Forward Control-F
Down line Control-N Cursor down; move down one line.
Previous line Control-P Cursor up; move up one line.
COPY + PASTE: Use mouse + keyboard shortcuts to copy and paste
Deletion:
Delete under cursor Control-D
Delete backwards Control-H
Kill line Control-K
"""
stdscr.addstr(instructions)
stdscr.refresh()
ending = """------------------------------------------------------\n
EDIT BELOW ONLY
------------------------------------------------------\n"""
stdscr.addstr(ending)
stdscr.refresh()
stdscr.addstr(initial_text)
stdscr.refresh()
box = Textbox(stdscr, insert_mode=False) # Inf recursion bug when True
box.edit()
message = box.gather()
remove_index = len(ending) + len(instructions)
return message[remove_index + 15:]
def create_editor(initial_text):
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()
initial_text = initial_text.encode(code, 'replace') # or 'ignore'
msg = curses.wrapper(emacs_textbox, initial_text)
return msg
def main():
initial_text = u"""
This is my po\xe9m
It is not very clever
But I'm fond of it
"""
msg = create_editor(initial_text)
print msg
if __name__ == '__main__':
main()