From dd8f50e141fa617cb002cc5c02e69ee7a4c4ff9f Mon Sep 17 00:00:00 2001 From: Seth Rider Date: Thu, 6 May 2021 15:16:59 +0100 Subject: [PATCH 1/4] Capability to choose target dictionary for add-translation --- plover_console_ui/add_translation.py | 72 +++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/plover_console_ui/add_translation.py b/plover_console_ui/add_translation.py index e3263ff..0527079 100644 --- a/plover_console_ui/add_translation.py +++ b/plover_console_ui/add_translation.py @@ -3,7 +3,7 @@ from prompt_toolkit.buffer import Buffer from prompt_toolkit.key_binding import KeyBindings -from prompt_toolkit.layout.containers import HSplit +from prompt_toolkit.layout.containers import HSplit, FormattedTextControl, Window from prompt_toolkit.widgets import TextArea from prompt_toolkit.application import get_app @@ -35,6 +35,42 @@ def __init__(self, engine, on_output, on_exit): # we start in the strokes field add_filter(engine) + self.dicts = [] + for path in self.engine.dictionaries: + d = self.engine.dictionaries[path] + if not d.readonly: + self.dicts.append(d) + + self.dict_index = 0 + + picker_kb = KeyBindings() + + # FormattedTextControl can't have accept_handler bound + @picker_kb.add("enter") + def _(event): + self.accept(None) + + @picker_kb.add("left") + def _(event): + target = self.dict_index - 1 + if target < 0: + target = len(self.dicts) - 1 + self.dict_index = target + + @picker_kb.add("right") + def _(event): + target = self.dict_index + 1 + if target > len(self.dicts) - 1: + target = 0 + self.dict_index = target + + self.dictionary_picker = Window(FormattedTextControl( + focusable=True, + text=lambda: f"Add to: {self.dicts[self.dict_index].path}", + style="class:normal", + key_bindings=picker_kb + )) + self.strokes_field = TextArea( prompt="Strokes: ", height=1, @@ -73,29 +109,41 @@ def _(event): self.update_output() on_exit() + def focus(direction): + layout = get_app().layout + if direction == 'next': + layout.focus_next() + if direction == 'previous': + layout.focus_previous() + + if layout.has_focus(self.strokes_field): + add_filter(self.engine) + else: + remove_filter(self.engine) + @kb.add("tab") def _(event): - self.cycle_focus() + focus('next') + + @kb.add("s-tab") + def _(event): + focus('previous') self.container = HSplit( - [self.strokes_field, self.translation_field], + [self.dictionary_picker, self.strokes_field, self.translation_field], key_bindings=kb, ) def strokes(self): return tuple(self.strokes_field.text.strip().split()) - def cycle_focus(self): - layout = get_app().layout - if layout.has_focus(self.strokes_field): - remove_filter(self.engine) - else: - add_filter(self.engine) - layout.focus_next() - def accept(self, _): if self.strokes() and self.translation_field.text: - self.engine.add_translation(self.strokes(), self.translation_field.text) + self.engine.add_translation( + self.strokes(), + self.translation_field.text, + self.dicts[self.dict_index].path + ) self.outcome = "Translation added" self.update_output() remove_filter(self.engine) From cf6719d7815f1d41e5e80ef4015319e735e76ee5 Mon Sep 17 00:00:00 2001 From: Seth Rider Date: Sat, 29 May 2021 13:24:02 +0100 Subject: [PATCH 2/4] Tidy up choose dictionary, docs, version bump --- README.rst | 2 ++ plover_console_ui/add_translation.py | 19 ++++++++++++++----- setup.cfg | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 89f3655..73c0889 100644 --- a/README.rst +++ b/README.rst @@ -110,6 +110,8 @@ Commands ======== - ``addtranslation``: enters ``ADD_TRANSLATION`` mode + - Tab/Shift-Tab to move between ``Strokes``, ``Output`` and choose dictionary field + - When in the 'choose dictionary' field you can press left/right arrows to cycle (writable) dictionaries - ``lookup``: enters ``LOOKUP`` mode - ``output``: toggles Plover output on/off - ``reset``: reconnects current ``machine`` (reloads dictionaries) diff --git a/plover_console_ui/add_translation.py b/plover_console_ui/add_translation.py index 0527079..31cfd2b 100644 --- a/plover_console_ui/add_translation.py +++ b/plover_console_ui/add_translation.py @@ -66,10 +66,10 @@ def _(event): self.dictionary_picker = Window(FormattedTextControl( focusable=True, - text=lambda: f"Add to: {self.dicts[self.dict_index].path}", + text=lambda: f"{self.dicts[self.dict_index].path}", style="class:normal", key_bindings=picker_kb - )) + ), height=1) self.strokes_field = TextArea( prompt="Strokes: ", @@ -120,6 +120,7 @@ def focus(direction): add_filter(self.engine) else: remove_filter(self.engine) + self.update_output() @kb.add("tab") def _(event): @@ -182,9 +183,17 @@ def translation_changed(self, buff: Buffer): def update_output(self): output = \ - "Add translation"\ - "\n(Escape to abort, Enter to add entry)"\ - "\n---------------" + " -----------------\n"\ + "| Add translation |\n"\ + " -----------------"\ + "\nEscape to abort, Enter to add entry" + + layout = get_app().layout + if layout.has_focus(self.dictionary_picker): + output += "\nā† or ā†’ to pick dictionary" + + output += "\n -----------------" + if self.strokes_info: output += f"\n{self.strokes_info}" if self.translation_info: diff --git a/setup.cfg b/setup.cfg index 98f84ca..b442ce7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = plover_console_ui -version = 1.1.4 +version = 1.2.0 description = Text User Interface for Plover long_description = file: README.rst author = Seth Rider From 32cfb36ebc5d675685848edd1f831028951d5870 Mon Sep 17 00:00:00 2001 From: Seth Rider Date: Sat, 29 May 2021 13:27:27 +0100 Subject: [PATCH 3/4] minor formatting --- plover_console_ui/add_translation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plover_console_ui/add_translation.py b/plover_console_ui/add_translation.py index 31cfd2b..a2a399d 100644 --- a/plover_console_ui/add_translation.py +++ b/plover_console_ui/add_translation.py @@ -200,6 +200,6 @@ def update_output(self): output += f"\n{self.translation_info}" if self.outcome: - output += f"\n---------------\n{self.outcome}" + output += f"\n -----------------\n{self.outcome}" self.on_output(output) From 65612405ca163ce18bd69199fdddd6f0b9d0071a Mon Sep 17 00:00:00 2001 From: Seth Rider <2758832+psethwick@users.noreply.github.com> Date: Sat, 29 May 2021 13:29:35 +0100 Subject: [PATCH 4/4] Update README.rst RST why..... --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index 73c0889..f285b0a 100644 --- a/README.rst +++ b/README.rst @@ -110,8 +110,11 @@ Commands ======== - ``addtranslation``: enters ``ADD_TRANSLATION`` mode + - Tab/Shift-Tab to move between ``Strokes``, ``Output`` and choose dictionary field + - When in the 'choose dictionary' field you can press left/right arrows to cycle (writable) dictionaries + - ``lookup``: enters ``LOOKUP`` mode - ``output``: toggles Plover output on/off - ``reset``: reconnects current ``machine`` (reloads dictionaries)