Skip to content

Commit

Permalink
strip brackets from function key names when determining method name
Browse files Browse the repository at this point in the history
  • Loading branch information
Menyadar committed Dec 7, 2023
1 parent 7697871 commit 06e7ccc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/twisted/conch/insults/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def functionKeyReceived(self, keyID, modifier):
name = keyID
if not isinstance(keyID, str):
name = name.decode("utf-8")
func = getattr(self, "func_" + name, None)
func = getattr(self, "func_" + name[1:-1], None)
if func is not None:
func(modifier)

Expand Down
1 change: 1 addition & 0 deletions src/twisted/conch/newsfragments/12046.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.conch.insults.window.Widget objects now correctly respond to function keys
49 changes: 48 additions & 1 deletion src/twisted/conch/test/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from typing import Callable

from twisted.conch.insults.window import ScrolledArea, TextOutput, TopWindow
from twisted.conch.insults.insults import FUNCTION_KEYS
from twisted.conch.insults.window import ScrolledArea, Selection, TextOutput, TopWindow
from twisted.trial.unittest import TestCase


Expand Down Expand Up @@ -66,3 +67,49 @@ def test_parent(self) -> None:
scrolled = ScrolledArea(widget)
self.assertIs(widget.parent, scrolled._viewport)
self.assertIs(scrolled._viewport.parent, scrolled)


class SelectionTests(TestCase):
"""
Tests for L{Selection}, a widget which allows to select item from
list of items.
"""

seq = [f"{x}".encode("ascii") for x in range(10)]
keys = {
"up": b"[UP_ARROW]",
"down": b"[DOWN_ARROW]",
"pgup": b"[PGUP]",
"pgdn": b"[PGDN]",
}

def test_defined_keynames(self) -> None:
"""
Test if expected key names are still defined in t.c.i.i.FUNCTION_KEYS
"""
self.assertTrue(set(self.keys.values()).issubset(set(FUNCTION_KEYS)))

def test_selection(self) -> None:
"""
Test if sending function key codes actually changes focus
"""

widget = Selection(self.seq, None)
widget.height = 10
self.assertIs(widget.focusedIndex, 0)

# Move down by one, second element should be selected
widget.keystrokeReceived(self.keys["down"], None)
self.assertIs(widget.focusedIndex, 1)

# Move down by page, last element should be selected
widget.keystrokeReceived(self.keys["pgdn"], None)
self.assertIs(widget.focusedIndex, 9)

# Move up by one, second to last element should be selected
widget.keystrokeReceived(self.keys["up"], None)
self.assertIs(widget.focusedIndex, 8)

# Move up by page, first element should be selected
widget.keystrokeReceived(self.keys["pgup"], None)
self.assertIs(widget.focusedIndex, 0)

0 comments on commit 06e7ccc

Please sign in to comment.