Skip to content

Commit

Permalink
✨ autosuggest message tags in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Jul 11, 2024
1 parent 7f5ce17 commit 340373c
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions userland/scripts/messages/filter_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
# 3rd party
from textual.containers import Horizontal, Vertical
from textual.screen import ModalScreen
from textual.widgets import Button, Input, Label
from textual.widgets import Button, Input, Label, OptionList
from textual.widgets.option_list import Option

# local
from userland.models.message.tag import MessageTag


class FilterModal(ModalScreen[list[str]]):
Expand All @@ -28,20 +32,25 @@ class FilterModal(ModalScreen[list[str]]):
width: 54;
}
#autocomplete_wrapper {
height: 5;
}
#filter {
margin-left: 0;
margin-top: 1;
}
#wrapper {
background: $primary-background;
height: 9;
height: 15;
padding: 1;
width: 60;
}
"""

_tags: list[str]
_alltags: list[str] = []

def __init__(self, *args, tags: list[str] | None = None, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -53,6 +62,7 @@ def compose(self):
Label("Tags"),
Input(" ".join(self._tags)),
),
Horizontal(OptionList(disabled=True), id="autocomplete_wrapper"),
Horizontal(
Button("Filter", variant="success", id="filter", name="filter"),
Button("Cancel", variant="error", id="cancel", name="cancel"),
Expand All @@ -65,15 +75,44 @@ def _submit(self) -> None:
assert tags
self.dismiss(tags.value.split(" "))

async def on_mount(self) -> None:
self._alltags = [t.name for t in await MessageTag.query.gino.all()]

def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.name == "cancel":
self.app.pop_screen() # pop this modal
return

self._submit()

async def on_input_changed(self, event: Input.Changed) -> None:
last_word = event.input.value.split(" ")[-1]
selections = self.query_one(OptionList)
selections.clear_options()

if last_word == "":
selections.disabled = True
return

suggestions = [
Option(t) for t in self._alltags if t.startswith(last_word)
]
selections.disabled = False
selections.add_options(suggestions)

async def on_input_submitted(self, event: Input.Submitted) -> None:
self._submit()

async def on_option_list_option_selected(
self, event: OptionList.OptionSelected
) -> None:
tags_input = self.query_one(Input)
tags = tags_input.value.split(" ")[:-1]
tags.append(str(event.option.prompt))
tags_input.value = "".join([" ".join(tags), " "])
tags_input.focus()
event.option_list.clear_options()
event.option_list.disabled = True

async def key_escape(self, _):
self.app.pop_screen() # pop this modal

0 comments on commit 340373c

Please sign in to comment.