mirror of
https://github.com/LorenEteval/Furious.git
synced 2026-10-06 05:47:59 +03:00
281 lines
10 KiB
Python
281 lines
10 KiB
Python
# Copyright (C) 2024–present Loren Eteval & contributors <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/>.
|
||
|
||
"""Provide Qt support for text editor theme."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from Furious.Core.Tun2socks import Tun2socks
|
||
from Furious.Frozenlib.Mixins import Mixins
|
||
from Furious.Plugins import getPluginRegistry
|
||
from Furious.Qt.AppStyleSheet import AppStyleSheet
|
||
|
||
from PySide6 import QtCore
|
||
from PySide6.QtGui import *
|
||
from PySide6.QtWidgets import QApplication
|
||
|
||
__all__ = [
|
||
'DraculaEditorTheme',
|
||
'DraculaJSONSyntaxHighlighter',
|
||
'DraculaLoggerSyntaxHighlighter',
|
||
]
|
||
|
||
|
||
def _currentTheme():
|
||
"""Return the application's active theme without coupling to its concrete type."""
|
||
app = QApplication.instance()
|
||
|
||
if app is not None:
|
||
themeGetter = getattr(app, 'theme', None)
|
||
|
||
if callable(themeGetter):
|
||
try:
|
||
return AppStyleSheet.normalizeTheme(themeGetter())
|
||
except Exception:
|
||
# A partially initialized application falls back to the safe default.
|
||
pass
|
||
|
||
return AppStyleSheet.Light
|
||
|
||
|
||
class EditorHighlightRules:
|
||
"""Represent editor highlight rules."""
|
||
|
||
def __init__(self, regex, color, isBold=False, isItalic=False, isJSONKey=False):
|
||
"""Initialize the EditorHighlightRules."""
|
||
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)
|
||
|
||
if isItalic:
|
||
self.rules.setFontItalic(True)
|
||
|
||
self.isJSONKey = isJSONKey
|
||
|
||
|
||
class EditorTheme:
|
||
"""Represent editor theme."""
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
"""Initialize the EditorTheme."""
|
||
super().__init__(*args, **kwargs)
|
||
|
||
@staticmethod
|
||
def getStyleSheet(*args, **kwargs):
|
||
"""Return style sheet."""
|
||
raise NotImplementedError
|
||
|
||
|
||
class DraculaEditorTheme(EditorTheme):
|
||
"""Compatibility facade for the application-owned editor theme."""
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
"""Initialize the DraculaEditorTheme."""
|
||
super().__init__(*args, **kwargs)
|
||
|
||
@staticmethod
|
||
def getStyleSheet(widgetName, fontFamily, theme=None):
|
||
"""Return token-driven editor styling while preserving the legacy API."""
|
||
if theme is None:
|
||
theme = _currentTheme()
|
||
|
||
return AppStyleSheet.editorStyleSheet(
|
||
widgetName=widgetName,
|
||
fontFamily=fontFamily,
|
||
theme=theme,
|
||
)
|
||
|
||
|
||
class AppQSyntaxHighlighter(Mixins.ThemeAware, QSyntaxHighlighter):
|
||
"""Apply syntax highlighting for app q syntax text."""
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
"""Initialize the AppQSyntaxHighlighter."""
|
||
self._theme = AppStyleSheet.normalizeTheme(kwargs.pop('theme', _currentTheme()))
|
||
|
||
super().__init__(*args, **kwargs)
|
||
|
||
self.highlightRules = list()
|
||
|
||
def buildHighlightRules(self, palette):
|
||
"""Return highlight rules for *palette*."""
|
||
return list()
|
||
|
||
def applyTheme(self, theme):
|
||
"""Rebuild formats from semantic tokens and rehighlight the document."""
|
||
self._theme = AppStyleSheet.normalizeTheme(theme)
|
||
palette = AppStyleSheet.paletteForTheme(self._theme)
|
||
self.highlightRules = self.buildHighlightRules(palette)
|
||
self.rehighlight()
|
||
|
||
def themeChangedCallback(self, theme: str):
|
||
"""Refresh syntax formats after an application theme change."""
|
||
self.applyTheme(theme)
|
||
|
||
def highlightBlock(self, text):
|
||
"""Handle highlight block for the app q syntax highlighter."""
|
||
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()
|
||
|
||
capturedStart = match.capturedStart()
|
||
captured = text[capturedStart : capturedStart + capturedLength]
|
||
shouldHighlight = True
|
||
|
||
for version in [
|
||
*getPluginRegistry().coreVersions(),
|
||
Tun2socks.version(),
|
||
]:
|
||
if captured == version:
|
||
# These x.y.z.u version values are not IPv4 addresses. Do not highlight
|
||
shouldHighlight = False
|
||
|
||
break
|
||
|
||
if shouldHighlight:
|
||
self.setFormat(capturedStart, capturedLength, highlightRule.rules)
|
||
|
||
|
||
class DraculaJSONSyntaxHighlighter(AppQSyntaxHighlighter):
|
||
"""Apply application-themed JSON syntax highlighting."""
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
"""Initialize the DraculaJSONSyntaxHighlighter."""
|
||
super().__init__(*args, **kwargs)
|
||
|
||
self.applyTheme(self._theme)
|
||
|
||
def buildHighlightRules(self, palette):
|
||
"""Return JSON rules resolved from editor semantic tokens."""
|
||
return [
|
||
EditorHighlightRules(
|
||
r'\b(true|false|null)\b', palette['editor_keyword'], isBold=True
|
||
),
|
||
EditorHighlightRules(
|
||
r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?',
|
||
palette['editor_number'],
|
||
),
|
||
EditorHighlightRules(r'[:,\[\]\{\}]', palette['editor_symbol']),
|
||
EditorHighlightRules(r'"[^"\\]*(\\.[^"\\]*)*"', palette['editor_string']),
|
||
EditorHighlightRules(
|
||
r'"([^"\\]*(\\.[^"\\]*)*)"\s*:',
|
||
palette['editor_key'],
|
||
isJSONKey=True,
|
||
),
|
||
EditorHighlightRules(r'^#.*', palette['editor_comment'], isItalic=True),
|
||
]
|
||
|
||
|
||
class DraculaLoggerSyntaxHighlighter(AppQSyntaxHighlighter):
|
||
# https://ihateregex.io/expr/ip/
|
||
"""Apply application-themed network logger syntax highlighting."""
|
||
IPV4_REGEX = (
|
||
r'(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)'
|
||
r'(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}(?::\d{1,5})?\b'
|
||
)
|
||
# https://vernon.mauery.com/content/2008/04/21/ipv6-regex/
|
||
IPV6_REGEX = (
|
||
r'(A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}Z)|'
|
||
r'(A(([0-9a-f]{1,4}:){1,7}|:):Z)|'
|
||
r'(A:(:[0-9a-f]{1,4}){1,7}Z)|'
|
||
r'(A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3})Z)|'
|
||
r'(A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3})Z)|'
|
||
r'(A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)|'
|
||
r'(A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}Z)(?::\d{1,5})?'
|
||
)
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
"""Initialize the DraculaLoggerSyntaxHighlighter."""
|
||
super().__init__(*args, **kwargs)
|
||
|
||
self.applyTheme(self._theme)
|
||
|
||
def buildHighlightRules(self, palette):
|
||
"""Return network-log rules resolved from editor semantic tokens."""
|
||
return [
|
||
EditorHighlightRules(
|
||
DraculaLoggerSyntaxHighlighter.IPV4_REGEX
|
||
+ r'|'
|
||
+ DraculaLoggerSyntaxHighlighter.IPV6_REGEX,
|
||
palette['editor_ip'],
|
||
),
|
||
EditorHighlightRules(
|
||
r'(https?:)?\/\/[^\s,"\'>)\]]+', palette['editor_url']
|
||
),
|
||
EditorHighlightRules(
|
||
# Application logging timestamp
|
||
r'\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\]' + r'|'
|
||
# Plugin-core logging timestamps
|
||
+ r'|'.join(getPluginRegistry().logTimestampPatterns()) + r'|'
|
||
# tun2socks logging timestamp
|
||
+ r'\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}',
|
||
palette['editor_timestamp'],
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[[A-Za-z0-9_]+\.[A-Za-z0-9_]+\.[A-Za-z0-9_]+\]',
|
||
palette['editor_logger'],
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[INFO\]|\[Info\]|INFO', palette['editor_info'], isBold=True
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[DEBUG\]|\[Debug\]|DEBUG',
|
||
palette['editor_debug'],
|
||
isBold=True,
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[WARNING\]|\[Warning\]|WARNING',
|
||
palette['editor_warning'],
|
||
isBold=True,
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[ERROR\]|\[Error\]|ERROR',
|
||
palette['editor_error'],
|
||
isBold=True,
|
||
),
|
||
EditorHighlightRules(
|
||
r'\[CRITICAL\]|\[Critical\]|CRITICAL',
|
||
palette['editor_critical'],
|
||
isBold=True,
|
||
),
|
||
EditorHighlightRules(r"'[^']*'", palette['editor_string']),
|
||
EditorHighlightRules(r'"[^"]*"', palette['editor_string']),
|
||
]
|