Skip to content

Commit

Permalink
Highlight alignment column
Browse files Browse the repository at this point in the history
  • Loading branch information
tangledhelix committed Nov 13, 2024
1 parent cb57f7c commit 48bc672
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/guiguts/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
highlight_double_quotes,
remove_highlights,
highlight_quotbrac_callback,
highlight_aligncol_callback,
)
from guiguts.maintext import maintext
from guiguts.mainwindow import (
Expand Down Expand Up @@ -591,6 +592,12 @@ def init_search_menu(self) -> None:
"Highlight S~urrounding Quotes & Brackets",
root().highlight_quotbrac,
)
menu_search.add_checkbox(
"Highlight Al~ignment Column",
root().highlight_aligncol,
lambda: highlight_aligncol_callback(True),
lambda: highlight_aligncol_callback(False),
)
menu_search.add_button(
"~Remove Highlights",
remove_highlights,
Expand Down
47 changes: 47 additions & 0 deletions src/guiguts/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from guiguts.maintext import maintext
from guiguts.preferences import preferences, PrefKey
from guiguts.root import root
from guiguts.utilities import IndexRange


Expand All @@ -19,6 +20,7 @@ class HighlightTag(StrEnum):
CURLY_DOUBLE_QUOTE = auto()
STRAIGHT_SINGLE_QUOTE = auto()
CURLY_SINGLE_QUOTE = auto()
ALIGNCOL = auto()


class HighlightColors:
Expand Down Expand Up @@ -88,6 +90,12 @@ class HighlightColors:
"Default": {"bg": "dodgerblue", "fg": "white"},
}

ALIGNCOL = {
"Light": {"bg": "greenyellow", "fg": "black"},
"Dark": {"bg": "greenyellow", "fg": "black"},
"Default": {"bg": "greenyellow", "fg": "black"},
}


def highlight_selection(
pat: str,
Expand Down Expand Up @@ -433,3 +441,42 @@ def remove_highlights_quotbrac() -> None:
HighlightTag.CURLY_SINGLE_QUOTE,
):
maintext().tag_delete(tag)


def highlight_aligncol_callback(value: bool) -> None:
"""Callback when highlight_aligncol active state is changed."""
if value:
root().highlight_aligncol.set(True)
root().aligncol = maintext().get_insert_index().col
highlight_aligncol()
else:
root().highlight_aligncol.set(False)
root().aligncol = -1
remove_highlights_aligncol()


def highlight_aligncol() -> None:
"""Add a highlight to all characters in the alignment column."""
if root().highlight_aligncol.get() and root().aligncol:
(top_index, bot_index) = get_screen_window_coordinates()

col = root().aligncol
row = int(top_index.split(".")[0])
end_row = int(bot_index.split(".")[0])

maintext().tag_delete(HighlightTag.ALIGNCOL)
_highlight_configure_tag(HighlightTag.ALIGNCOL, HighlightColors.ALIGNCOL)
while row <= end_row:
# find length of row; don't highlight if row is too short to contain col
rowlen = int(maintext().index(f"{row}.0 lineend").split(".")[1])
if 0 < col < rowlen:
maintext().tag_add(HighlightTag.ALIGNCOL, f"{row}.{col}")
row += 1

# TODO: refactor to use on_change like quotbrac does
root().after(500, highlight_aligncol)


def remove_highlights_aligncol() -> None:
"""Remove highlights for alignment column"""
maintext().tag_delete(HighlightTag.ALIGNCOL)
2 changes: 2 additions & 0 deletions src/guiguts/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, **kwargs: Any) -> None:
self.allow_config_saves = False
self.split_text_window = PersistentBoolean(PrefKey.SPLIT_TEXT_WINDOW)
self.highlight_quotbrac = PersistentBoolean(PrefKey.HIGHLIGHT_QUOTBRAC)
self.highlight_aligncol = tk.BooleanVar()
self.aligncol = -1

self.option_add("*tearOff", preferences.get(PrefKey.TEAROFF_MENUS))
self.rowconfigure(0, weight=1)
Expand Down

0 comments on commit 48bc672

Please sign in to comment.