-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_ui.py
More file actions
256 lines (215 loc) · 11.2 KB
/
search_ui.py
File metadata and controls
256 lines (215 loc) · 11.2 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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_model2.ui'
#
# Created by: PyQt5 UI code generator 5.15.11
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import re
from custom_widgets import SyntaxHighlighter
from config import get_widget_style
class SearchThread(QtCore.QThread):
found_signal = QtCore.pyqtSignal(str, int)
def __init__(self, paths, exeption_paths, pattern, file_extension=None, limits=None):
super().__init__()
self.paths = paths
self.exeption_paths = exeption_paths
self.pattern = pattern
self.file_extension = file_extension
if limits:
self.limits_max_results_count = limits["max_results"]
self.limits_max_file_size = limits["max_file_size"]
else:
self.limits_max_results_count = -1
self.limits_max_file_size = -1
self.is_running = True
def run(self):
results_count = 0
if len(self.paths) != 0:
try:
regex = re.compile(self.pattern)
except Exception as e:
print("Failed to compile regex. Error: ", e)
return
for path in self.paths:
if not self.is_running or (self.limits_max_results_count != -1 and results_count >= self.limits_max_results_count):
break
for root, _, files in os.walk(path):
if not self.is_running or (self.limits_max_results_count != -1 and results_count >= self.limits_max_results_count):
break
# Check for exeption paths
normalized_root = os.path.normpath(root)
if any(normalized_root.startswith(os.path.normpath(exclusion)) for exclusion in self.exeption_paths):
continue
for file in files:
if not self.is_running or (self.limits_max_results_count != -1 and results_count >= self.limits_max_results_count):
break
if not self.file_extension or any(file.endswith(extension) for extension in self.file_extension):
file_path = os.path.join(root, file)
if self.limits_max_file_size != -1 and (os.path.getsize(file_path) / (1024 * 1024)) > self.limits_max_file_size:
continue
try:
with open(file_path, "r", encoding="UTF-8") as f:
content = f.read()
for match in regex.finditer(content):
if not self.is_running or (self.limits_max_results_count != -1 and results_count >= self.limits_max_results_count):
break
self.found_signal.emit(file_path, match.start())
results_count += 1
except Exception as e:
print("Failed to read file. Error: ", e)
class Ui_SearchWindow(object):
def setupUi(self, MainWindow, title: str, icon : QtGui.QIcon, size : QtCore.QRect):
self.MainWindow = MainWindow
MainWindow.setObjectName("SearchWindow")
MainWindow.setWindowTitle(title)
MainWindow.resize(size.width(), size.height())
MainWindow.move(size.x(), size.y())
MainWindow.setWindowIcon(QtGui.QIcon(icon))
MainWindow.setStyleSheet(f"background-color: {get_widget_style("SearchWindow", "background-color")};\n"
"color: white;\n"
"selection-background-color: rgb(255, 255, 127);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollAreaWidgetContents = QtWidgets.QWidget(self.centralwidget)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea.setGeometry(0, 0, MainWindow.width(), MainWindow.height())
self.scrollArea.setStyleSheet(f"""
QScrollBar:vertical {{
border: none;
background: {get_widget_style("SearchWindow", "background-color")};
width: 14px;
margin: 15px 0 15px 0;
}}
QScrollBar::handle:vertical {{
background: {get_widget_style("SearchWindow", "scroll-color")};
min-height: 20px;
}}
QScrollBar::add-line:vertical {{
background: none;
height: 15px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}}
QScrollBar::sub-line:vertical {{
background: none;
height: 15px;
subcontrol-position: top;
subcontrol-origin: margin;
}}
QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical {{
background: none;
}}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{
background: none;
}}
""")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setAlignment(QtCore.Qt.AlignmentFlag.AlignTop | QtCore.Qt.AlignmentFlag.AlignHCenter)
self.searched_files = 0
self.label = QtWidgets.QLabel(f"Search result: {self.searched_files} matches", self.scrollAreaWidgetContents)
self.label.setStyleSheet(f"""
background-color: {get_widget_style("SearchWindow_Header", "background-color")};
border: 1px solid {get_widget_style("SearchWindow_Header", "border-color")};
color: {get_widget_style("SearchWindow_Header", "color")};
padding: 100%;
padding-top: 5px;
padding-bottom: 5px;
margin-bottom: 20px;
""")
self.label.setFont(QtGui.QFont("Impact", 23))
self.label.adjustSize()
self.label.setFixedHeight(80)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.verticalLayout.addWidget(self.label)
self.code_editors = []
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.hightlight = []
def addCodeEditor(self, content, focus_on, path):
self.searched_files += 1
self.label.setText(f"Search result: {self.searched_files} matches")
label = QtWidgets.QLabel(path, self.scrollAreaWidgetContents)
label.setStyleSheet(f"""
background-color: {get_widget_style("SearchWindow_CodeViewer", "file-background-color")};
color: {get_widget_style("SearchWindow_CodeViewer", "file-color")};
margin-top: 5px;
""")
label.setFont(QtGui.QFont("sans-serif", 14))
label.adjustSize()
label.setFixedHeight(30)
label.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)
self.verticalLayout.addWidget(label)
widget = QtWidgets.QPlainTextEdit(content, self.scrollAreaWidgetContents)
self.code_editors.append(widget)
widget.setFixedHeight(700)
widget.setStyleSheet(f"""
font-family: Consolas, 'Courier New', monospace;
font-size: 16px;
background-color: {get_widget_style("SearchWindow_CodeViewer", "code-background-color")};
color: {get_widget_style("SearchWindow_CodeViewer", "default-color")};
selection-color: {get_widget_style("SearchWindow_CodeViewer", "selection-color")};
selection-background-color: {get_widget_style("SearchWindow_CodeViewer", "selection-background-color")};
border: 1px solid {get_widget_style("SearchWindow_CodeViewer", "border-color")};
margin-bottom: 20px;
""")
cursor = widget.textCursor()
cursor.setPosition(focus_on)
widget.setTextCursor(cursor)
widget.setFocus()
extra_selection = []
try:
red, green, blue = get_widget_style("SearchWindow_CodeViewer", "extra-selection-background-color").replace("rgb(", "").replace(")", "").split(", ")
except Exception:
red, green, blue = 0, 0, 0
line_color = QtGui.QColor(int(red), int(green), int(blue))
selection = QtWidgets.QTextEdit.ExtraSelection()
selection.format.setBackground(line_color)
selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True)
selection.cursor = cursor
selection.cursor.clearSelection()
extra_selection.append(selection)
widget.setExtraSelections(extra_selection)
self.add_syntax_highlight(widget, os.path.splitext(path)[1])
self.verticalLayout.addWidget(widget)
def add_syntax_highlight(self, widget : QtWidgets.QPlainTextEdit, extension):
if extension == ".py":
syntax = SyntaxHighlighter.SyntaxType.PythonSyntax
elif extension == ".cpp" or extension == ".hpp":
syntax = SyntaxHighlighter.SyntaxType.CPPSyntax
elif extension == ".c" or extension == ".h":
syntax = SyntaxHighlighter.SyntaxType.CSyntax
elif extension == ".js":
syntax = SyntaxHighlighter.SyntaxType.JSSyntax
elif extension == ".html":
syntax = SyntaxHighlighter.SyntaxType.HTMLSyntax
elif extension == ".css":
syntax = SyntaxHighlighter.SyntaxType.CSSSyntax
elif extension == ".lua":
syntax = SyntaxHighlighter.SyntaxType.LuaSyntax
else:
syntax = None
if syntax:
self.hightlight.append(SyntaxHighlighter(widget.document(), syntax))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("SearchWindow", "SearchWindow"))
def update(self):
self.scrollArea.setGeometry(0, 0, self.MainWindow.width(), self.MainWindow.height())
class SearchWindow(QtWidgets.QMainWindow):
def __init__(self, parent, title, icon, size):
super().__init__(parent)
self.ui = Ui_SearchWindow()
self.ui.setupUi(self, title, icon, size)
def resizeEvent(self, event):
self.ui.update()
super().resizeEvent(event)