diff --git a/file.py b/file.py index 5f2c0e1..0ae12e3 100644 --- a/file.py +++ b/file.py @@ -1,8 +1,9 @@ #!/usr/bin/python3 from PyQt6.QtWidgets import QWidget, QFileDialog, QVBoxLayout -from PyQt6.QtGui import QFont +from PyQt6.QtGui import QFont, QTextCharFormat, QColor from binary_text_edit import BinaryTextEdit +from highlighting import Higlighter class FileActions: @@ -89,6 +90,12 @@ class File: self.content_binary_input.setFont(QFont("Ubuntu Mono", 12)) self.content_binary_input.textChanged.connect(self.on_edit) + self.bit_highlighter = Higlighter() + self.one_format = QTextCharFormat() + self.one_format.setBackground(QColor(200, 150, 100)) + self.bit_highlighter.add_mapping("1", self.one_format) + self.bit_highlighter.setDocument(self.content_binary_input.document()) + self.content_widget.setLayout(self.content_widget_layout) self.app.main_window.openFileTabs.addTab( # add a tab for the file in the top files list diff --git a/highlighting.py b/highlighting.py new file mode 100644 index 0000000..c639f07 --- /dev/null +++ b/highlighting.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +import re +from PyQt6.QtGui import QSyntaxHighlighter + + +class Higlighter(QSyntaxHighlighter): + def __init__(self, parent=None): + QSyntaxHighlighter.__init__(self, parent) + + self.mappings = {} + + def add_mapping(self, pattern, format): + self.mappings[pattern] = format + + def highlightBlock(self, text): + for pattern, format in self.mappings.items(): + for match in re.finditer(pattern, text): + start, end = match.span() + self.setFormat(start, end - start, format)