-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextEditor.py
More file actions
387 lines (337 loc) · 17.5 KB
/
Copy pathtextEditor.py
File metadata and controls
387 lines (337 loc) · 17.5 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import font
import json, random, os, importlib
# Window initialize
window = tk.Tk()
# window.resizable(False, False)
window.title("Text editor")
# Settings
CONFIG_FILE = "textEditor.cfg"
DEFAULT_CFG = {
'font' : 'Consolas',
'fontSize' : '17' ,
'fontDeco' : 'normal' ,
'bgColor' : 'Black' ,
'fgColor' : 'Green' ,
'cuColor' : 'Red' ,
'disco' : 'false' ,
'syntax' : 'Text' ,
"color_Keywords" : "Pink" ,
"color_Symbols" : "Cyan" ,
"color_Operators": "Grey" ,
"color_Variables": "Red" ,
"color_Comments" : "Darkgrey",
}
cfg = dict(DEFAULT_CFG)
# Config is loaded as JSON (previously used exec() on a .cfg file, which was
# arbitrary-code-execution from disk). Old Python-style .cfg files will fail
# to parse and fall back to defaults.
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
loaded = json.load(f)
if isinstance(loaded, dict):
cfg.update({k: v for k, v in loaded.items() if k in DEFAULT_CFG})
except (OSError, ValueError) as e:
print(f"[WARNING] Could not read {CONFIG_FILE} as JSON: {e}. Using defaults.")
# ---------------------------------------------------------------------------- #
# Constants #
# ---------------------------------------------------------------------------- #
AVAILABLE_COLORS = [
"Red",
"Green",
"Blue",
"Black",
"White",
"Grey",
"Cyan",
"Brown",
"Yellow",
"Orange",
]
AVAILABLE_FONTS = list(font.families())
AVAILABLE_LANGS = [
"Text",
"PHP",
"JavaScript",
"Python"
]
FILETYPES = {
".py": "Python",
".php": "PHP",
".js": "JavaScript",
".ts": "TypeScript",
".txt": "Text",
}
SYNTAX = {}
SYNTAX["PHP"] = {
# TYPE: [COLOR, [SEARCHFOR]]
"Keywords": [cfg["color_Keywords"], ["if", "else", "while", "foreach", "for", "echo", "function"]],
"Symbols": [cfg["color_Symbols"], ["<?php", "?>", "{", "}", "[", "]", "(", ")", ";", ",", "."]],
"Operators": [cfg["color_Operators"], ["==", ">", ">=", "<=", "<", "!=", "===", "!==", "="]],
"Variables": [cfg["color_Variables"], ["$"]],
"Comments": [cfg["color_Comments"], ["//", "#"]]
}
SYNTAX["Python"] = {
# TYPE: [COLOR, [SEARCHFOR]]
"Keywords": [cfg["color_Keywords"], ["if", "else", "elif", "while", "for", "print", "True", "False", "def"]],
"Symbols": [cfg["color_Symbols"], [":", "?>", "{", "}", "[", "]", "(", ")", ";", "\""]],
"Operators": [cfg["color_Operators"], ["==", ">", ">=", "<=", "<", "!=", "="]],
"Variables": [cfg["color_Variables"], []],
"Comments": [cfg["color_Comments"], ["#"]]
}
# ---------------------------------------------------------------------------- #
# SYNTAX HIGHLIGHTER #
# ---------------------------------------------------------------------------- #
def highlight(textarea):
if cfg["syntax"] != "Text":
try:
syntax = SYNTAX[cfg["syntax"]]
highlightWords = {}
for type in syntax:
color = syntax[type][0]
searchfor = syntax[type][1]
for search in searchfor:
highlightWords[search] = color
'''the highlight function, called when a Key-press event occurs'''
for k,v in highlightWords.items(): # iterate over dict
startIndex = "1.0"
while True:
startIndex = textarea.search(k, startIndex, tk.END) # search for occurence of k
if startIndex:
endIndex = textarea.index('%s+%dc' % (startIndex, (len(k)))) # find end of k
textarea.tag_add(k, startIndex, endIndex) # add tag to k
textarea.tag_config(k, foreground=v) # and color it with v
startIndex = endIndex # reset startIndex to continue searching
else:
break
except:
"""Syntax is probably set to text, or something"""
textarea.config(background=cfg['bgColor'], foreground=cfg['fgColor'], insertbackground=cfg['cuColor'], font=(cfg['font'], cfg['fontSize'], cfg['fontDeco']))
# ---------------------------------------------------------------------------- #
# OPEN FILE #
# ---------------------------------------------------------------------------- #
def open_file():
"""Open a file for editing."""
filepath = askopenfilename(
filetypes=[("All Files", "*.*"), ("Text Files", "*.txt")]
)
if not filepath:
return
textarea.delete("1.0", tk.END)
with open(filepath, mode="r", encoding="utf-8") as input_file:
text = input_file.read()
textarea.insert(tk.END, text)
fileName.config(text=filepath)
# Update syntax highlight
for extension in FILETYPES:
if filepath.endswith(extension):
cfg["syntax"] = FILETYPES[extension]
updateSettingsOverview()
status.config(text=f"[FILE OPENED]", foreground="Green")
# ---------------------------------------------------------------------------- #
# SAVE FILE #
# ---------------------------------------------------------------------------- #
def save_file(fn = None):
"""Save the current file as a new file."""
if fn == None or fn == "No file opened.":
fileType = list(FILETYPES.keys())[list(FILETYPES.values()).index(cfg["syntax"])] or "*.txt"
filepath = asksaveasfilename(
defaultextension=fileType,
filetypes=[(f"{cfg['syntax']} file", f"*{fileType}"), ("All Files", "*.*")],
)
else:
filepath = fn
if not filepath:
return
with open(filepath, mode="w", encoding="utf-8") as output_file:
text = textarea.get("1.0", tk.END)
output_file.write(text)
fileName.config(text=filepath)
# Update syntax highlight
for extension in FILETYPES:
if filepath.endswith(extension):
cfg["syntax"] = FILETYPES[extension]
updateSettingsOverview()
status.config(text=f"[FILE SAVED]", foreground="Green")
# ---------------------------------------------------------------------------- #
# TOGGLE DARK MODE #
# ---------------------------------------------------------------------------- #
def toggleDark():
global cfg
if cfg["bgColor"] == "Black":
cfg["bgColor"] = "White"
cfg['fgColor'] = "Black"
textarea.config(background=cfg["bgColor"], foreground=cfg["fgColor"])
toggleDarkBtn.config(text="🌚 Dark mode")
elif cfg["bgColor"] == "White":
cfg["bgColor"] = "Black"
cfg['fgColor'] = "Green"
textarea.config(background=cfg["bgColor"], foreground=cfg["fgColor"])
toggleDarkBtn.config(text="🌞 Light mode")
else:
cfg["bgColor"] = "Black"
cfg['fgColor'] = "Green"
textarea.config(background=cfg["bgColor"], foreground=cfg["fgColor"])
toggleDarkBtn.config(text="🌞 Light mode")
status.config(text="[DARK MODE TOGGLED]", foreground="Green")
# ---------------------------------------------------------------------------- #
# UPDATE SETTINGSOVERVIEW #
# ---------------------------------------------------------------------------- #
def updateSettingsOverview():
settingsOverviewText = f"[{cfg['syntax']}] [{cfg['font']}] [{cfg['fontSize']}px] [{cfg['fontDeco']}]"
settingsOverview.config(text = settingsOverviewText)
highlight(textarea)
# ---------------------------------------------------------------------------- #
# SAVE SETTINGS #
# ---------------------------------------------------------------------------- #
def saveSettings(savecfg, windowReference = None):
global settingsStatus
try:
settingsStatus
except:
print("settingsStatus not set yet")
else:
settingsStatus.destroy()
try:
for settingName in savecfg:
if len(savecfg[settingName].get()) > 0:
cfg[settingName] = savecfg[settingName].get()
print(f"Setting {settingName} = {cfg[settingName]}")
with open(CONFIG_FILE, "w", encoding="utf-8") as configFile:
json.dump(cfg, configFile, indent=2)
textarea.config(background=cfg['bgColor'], foreground=cfg['fgColor'], insertbackground=cfg['cuColor'], font=(cfg['font'], cfg['fontSize'], cfg['fontDeco']))
status.config(text="[SETTINGS SAVED]", foreground="Green")
updateSettingsOverview()
# retoggle highlight
highlight(textarea)
windowReference.destroy()
except Exception as e:
settingsStatus = tk.Label(windowReference, text=f"Error: {e}", foreground="Red")
settingsStatus.pack()
# ---------------------------------------------------------------------------- #
# SETTINGS WINDOW #
# ---------------------------------------------------------------------------- #
def settings():
global cfg
availableSettings = [
["Font" , "font" , AVAILABLE_FONTS] ,
["Font size" , "fontSize" , "str"] ,
["Font decoration" , "fontDeco" , "str"] ,
["Background color", "bgColor" , AVAILABLE_COLORS],
["Text color" , "fgColor" , AVAILABLE_COLORS],
["Cursor color" , "cuColor" , AVAILABLE_COLORS],
["Syntax highlight", "syntax" , AVAILABLE_LANGS] ,
["Disco mode" , "disco" , "bool"] ,
["Code Keywords" , "color_Keywords" , AVAILABLE_COLORS],
["Code Symbols" , "color_Symbols" , AVAILABLE_COLORS],
["Code Operators" , "color_Operators", AVAILABLE_COLORS],
["Code Variables" , "color_Variables", AVAILABLE_COLORS],
["Code Comments" , "color_Comments" , AVAILABLE_COLORS],
]
savecfg = {} # the temp for storing about to be saved cfg
showcfg = {} # only for display
# Settings window initialize
settingsWindow = tk.Toplevel(window)
settingsWindow.grab_set()
settingsWindow.resizable(False, False)
# settingsWindow.geometry('500x500')
settingsWindow.title("Settings")
for i, setting in enumerate(availableSettings):
cfgFormalName = setting[0]
cfgName = setting[1]
cfgType = setting[2]
currentVal = cfg[cfgName]
tk.Label(settingsWindow, text=cfgFormalName).grid(column=0, row=i)
savecfg[cfgName] = tk.StringVar(settingsWindow)
if cfgType == "str":
savecfg[cfgName] = tk.Entry(settingsWindow)
savecfg[cfgName].insert(0, currentVal)
showcfg[cfgName] = savecfg[cfgName]
elif cfgType == "bool":
savecfg[cfgName].set(cfg[cfgName])
showcfg[cfgName] = tk.Checkbutton(settingsWindow, text=cfgFormalName,variable=savecfg[cfgName], onvalue='true', offvalue='false')
elif type(cfgType) == list:
savecfg[cfgName].set(cfg[cfgName])
showcfg[cfgName] = tk.OptionMenu(settingsWindow, savecfg[cfgName], cfg[cfgName], *cfgType)
showcfg[cfgName].config(indicatoron=0, compound=tk.RIGHT, image=img_down, width=120)
showcfg[cfgName].grid(column=1,row=i)
settingsWindow.bind("<Return>", lambda event: saveSettings(savecfg, settingsWindow))
tk.Button(settingsWindow, text="💾 Save", command=lambda: saveSettings(savecfg, settingsWindow)).grid(column=1,row=i+1)
# ---------------------------------------------------------------------------- #
# HELP WINDOW #
# ---------------------------------------------------------------------------- #
def help():
helpWindow = tk.Toplevel(window)
helpWindow.grab_set()
helpWindow.resizable(False, False)
helpWindow.geometry('700x500')
helpWindow.title("Help")
helpText = tk.Text(helpWindow)
text = """
████████╗███████╗██╗░░██╗████████╗███████╗██████╗░██╗████████╗░█████╗░██████╗░
╚══██╔══╝██╔════╝╚██╗██╔╝╚══██╔══╝██╔════╝██╔══██╗██║╚══██╔══╝██╔══██╗██╔══██╗
░░░██║░░░█████╗░░░╚███╔╝░░░░██║░░░█████╗░░██║░░██║██║░░░██║░░░██║░░██║██████╔╝
░░░██║░░░██╔══╝░░░██╔██╗░░░░██║░░░██╔══╝░░██║░░██║██║░░░██║░░░██║░░██║██╔══██╗
░░░██║░░░███████╗██╔╝╚██╗░░░██║░░░███████╗██████╔╝██║░░░██║░░░╚█████╔╝██║░░██║
░░░╚═╝░░░╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚══════╝╚═════╝░╚═╝░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝
Version 0.0.0.0.0.0.1b
KEYBINDS
CTRL+S - Save
CTRL+D - Toggle dark mode
CTRL+H - Bring up this help menu
CTRL+P - Settings/preferences
"""
helpText.insert(tk.END,text)
helpText.config(state=tk.DISABLED, background=cfg['bgColor'], foreground=cfg['fgColor'])
helpText.pack(expand=True,fill=tk.BOTH)
# ---------------------------------------------------------------------------- #
# DISCO #
# ---------------------------------------------------------------------------- #
def disco():
if cfg['disco'] == 'true':
bg = random.choice(AVAILABLE_COLORS)
fg = random.choice([i for i in AVAILABLE_COLORS if i != bg])
textarea.config(background=bg, foreground=fg, insertbackground=fg)
window.after(500, disco)
# Images
img_down_code = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x0e\x00\x00\x00\x07\x08\x06\x00\x00\x008G|\x19\x00\x00\x00\tpHYs\x00\x00\x10\x9b\x00\x00\x10\x9b\x01t\x89\x9cK\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x00OIDAT\x18\x95\x95\xce\xb1\x0e@P\x0cF\xe1\xefzI\x8fc$\x12\x111\x19\xec\x9e\x12\xcb\x95 A\x9d\xa4K\xff\x9e\xb6\t\x13J\xffX \xa1\xc7\x16\xac\x19\xc5\xb1!*\x8fy\xf6BB\xf7"\r_\xff77a\xcd\xbd\x10\xedI\xaa\xa3\xd2\xf9r\xf5\x14\xee^N&\x14\xab\xef\xa9\'\x00\x00\x00\x00IEND\xaeB`\x82'
img_down = tk.PhotoImage(master=window, data=img_down_code)
# Frames/Containers
top = tk.Frame(window)
top.pack( side = tk.TOP )
middle = tk.Frame(window)
middle.pack( expand=True, fill=tk.BOTH )
bottom = tk.Frame(window)
bottom.pack( side = tk.BOTTOM )
# Widgets
fileName = tk.Label(text = "No file opened.")# , background = cfg['bgColor'], foreground = cfg['fgColor'])
status = tk.Label(text = "[IDLE]" )# , background = cfg['bgColor'], foreground = cfg['fgColor'])
settingsOverview = tk.Label(text = f"[{cfg['syntax']}] [{cfg['font']}] [{cfg['fontSize']}px] [{cfg['fontDeco']}]", background="Black", foreground="Grey")
textarea = tk.Text(background=cfg['bgColor'], foreground=cfg['fgColor'], insertbackground=cfg['cuColor'], font=(cfg['font'], cfg['fontSize'], cfg['fontDeco']))
# Buttons
saveBtn = tk.Button(text = "💾 Save" , command = lambda: save_file(fileName.cget("text")))
saveAsBtn = tk.Button(text = "💾 Save as..." , command = save_file)
openBtn = tk.Button(text = "📂 Open file...", command = open_file)
toggleDarkBtn = tk.Button(text = "🌞 Light mode", command=toggleDark)
settingsBtn = tk.Button(text = "⚙️ Settings", command=settings)
helpBtn = tk.Button(text = "❔ Help", command=help)
# Grids
settingsOverview.pack(in_=bottom, side=tk.LEFT, padx=10)
saveBtn.pack(in_=bottom, side=tk.LEFT, padx=10)
saveAsBtn.pack(in_=bottom, side=tk.LEFT, padx=10)
settingsBtn.pack(in_=bottom, side=tk.LEFT, padx=10)
helpBtn.pack(in_=bottom, side=tk.LEFT, padx=10)
status.pack(in_=bottom, side=tk.LEFT, padx=10)
fileName.pack(in_=top, padx=10)
toggleDarkBtn.pack(in_=top, side=tk.LEFT, padx=10)
openBtn.pack(in_=top, side=tk.LEFT, padx=10)
textarea.pack(in_=middle, expand=True, fill=tk.BOTH)
window.after(1000, disco)
window.bind('<Control-s>', lambda event: save_file(fileName.cget("text")))
window.bind('<Control-d>', lambda event: toggleDark())
window.bind('<Control-h>', lambda event: help())
window.bind('<Control-p>', lambda event: settings())
window.bind('<Key>', lambda event: highlight(textarea))
window.mainloop()