Skip to content

Commit

Permalink
Merge pull request #22 from psethwick/choose-dict-addtranslation
Browse files Browse the repository at this point in the history
Capability to choose target dictionary for add-translation
  • Loading branch information
psethwick authored May 29, 2021
2 parents 032d2dd + 6561240 commit 526df4c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +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)
Expand Down
89 changes: 73 additions & 16 deletions plover_console_ui/add_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"{self.dicts[self.dict_index].path}",
style="class:normal",
key_bindings=picker_kb
), height=1)

self.strokes_field = TextArea(
prompt="Strokes: ",
height=1,
Expand Down Expand Up @@ -73,29 +109,42 @@ 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)
self.update_output()

@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)
Expand Down Expand Up @@ -134,15 +183,23 @@ 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:
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)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 526df4c

Please sign in to comment.