-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesktopNote.py
244 lines (221 loc) · 7.15 KB
/
DesktopNote.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import cmd
import os
import pickle
TEXT: list[str] = []
class TextRevise(cmd.Cmd):
prompt = '<revise>'
x = 0
y = 0
def screen(self):
os.system('cls')
for _i, _x in enumerate(TEXT):
if _i == self.y:
if TEXT[self.y] == '':
print(str(_i) + '>', '><')
else:
print(
str(_i) + '>',
(
TEXT[self.y][:self.x]
+ '>' + TEXT[self.y][self.x] + '<'
+ TEXT[self.y][self.x + 1:]
)
)
else:
print(str(_i) + '>', _x)
def do_mv(self, arg):
'''Move to a location
mv <row_order :: int> <char_order :: int>'''
arg = [int(x) for x in arg.split()]
self.x = arg[1]
self.y = arg[0]
self.screen()
def do_r(self, arg):
'''Right move
r <length :: int>'''
self.x += int(arg)
self.screen()
def do_l(self, arg):
'''Left move
l <length :: int>'''
self.x -= int(arg)
self.screen()
def do_u(self, arg):
'''Up move
u <length :: int>'''
self.y -= int(arg)
self.screen()
def do_d(self, arg):
'''Down move
d <length :: int>'''
self.y += int(arg)
self.screen()
def do_rm(self, arg):
'''Delete a char or chars
rm [[<row_order :: int> <char_order :: int> [<length :: int>]] | [<length>]]'''
arg = [int(x) for x in arg.split()]
_leng = len(arg)
# Delete string
if _leng == 0:
TEXT[self.y] = TEXT[self.y][:self.x] + TEXT[self.y][self.x + 1:]
elif _leng == 1:
_l = arg[0]
TEXT[self.y] = TEXT[self.y][:self.x] + TEXT[self.y][self.x + _l:]
elif _leng == 2:
_x = arg[1]
_y = arg[0]
TEXT[_y] = TEXT[_y][:_x] + TEXT[_y][_x + 1:]
elif _leng == 3:
_x = arg[1]
_y = arg[0]
_l = arg[2]
TEXT[_y] = TEXT[_y][:_x] + TEXT[_y][_x + _l:]
# Move pointer
if _leng == 2 or _leng == 3:
self.x = _x
self.y = _y
_leng_s = len(TEXT[self.y])
if self.x >= _leng_s:
if _leng_s == 0:
self.x = 0
else:
self.x = _leng_s - 1
self.screen()
def do_ad(self, arg):
'''Add a string on the right side
ad [<row_order :: int> <char_order :: int>]'''
arg = [int(x) for x in arg.split()]
_leng = len(arg)
# Add string
_s = input('Added string: ')
if _leng == 0:
TEXT[self.y] = TEXT[self.y][:self.x + 1] + _s + TEXT[self.y][self.x + 1:]
elif _leng == 2:
_x = arg[1]
_y = arg[0]
TEXT[_y] = TEXT[_y][:_x + 1] + _s + TEXT[_y][_x + 1:]
# Move pointer
_leng_s = len(_s)
if _leng == 2:
self.x = _x
self.y = _y
self.x += _leng_s
self.screen()
def do_rv(self, arg): # (x, y)或无, 将一个字符修改; (x, y, l)或l, 将 l 个字符修改
'''Revise a char or chars
rv [[<row_order :: int> <char_order :: int> [<length :: int>]] | [<length>]]'''
arg = [int(x) for x in arg.split()]
_leng = len(arg)
# Revise string
_s = input('New string: ')
if _leng == 0:
TEXT[self.y] = TEXT[self.y][:self.x] + _s + TEXT[self.y][self.x + 1:]
elif _leng == 1:
_l = arg[0]
TEXT[self.y] = TEXT[self.y][:self.x] + _s + TEXT[self.y][self.x + _l:]
elif _leng == 2:
_x = arg[1]
_y = arg[0]
TEXT[_y] = TEXT[_y][:_x] + _s + TEXT[_y][_x + 1:]
elif _leng == 3:
_x = arg[1]
_y = arg[0]
_l = arg[2]
TEXT[_y] = TEXT[_y][:_x] + _s + TEXT[_y][_x + _l:]
# Move pointer
_leng_s = len(_s)
if _leng == 2 or _leng == 3:
self.x = _x
self.y = _y
_leng_ss = TEXT[self.y]
if self.x >= len(TEXT[self.y]):
if _leng_ss == 0:
self.x = 0
else:
self.x = _leng_ss - 1
else:
self.x += _leng_s - 1
self.screen()
def do_ex(self, arg):
'''Exit revising shell
ex'''
return True
class Note(cmd.Cmd):
intro = '''
A notebook used on the desktop in a cmd interface.
==========================================================
'''
prompt = '<note>'
try:
fnote = open('note.dat', 'rb')
except BaseException:
fnote = open('note.dat', 'wb')
pickle.dump([], fnote)
fnote.close()
fnote = open('note.dat', 'rb')
note: list[list[str, list[str]]] = pickle.load(fnote)
fnote.close()
def do_show(self, arg):
'''Show the notes
show [<note_order :: int>]'''
os.system('cls')
print(' ==========================================================')
try:
arg = int(arg)
print('Title:', arg, self.note[arg][0])
print('Text:')
for i, s in enumerate(self.note[arg][1]):
print(str(i) + '>', s)
print(' ==========================================================')
except BaseException:
for i, x in enumerate(self.note):
print('Title:', i, x[0])
print('Text:')
for j, s in enumerate(x[1]):
print(str(j) + '>', s)
print(' ==========================================================')
def do_write(self, arg):
'''Write a new note
write'''
temp_title = input('New note title: ')
temp_text: list[str] = []
print('New note text(end with END):')
while (s := input()) != 'END':
temp_text.append(s)
self.note.append([temp_title, temp_text])
self.fnote = open('note.dat', 'wb')
pickle.dump(self.note, self.fnote)
self.fnote.close()
def do_revise(self, arg):
'''Revise a note
revise <note_order :: int>'''
arg = int(arg)
global TEXT
TEXT = self.note[arg][1]
for i, x in enumerate(self.note[arg][1]):
if i == 0:
if x == '':
print('0>', '><')
else:
print('0>', '>' + x[0] + '<' + x[1:])
print(str(i) + '>', x)
TextRevise().cmdloop()
self.note[arg][1] = TEXT
self.fnote = open('note.dat', 'wb')
pickle.dump(self.note, self.fnote)
self.fnote.close()
def do_delete(self, arg):
'''Delete a note
delete <note_order :: int>'''
arg = int(arg)
self.note.pop(arg)
self.fnote = open('note.dat', 'wb')
pickle.dump(self.note, self.fnote)
self.fnote.close()
def do_exit(self, arg):
'''Exit DesktopNote
exit'''
return exit()
def postcmd(self, stop: bool, line: str) -> bool:
return False
Note().cmdloop()