mirror of
https://github.com/LorenEteval/Furious.git
synced 2026-09-26 00:38:06 +03:00
90 lines
3.1 KiB
Python
90 lines
3.1 KiB
Python
# Copyright (C) 2023 Loren Eteval <loren.eteval@proton.me>
|
|
#
|
|
# This file is part of Furious.
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from PySide6 import QtCore
|
|
from PySide6.QtGui import QColor, QFont, QSyntaxHighlighter, QTextCharFormat
|
|
|
|
|
|
class HighlightRules:
|
|
def __init__(self, regex, color, isBold=False, isJSONKey=False):
|
|
self.regex = QtCore.QRegularExpression(regex)
|
|
self.color = QColor(color)
|
|
|
|
self.rules = QTextCharFormat()
|
|
self.rules.setForeground(self.color)
|
|
|
|
if isBold:
|
|
self.rules.setFontWeight(QFont.Weight.Bold)
|
|
|
|
self.isJSONKey = isJSONKey
|
|
|
|
|
|
class Theme(QSyntaxHighlighter):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
@staticmethod
|
|
def getStyleSheet(*args, **kwargs):
|
|
raise NotImplementedError
|
|
|
|
|
|
class DraculaTheme(Theme):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.highlightRules = [
|
|
# Keywords: true, false, null. Pink. Bold
|
|
HighlightRules(r'\b(true|false|null)\b', '#FF79C6', isBold=True),
|
|
# Numbers. Purple
|
|
HighlightRules(r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?', '#BD93F9'),
|
|
# Symbols: :, [, ], {, }. White
|
|
HighlightRules(r'[:,\[\]\{\}]', '#F8F8F2'),
|
|
# Double-quoted strings. Yellow
|
|
HighlightRules(r'"[^"\\]*(\\.[^"\\]*)*"', '#F1FA8C'),
|
|
# JSON keys. Green
|
|
HighlightRules(r'"([^"\\]*(\\.[^"\\]*)*)"\s*:', '#50FA7B', isJSONKey=True),
|
|
# Comments(only for hints on display). Grey
|
|
HighlightRules(r'^#.*', '#6272a4'),
|
|
]
|
|
|
|
def highlightBlock(self, text):
|
|
for highlightRule in self.highlightRules:
|
|
iterator = highlightRule.regex.globalMatch(text)
|
|
|
|
while iterator.hasNext():
|
|
match = iterator.next()
|
|
|
|
if highlightRule.isJSONKey:
|
|
# JSON keys. Ignore trailing :
|
|
capturedLength = match.capturedLength() - 1
|
|
else:
|
|
capturedLength = match.capturedLength()
|
|
|
|
self.setFormat(
|
|
match.capturedStart(), capturedLength, highlightRule.rules
|
|
)
|
|
|
|
@staticmethod
|
|
def getStyleSheet(widgetName, fontFamily):
|
|
return (
|
|
f'{widgetName} {{'
|
|
f' background-color: #282A36;'
|
|
f' color: #F8F8F2;'
|
|
f' font-family: \'{fontFamily}\';'
|
|
f'}}'
|
|
)
|