forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_editor.py
133 lines (109 loc) · 6.73 KB
/
file_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
127
128
129
130
131
132
133
#!/usr/bin/env python
import sys
import wx
import wx.stc as stc
from runtime import *
# ============================================================================
# Class FileEditor
# ============================================================================
class FileEditor(wx.Dialog):
def __init__(self, parent, file_path):
# super().__init__(parent=None, title="File Editor", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
super().__init__(parent=parent, title="File Editor", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
self.file_path = file_path
self.create_widgets()
self.load_file()
def create_widgets(self):
self.text_ctrl = stc.StyledTextCtrl(self, style=wx.HSCROLL)
if sys.platform == "win32":
self.text_ctrl.SetLexer(stc.STC_LEX_BATCH)
self.text_ctrl.StyleSetSpec(stc.STC_BAT_DEFAULT, "fore:#000000")
self.text_ctrl.StyleSetSpec(stc.STC_BAT_COMMENT, "fore:#008000")
self.text_ctrl.StyleSetSpec(stc.STC_BAT_WORD, "fore:#000000,bold,back:#FFFFFF")
self.text_ctrl.SetKeyWords(0, " ".join(["if else goto echo set", "cd dir rd md del", "call start exit rem"]))
self.text_ctrl.StyleSetForeground(stc.STC_BAT_COMMAND, wx.Colour(0, 128, 192)) # command color
self.text_ctrl.StyleSetForeground(stc.STC_BAT_LABEL, wx.Colour(0, 128, 192)) # label color
self.text_ctrl.StyleSetForeground(stc.STC_BAT_COMMENT, wx.Colour(0, 128, 0)) # comment color
self.text_ctrl.StyleSetForeground(stc.STC_BAT_WORD, wx.Colour(0, 0, 255)) # keyword color
self.text_ctrl.StyleSetForeground(stc.STC_BAT_HIDE, wx.Colour(128, 128, 128)) # color for hidden text
self.text_ctrl.StyleSetForeground(stc.STC_BAT_IDENTIFIER, wx.Colour(255, 128, 0)) # variable text color
self.text_ctrl.StyleSetForeground(stc.STC_BAT_OPERATOR , wx.Colour(255, 0, 255)) # operator text color
else:
self.text_ctrl.SetLexer(stc.STC_LEX_BASH)
self.text_ctrl.StyleSetSpec(stc.STC_SH_DEFAULT, "fore:#000000")
self.text_ctrl.StyleSetSpec(stc.STC_SH_COMMENTLINE , "fore:#008000")
self.text_ctrl.StyleSetSpec(stc.STC_SH_WORD, "fore:#000000,bold,back:#FFFFFF")
self.text_ctrl.SetKeyWords(0, " ".join(["if else elif fi echo set", "cd dir rd md rm", "exit"]))
self.text_ctrl.StyleSetForeground(stc.STC_SH_OPERATOR , wx.Colour(0, 128, 192)) # operator color
self.text_ctrl.StyleSetForeground(stc.STC_SH_STRING , wx.Colour(205, 146, 93)) # label color
self.text_ctrl.StyleSetForeground(stc.STC_SH_COMMENTLINE, wx.Colour(0, 128, 0)) # comment color
self.text_ctrl.StyleSetForeground(stc.STC_SH_WORD, wx.Colour(0, 0, 255)) # keyword color
self.text_ctrl.StyleSetForeground(stc.STC_SH_IDENTIFIER, wx.Colour(255, 128, 0)) # variable text color
self.text_ctrl.SetCaretForeground(wx.BLACK)
self.text_ctrl.SetMarginType(1, stc.STC_MARGIN_NUMBER)
self.text_ctrl.SetMarginWidth(1, 30)
font = wx.Font(9, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
self.text_ctrl.StyleSetFont(wx.stc.STC_STYLE_DEFAULT, font)
self.open_folder = wx.Button(self, label="Open Folder")
self.open_shell = wx.Button(self, label="Open Shell")
self.save_button = wx.Button(self, label="Save and Continue")
self.cancel_button = wx.Button(self, label="Cancel and Abort")
if sys.platform in ["win32", "darwin"]:
self.open_folder.SetToolTip(u"Open Folder in working directory")
self.open_shell.SetToolTip(u"Open command shell in working directory")
else:
self.open_folder.SetToolTip(u"Open Folder in working directory\nNote: PF_FILEMANAGER needs to be set.")
self.open_shell.SetToolTip(u"Open Terminal shell in working directory")
self.save_button.SetToolTip(u"Save the file and continue to flashing.")
self.cancel_button.SetToolTip(u"Cancel and Abort.")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.text_ctrl, proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
sizer.Add(wx.StaticLine(self), proportion=0, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=10)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add((0, 0), 1, wx.EXPAND, 5)
button_sizer.Add(self.open_folder, proportion=0, flag=wx.ALL, border=5)
button_sizer.Add(self.open_shell, proportion=0, flag=wx.ALL, border=5)
button_sizer.Add(self.save_button, proportion=0, flag=wx.ALL, border=5)
button_sizer.Add(self.cancel_button, proportion=0, flag=wx.ALL, border=5)
button_sizer.Add((0, 0), 1, wx.EXPAND, 5)
sizer.Add(button_sizer, proportion=0, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=10)
self.SetSizer(sizer)
self.SetSize((1600, 900)) # set initial size of the editor window
self.SetMinSize((400, 300)) # set minimum size of the editor window
self.open_folder.Bind(wx.EVT_BUTTON, self.on_open_folder)
self.open_shell.Bind(wx.EVT_BUTTON, self.on_open_shell)
self.save_button.Bind(wx.EVT_BUTTON, self.on_save)
self.cancel_button.Bind(wx.EVT_BUTTON, self.on_cancel)
# self.Bind(wx.EVT_SIZE, self._on_resize)
# fix horizontal scroll bar
self.text_ctrl.SetWrapMode(wx.stc.STC_WRAP_NONE)
self.text_ctrl.SetUseHorizontalScrollBar(True)
# disable vertical scrolling on mouse wheel
self.text_ctrl.SetUseVerticalScrollBar(True)
self.text_ctrl.SetScrollWidthTracking(True)
self.text_ctrl.SetScrollWidth(1)
# center the dialog
self.CenterOnParent()
# set tab width
self.text_ctrl.SetTabWidth(4)
# set indentation
self.text_ctrl.SetIndent(4)
def load_file(self):
with open(self.file_path, 'r', encoding='ISO-8859-1', errors="replace") as f:
contents = f.read()
self.text_ctrl.SetValue(contents)
def on_open_folder(self, event):
open_folder(self.Parent, self.file_path, True)
def on_open_shell(self, event):
open_terminal(self.Parent, self.file_path, True)
def on_save(self, event):
with open(self.file_path, 'w', encoding='ISO-8859-1', errors="replace", newline='\n') as f:
f.write(self.text_ctrl.GetValue())
self.EndModal(wx.ID_OK)
def on_cancel(self, event):
self.EndModal(wx.ID_CANCEL)
def _on_resize(self, event):
width = self.Rect.Width
height = self.Rect.Height
print(f"width: {width}\nheight: {height}")
event.Skip(True)