-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred.py
292 lines (251 loc) · 11.1 KB
/
red.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
#pnk.lang.gen;
from tkinter import Tk,Frame,Text,Entry,PanedWindow
from re import finditer,match
from threading import Thread
import subprocess
import datetime
class Red(Tk):
def __init__(self):
super().__init__()
self.configure(bg="#202124")
self.pw = PanedWindow(self,orient="horizontal",sashwidth=2,relief="flat",showhandle=True,sashpad=1,bg="#111")
self.pw.pack(expand=1,fill="both",padx=5,pady=5,)
self.pw.pack(expand=1,fill="both",padx=5,pady=5,)
self.run()
self.bind_all("<Control-R>", self.run)
def run(self,e=None):
self.app = App(self,)
self.pw.add(self.app)
class App(Frame):
def __init__(self,parent,):
super().__init__(parent,)
self.master = parent
self.configure(bg="#202124")
self.iSrcPad = SrcPad(self,)
self.iSrcPad.pack(expand=1,fill="both",padx=5,pady=5,)
class SrcPad(Frame):
def __init__(self,parent,):
super().__init__(parent,)
self.master = parent
self.configure(bg="#202124")
self.srcTxt = Text(self,fg="#DDD",blockcursor=True,bg="#222",cursor="pencil",
font=("VictorMono",16),highlightbackground="#444",highlightcolor="#2BCDBB",
insertbackground="red",relief="flat",padx=20,pady=20,undo=True,wrap="word",width=132)
self.srcTxt.pack(expand=1,fill="y",padx=5,pady=5,)
self.srcTxt.bind("<Shift_R>", self.synThd)
self.srcTxt.bind("<Shift-Return>", self.add_indent)
self.srcTxt.bind("<Control-Return>", self.stay_indent)
self.srcTxt.bind("<Control-l>", self.cleartxt)
self.srcTxt.bind("<Escape>", self.myExit)
self.comEntry = Entry(self,fg="#DDD",bg="#202124",cursor="shuttle",font=("VictorMono",16), width=132,
highlightbackground="#444",highlightcolor="#222",insertbackground="red",relief="flat")
self.comEntry.pack(expand=0,fill="none",padx=5,pady=5,)
self.comEntry.bind("<Return>", self.cliSh)
self.comEntry.bind("<Escape>", self.cliclr)
self.tagConf()
self.myExit()
def add_indent(self, event):
text = self.srcTxt
line = text.get("insert linestart", "insert")
m = match(r"^(\s+)", line)
whitespace = m.group(0) if m else ""
text.insert("insert", f"\n {whitespace}")
return "break"
def stay_indent(self, event):
text = self.srcTxt
line = text.get("insert linestart", "insert")
m = match(r"^(\s+)", line)
whitespace = m.group(0) if m else ""
text.insert("insert", f"\n{whitespace}")
return "break"
def thdStart(self,x,e=None):
self.x = x
thd = Thread(target=x,daemon=True)
thd.start()
def mapitOut(self,e=None):
self.thdStart(self.shell)
def cliclr(self,e=None):
self.comEntry.delete(0,"end")
def myExit(self,e=None):
self.comEntry.focus()
def cliSh(self,e=None):
line = self.comEntry.get()
tt = line.split()
if not tt:
pass
elif tt[0] == "o":
self.openfile()
elif tt[0] == "w":
self.savefile()
elif tt[0] == "d":
self.cleartxt()
self.cliclr()
elif tt[0] == "c":
self.compile()
elif tt[0] == "s":
self.mapitOut()
def clearOut(self,e=None):
self.iShOut.destroy()
self.comEntry.focus()
def compile(self,e=None):
self.thdStart(self.redc)
def shell(self,e=None):
line = self.comEntry.get()
tt = line.split()
cmd = " ".join( str(i) for i in tt[1:])
self.iShOut = ShOut(self,)
self.iShOut.place(x=30,y=30)
self.iShOut.txt.bind("<Escape>", self.clearOut)
self.iShOut.txt.focus()
self.subps(cmd, "1.0")
def subps(self,cmd,idx,e=None):
self.cmd = cmd
self.idx = idx
try:
result = subprocess.run(f"{cmd}",shell=True,executable="/bin/bash",stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
if result.returncode == 0:
self.iShOut.txt.insert(f"{idx}",f"\n\n{result.stdout}\n")
else:
self.iShOut.txt.insert(f"{idx}",f"#$%&*^ {datetime.datetime.now().strftime('<%m.%d.%Y.%H:%M>')} {result.stderr}\n")
except Exception as e:
self.iShOut.txt.insert(f"{idx}",f"#$%&*^ {datetime.datetime.now().strftime('<%m.%d.%Y.%H:%M>')} {str(e)}\n")
def redc(self,e=None):
cmd = f"redc -c {self.file} > .out ; cat .out"
self.cliclr()
self.iShOut = ShOut(self,)
self.iShOut.place(x=30,y=30)
self.iShOut.txt.insert("1.0",f"#$%&*^ {datetime.datetime.now().strftime('<%m.%d.%Y.%H:%M>')} Compiling...Please Wait!\n")
self.iShOut.txt.bind("<Escape>", self.clearOut)
self.iShOut.txt.focus()
self.subps(cmd, "end")
def openfile(self,e=None):
line = self.comEntry.get()
f = line.split()
self.file = f[1]
self.cleartxt()
f = open(f[1], "r")
thefile = f.read()
self.srcTxt.insert("1.0", thefile)
self.synThd()
def savefile(self,e=None):
line = self.comEntry.get()
f = line.split()
self.file = f[1]
data = self.srcTxt.get("1.0","end-1c")
with open(f[1], "w") as file:
file.write(data)
self.synThd()
def cleartxt(self,e=None):
self.srcTxt.delete("1.0","end")
def remTag(self,e=None):
for tag in self.srcTxt.tag_names():
self.srcTxt.tag_remove(tag, "1.0", "end")
self.tagConf()
def synThd(self,e=None):
self.remTag()
self.thdStart(self.doSyntax)
def doSyntax(self,e=None):
self.syntax(self.srcTxt)
def tagConf(self,e=None):
d = { "arg": ("#333", "#eccca2"), "args": ("#444", "#eccca2"), "brc": ("#222", "red"), "brcc": ("#222", "blue"), "paren": ("#222", "orange"), "slash": ("#222", "green"), "parenn": ("#222", "yellow"), "crlb": ("#444", "#eccca2"), "hash": ("#222", "magenta"), "col": ("#222", "#00ffff"), "eql": ("#222", "#00ffff"),"dash": ("#222", "#00ffff"),"larr": ("#222", "red"),"rarr": ("#222", "blue"), "pls": ("#222", "#bfff00"), "star": ("#222", "#bfff00"), "qs": ("#222", "#bfff00"), "dol": ("#222", "green"), "exc": ("#222", "orange"), "nnn": ("#222","orange"), "pct": ("#222","purple"), "smcl": ("#222", "#bfff00"), "dot": ("#222", "pink"), "cm": ("#222", "cyan"), "upar": ("#222", "cyan"),}
for key,value in d.items():
self.srcTxt.tag_configure(key,background=value[0],foreground=value[1])
def tagg(self,x,y,a,b,e=None):
self.x = x
self.y = y
self.a = a
self.b = b
self.x.tag_add(y,f"1.0+{a}c",f"1.0+{b}c")
def syntax(self,xx,e=None):
self.xx = xx
data = self.xx.get("1.0","end-1c")
args_idx = [(m.start(),m.end()) for m in finditer(r'\"(.*?)\"', data)]
for start,end in args_idx:
self.tagg(xx,"args",start,end)
arg_idx = [(m.start(),m.end()) for m in finditer(r'\'', data)]
for start,end in arg_idx:
self.tagg(xx,"arg",start,end)
crlb_idx = [(m.start(),m.end()) for m in finditer(r'\{(.*?)\}', data)]
for start,end in crlb_idx:
self.tagg(xx,"crlb",start,end)
brc_idx = [(m.start(),m.end()) for m in finditer(r'\[', data)]
for start,end in brc_idx:
self.tagg(xx,"brc",start,end)
paren_idx = [(m.start(),m.end()) for m in finditer(r'\(', data)]
for start,end in paren_idx:
self.tagg(xx,"paren",start,end)
parenn_idx = [(m.start(),m.end()) for m in finditer(r'\)', data)]
for start,end in parenn_idx:
self.tagg(xx,"parenn",start,end)
slash_idx = [(m.start(),m.end()) for m in finditer(r'\/', data)]
for start,end in slash_idx:
self.tagg(xx,"slash",start,end)
brcc_idx = [(m.start(),m.end()) for m in finditer(r'\]', data)]
for start,end in brcc_idx:
self.tagg(xx,"brcc",start,end)
hash_idx = [(m.start(),m.end()) for m in finditer(r'#', data)]
for start,end in hash_idx:
self.tagg(xx,"hash",start,end)
col_idx = [(m.start(),m.end()) for m in finditer(r':', data)]
for start,end in col_idx:
self.tagg(xx,"col",start,end)
dash_idx = [(m.start(),m.end()) for m in finditer(r'-', data)]
for start,end in dash_idx:
self.tagg(xx,"dash",start,end)
eql_idx = [(m.start(),m.end()) for m in finditer(r'=', data)]
for start,end in eql_idx:
self.tagg(xx,"eql",start,end)
pls_idx = [(m.start(),m.end()) for m in finditer(r'\+', data)]
for start,end in pls_idx:
self.tagg(xx,"pls",start,end)
star_idx = [(m.start(),m.end()) for m in finditer(r'\*', data)]
for start,end in star_idx:
self.tagg(xx,"star",start,end)
dot_idx = [(m.start(),m.end()) for m in finditer(r'\.', data)]
for start,end in dot_idx:
self.tagg(xx,"dot",start,end)
qs_idx = [(m.start(),m.end()) for m in finditer(r'\?', data)]
for start,end in qs_idx:
self.tagg(xx,"qs",start,end)
dol_idx = [(m.start(),m.end()) for m in finditer(r'\$', data)]
for start,end in dol_idx:
self.tagg(xx,"dol",start,end)
exc_idx = [(m.start(),m.end()) for m in finditer(r'!', data)]
for start,end in exc_idx:
self.tagg(xx,"exc",start,end)
nnn_idx = [(m.start(),m.end()) for m in finditer(r'~', data)]
for start,end in nnn_idx:
self.tagg(xx,"nnn",start,end)
pct_idx = [(m.start(),m.end()) for m in finditer(r'%', data)]
for start,end in pct_idx:
self.tagg(xx,"pct",start,end)
larr_idx = [(m.start(),m.end()) for m in finditer(r'<', data)]
for start,end in larr_idx:
self.tagg(xx,"larr",start,end)
rarr_idx = [(m.start(),m.end()) for m in finditer(r'>', data)]
for start,end in rarr_idx:
self.tagg(xx,"rarr",start,end)
smcl_idx = [(m.start(),m.end()) for m in finditer(r';', data)]
for start,end in smcl_idx:
self.tagg(xx,"smcl",start,end)
dot_idx = [(m.start(),m.end()) for m in finditer(r'\.', data)]
for start,end in dot_idx:
self.tagg(xx,"dot",start,end)
cm_idx = [(m.start(),m.end()) for m in finditer(r'\,', data)]
for start,end in cm_idx:
self.tagg(xx,"cm",start,end)
upar_idx = [(m.start(),m.end()) for m in finditer(r'\^', data)]
for start,end in upar_idx:
self.tagg(xx,"upar",start,end)
class ShOut(Frame):
def __init__(self,parent,):
super().__init__(parent,)
self.master = parent
self.configure(bg="#202124")
self.txt = Text(self,fg="#DDD",blockcursor=True,bg="#222",cursor="pencil",font=("VictorMono",16),highlightbackground="#444",highlightcolor="#2BCDBB",insertbackground="red",relief="flat",padx=20,pady=20,undo=True,wrap="word")
self.txt.pack(expand=0,fill="none",padx=5,pady=5,)
if __name__ == '__main__':
go = Red()
go.title("red-ide")
go.mainloop()