Much more plugin interop work as well as more images

This commit is contained in:
itdominator 2023-10-19 22:55:00 -05:00
parent f2a0287950
commit c88fec7a27
10 changed files with 184 additions and 53 deletions

View File

@ -11,4 +11,6 @@ WIP
# Images
![1 Newton default view. ](images/pic1.png)
![2 Newton search and replace plus menu shown. ](images/pic2.png)
![2 Newton search and replace plus menu shown. ](images/pic2.png
![3 Newton displaying inline colors. ](images/pic3.png
![4 Newton as transparent with youtube playing below it. ](images/pic3.png)

BIN
images/pic3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

BIN
images/pic4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 KiB

View File

@ -7,8 +7,10 @@ import colorsys
class ColorConverterMixin:
# NOTE: HSV HSL, and Hex Alpha parsing are available in Gtk 4.0- not lower.
# So, for compatability we're gunna convert to rgba string ourselves...
def get_color_text(self, buffer, start, end):
text = buffer.get_text(start, end, include_hidden_chars = False)
text = buffer.get_text(start, end, include_hidden_chars = False)
try:
if "hsl" in text:
@ -16,11 +18,53 @@ class ColorConverterMixin:
if "hsv" in text:
text = self.hsv_to_rgb(text)
if "#" == text[0]:
hex = text[1:]
size = len(hex)
if size in [4, 8, 16]:
rgba = self.hex_to_rgba(hex, size)
print(rgba)
except Exception as e:
...
return text
def hex_to_rgba(self, hex, size):
rgba = []
slots = None
step = 2
bytes = 16
if size == 4: # NOTE: RGBA
step = 1
slots = (0, 1, 2, 3)
if size == 6: # NOTE: RR GG BB
slots = (0, 2, 4)
if size == 8: # NOTE: RR GG BB AA
step = 2
slots = (0, 2, 4, 6)
if size == 16: # NOTE: RRRR GGGG BBBB AAAA
step = 4
slots = (0, 4, 8, 12)
for i in slots:
v = int(hex[i : i + step], bytes)
rgba.append(v)
rgb_sub = ','.join(map(str, tuple(rgba)))
return f"rgba({rgb_sub})"
# return tuple(rgba)
def hsl_to_rgb(self, text):
_h, _s , _l = text.replace("hsl", "") \
.replace("deg", "") \

View File

@ -1,4 +1,5 @@
# Python imports
import random
# Lib imports
import gi
@ -20,6 +21,8 @@ class Plugin(ColorConverterMixin, PluginBase):
self.name = "Colorize" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
# where self.name should not be needed for message comms
self.tag_stub_name = "colorize_tag"
self._buffer = None
def run(self):
...
@ -36,29 +39,83 @@ class Plugin(ColorConverterMixin, PluginBase):
def _set_active_src_view(self, source_view):
self._active_src_view = source_view
def _buffer_changed_first_load(self, buffer):
self._buffer = buffer
self._do_colorize(buffer)
def _buffer_changed(self, buffer):
tag_table = buffer.get_tag_table()
mark = buffer.get_insert()
iter = buffer.get_iter_at_mark(mark)
tags = iter.get_tags()
self._event_system.emit("pause_event_processing")
self._handle_colorize(buffer)
self._event_system.emit("resume_event_processing")
iter.forward_line() # NOTE: Jump to start of next line
end = iter.copy()
iter.backward_line() # NOTE: To now easily get start of prior line
start = iter.copy()
def _handle_colorize(self, buffer):
self._buffer = buffer
tag_table = buffer.get_tag_table()
mark = buffer.get_insert()
start = None
end = buffer.get_iter_at_mark(mark)
for tag in tags:
if tag.props.name and self.tag_stub_name in tag.props.name:
buffer.remove_tag(tag, start, end)
tag_table.remove(tag)
i = 0
walker_iter = end.copy()
working_tag = self.find_working_tag(walker_iter, i)
if working_tag:
start = self.find_start_range(walker_iter, working_tag)
self.find_end_range(end, working_tag)
buffer.remove_tag(working_tag, start, end)
else:
start = self.traverse_backward_25_or_less(walker_iter)
self.traverse_forward_25_or_less(end)
self._do_colorize(buffer, start, end)
def find_working_tag(self, walker_iter, i):
tags = walker_iter.get_tags()
for tag in tags:
if tag.props.name and self.tag_stub_name in tag.props.name:
return tag
res = walker_iter.backward_char()
if not res: return
if i > 25: return
return self.find_working_tag(walker_iter, i + 1)
def find_start_range(self, walker_iter, working_tag):
tags = walker_iter.get_tags()
for tag in tags:
if tag.props.name and working_tag.props.name in tag.props.name:
res = walker_iter.backward_char()
if res:
self.find_start_range(walker_iter, working_tag)
return walker_iter
def find_end_range(self, end, working_tag):
tags = end.get_tags()
for tag in tags:
if tag.props.name and working_tag.props.name in tag.props.name:
res = end.forward_char()
if res:
self.find_end_range(end, working_tag)
def traverse_backward_25_or_less(self, walker_iter):
i = 1
while i <= 25:
res = walker_iter.backward_char()
if not res: break
i += 1
def traverse_forward_25_or_less(self, end):
i = 1
while i <= 25:
res = end.forward_char()
if not res: break
i += 1
def _do_colorize(self, buffer = None, start_itr = None, end_itr = None):
# rgb(a), hsl, hsv
results = self.finalize_non_hex_matches( self.collect_preliminary_results(buffer, start_itr, end_itr) )
@ -135,18 +192,28 @@ class Plugin(ColorConverterMixin, PluginBase):
results = []
for start, end in result_hits:
while not end.get_char() in [";", " "]:
end.forward_char()
i = 0
_ch = end.get_char()
ch = ord(end.get_char()) if _ch else -1
results.append([start, end])
while ((ch >= 48 and ch <= 57) or (ch >= 65 and ch <= 70) or (ch >= 97 and ch <= 102)):
if i > 16: break
i += 1
end.forward_char()
_ch = end.get_char()
ch = ord(end.get_char()) if _ch else -1
if i in [3, 4, 6, 8, 9, 12, 16]:
results.append([start, end])
return results
def process_results(self, buffer, results):
# NOTE: HSV and HSL parsing are available in Gtk 4.0. Not lower...
for start, end in results:
text = self.get_color_text(buffer, start, end)
color = Gdk.RGBA()
if color.parse(text):
tag = self.get_colorized_tag(buffer, text, color)
buffer.apply_tag(tag, start, end)

View File

@ -7,24 +7,23 @@
class AddCommentMixin:
def add_comment_characters(self, document, start_tag, end_tag, start, end, deselect, oldPos):
smark = document.create_mark("start", start, False)
imark = document.create_mark("iter", start, False)
emark = document.create_mark("end", end, False)
def add_comment_characters(self, buffer, start_tag, end_tag, start, end, deselect, oldPos):
smark = buffer.create_mark("start", start, False)
imark = buffer.create_mark("iter", start, False)
emark = buffer.create_mark("end", end, False)
number_lines = end.get_line() - start.get_line() + 1
comment_pos_iter = None
count = 0
document.begin_user_action()
buffer.begin_user_action()
for i in range(0, number_lines):
iter = document.get_iter_at_mark(imark)
iter = buffer.get_iter_at_mark(imark)
if not comment_pos_iter:
(comment_pos_iter, count) = self.discard_white_spaces(iter)
if self.is_commented(comment_pos_iter, start_tag):
new_code = self.remove_comment_characters(document, start_tag, end_tag, start, end)
new_code = self.remove_comment_characters(buffer, start_tag, end_tag, start, end)
return
else:
comment_pos_iter = iter
@ -35,33 +34,33 @@ class AddCommentMixin:
iter.forward_char()
document.insert(comment_pos_iter, start_tag)
document.insert(comment_pos_iter, " ")
buffer.insert(comment_pos_iter, start_tag)
buffer.insert(comment_pos_iter, " ")
if end_tag:
if i != number_lines -1:
iter = document.get_iter_at_mark(imark)
iter = buffer.get_iter_at_mark(imark)
iter.forward_to_line_end()
document.insert(iter, end_tag)
buffer.insert(iter, end_tag)
else:
iter = document.get_iter_at_mark(emark)
document.insert(iter, end_tag)
iter = buffer.get_iter_at_mark(emark)
buffer.insert(iter, end_tag)
iter = document.get_iter_at_mark(imark)
iter = buffer.get_iter_at_mark(imark)
iter.forward_line()
document.delete_mark(imark)
imark = document.create_mark("iter", iter, True)
buffer.delete_mark(imark)
imark = buffer.create_mark("iter", iter, True)
document.end_user_action()
buffer.end_user_action()
document.delete_mark(imark)
new_start = document.get_iter_at_mark(smark)
new_end = document.get_iter_at_mark(emark)
buffer.delete_mark(imark)
new_start = buffer.get_iter_at_mark(smark)
new_end = buffer.get_iter_at_mark(emark)
document.select_range(new_start, new_end)
document.delete_mark(smark)
document.delete_mark(emark)
buffer.select_range(new_start, new_end)
buffer.delete_mark(smark)
buffer.delete_mark(emark)
if deselect:
oldPosIter = document.get_iter_at_offset(oldPos + 2)
document.place_cursor(oldPosIter)
oldPosIter = buffer.get_iter_at_offset(oldPos + 2)
buffer.place_cursor(oldPosIter)

View File

@ -76,8 +76,9 @@ class Plugin(AddCommentMixin, RemoveCommentMixin, CodeCommentTags, PluginBase):
buffer.end_user_action()
return
self._event_system.emit("pause_event_processing")
new_code = self.add_comment_characters(buffer, start_tag, end_tag, start, end, deselect, oldPos)
self._event_system.emit("resume_event_processing")
def discard_white_spaces(self, iter):
count = 0

View File

@ -4,16 +4,29 @@ from collections import defaultdict
# Lib imports
# Application imports
from .singleton import Singleton
class EventSystem:
class EventSystem(Singleton):
""" Create event system. """
def __init__(self):
self.subscribers = defaultdict(list)
self._is_paused = False
self._subscribe_to_events()
def _subscribe_to_events(self):
self.subscribe("pause_event_processing", self._pause_processing_events)
self.subscribe("resume_event_processing", self._resume_processing_events)
def _pause_processing_events(self):
self._is_paused = True
def _resume_processing_events(self):
self._is_paused = False
def subscribe(self, event_type, fn):
self.subscribers[event_type].append(fn)
@ -25,6 +38,9 @@ class EventSystem:
self.subscribers.pop(event_type, None)
def emit(self, event_type, data = None):
if self._is_paused and event_type != "resume_event_processing":
return
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:
@ -36,6 +52,9 @@ class EventSystem:
fn()
def emit_and_await(self, event_type, data = None):
if self._is_paused and event_type != "resume_event_processing":
return
""" NOTE: Should be used when signal has only one listener and vis-a-vis """
if event_type in self.subscribers:
response = None

View File

@ -6,15 +6,14 @@
"tear_down" : "<Control>q",
"toggle_highlight_line" : "<Control>h",
"open_files" : "<Control>o",
"move_lines_up" : "<Control>Up",
"move_lines_down" : "<Control>Down",
"keyboard_move_lines_up" : "<Control>Up",
"keyboard_move_lines_down" : "<Control>Down",
"keyboard_undo" : "<Control>z",
"keyboard_redo" : "<Control>y",
"keyboard_create_tab" : "<Control>t",
"keyboard_close_tab" : "<Control>w",
"keyboard_save_file" : "<Control>s",
"keyboard_insert_mark" : "<Control>m",
"keyboard_tggl_comment" : "<Control>slash",
"keyboard_clear_marks" : "<Shift><Control>m",
"keyboard_save_file_as" : "<Shift><Control>s",
"keyboard_up" : "Up",

View File

@ -117,7 +117,7 @@
"error_color":"#ff0000"
},
"debugging":{
"ch_log_lvl":10,
"ch_log_lvl":20,
"fh_log_lvl":20
}
}
}