2022-10-04 03:14:18 +00:00
|
|
|
# Python imports
|
2022-10-05 03:58:27 +00:00
|
|
|
import base64, re
|
2022-10-04 03:14:18 +00:00
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
class GrepPreviewWidget(Gtk.Box):
|
2022-10-05 03:58:27 +00:00
|
|
|
def __init__(self, _path, sub_keys, _data, _grep_query):
|
2022-10-04 03:14:18 +00:00
|
|
|
super(GrepPreviewWidget, self).__init__()
|
2022-10-05 03:58:27 +00:00
|
|
|
|
2022-10-04 03:14:18 +00:00
|
|
|
self.set_orientation(Gtk.Orientation.VERTICAL)
|
2022-10-05 03:58:27 +00:00
|
|
|
line_color = "#e0cc64"
|
|
|
|
highlight_color = "#FBF719"
|
|
|
|
grep_query = _grep_query
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-05 03:58:27 +00:00
|
|
|
path = self.decode_str(_path)
|
|
|
|
lbl = '/'.join( path.split("/")[-3:] )
|
|
|
|
title = Gtk.LinkButton.new_with_label(uri=f"file://{path}", label=lbl)
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-04 07:30:46 +00:00
|
|
|
text_view = Gtk.TextView()
|
2022-10-05 03:58:27 +00:00
|
|
|
buffer = text_view.get_buffer()
|
2022-10-04 07:30:46 +00:00
|
|
|
text_view.set_editable(False)
|
2022-10-05 03:58:27 +00:00
|
|
|
text_view.set_monospace(True)
|
2022-10-04 07:30:46 +00:00
|
|
|
text_view.set_wrap_mode(Gtk.WrapMode.NONE)
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-04 07:30:46 +00:00
|
|
|
for i, key in enumerate(sub_keys):
|
2022-10-05 03:58:27 +00:00
|
|
|
line_num = self.make_utf8_line_num(line_color, key)
|
2022-10-04 07:30:46 +00:00
|
|
|
itr = buffer.get_end_iter()
|
|
|
|
buffer.insert_markup(itr, line_num, len(line_num))
|
2022-10-05 03:58:27 +00:00
|
|
|
|
|
|
|
decoded = f"\t{self.decode_str(_data[key])}"
|
|
|
|
self.make_utf8_line_highlight(buffer, itr, i, highlight_color, decoded, grep_query)
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-04 07:30:46 +00:00
|
|
|
self.add(title)
|
|
|
|
self.add(text_view)
|
|
|
|
self.show_all()
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-04 07:30:46 +00:00
|
|
|
def decode_str(self, target):
|
|
|
|
return base64.urlsafe_b64decode(target.encode('utf-8')).decode('utf-8')
|
2022-10-04 03:14:18 +00:00
|
|
|
|
2022-10-04 07:30:46 +00:00
|
|
|
def make_utf8_line_num(self, color, target):
|
|
|
|
return bytes(f"\n<span foreground='{color}'>{target}</span>", "utf-8").decode("utf-8")
|
2022-10-05 03:58:27 +00:00
|
|
|
|
|
|
|
def make_utf8_line_highlight(self, buffer, itr, i, color, target, query):
|
|
|
|
parts = re.split(r"(" + query + ")(?i)", target.replace("\n", ""))
|
|
|
|
for part in parts:
|
|
|
|
itr = buffer.get_end_iter()
|
|
|
|
|
|
|
|
if not query.lower() == part.lower() and not query.lower() in part.lower():
|
|
|
|
buffer.insert(itr, part, length=len(part))
|
|
|
|
else:
|
|
|
|
new_s = f"<span foreground='#000000' background='{color}'>{part}</span>"
|
|
|
|
_part = bytes(new_s, "utf-8").decode("utf-8")
|
|
|
|
buffer.insert_markup(itr, _part, len(_part))
|