From 85afda8832e9a31fa3c55b16c6ccab95847210b2 Mon Sep 17 00:00:00 2001 From: purxiz Date: Wed, 6 Jan 2021 11:48:08 -0600 Subject: [PATCH 1/2] fixes incorrect hint base The bases for the hint were being decoded as base 16, despite being displayed as base 36 (0-9, a-z). This fix makes it so typing 'search string .index' returns the unicode char at the expected index, based on what is displayed by the program. It also slightly changes the behavior. Before, only the selected result would appear. For example, musical note `musical note .18` would only show a single result, since there was only a single result at index 18. Now, searching by index doesn't remove codepoints/results, it simply highlights the result at the correct index for you. --- kittens/unicode_input/main.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/kittens/unicode_input/main.py b/kittens/unicode_input/main.py index 6cf6ea116..e3395bff4 100644 --- a/kittens/unicode_input/main.py +++ b/kittens/unicode_input/main.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2018, Kovid Goyal - import os import string import subprocess @@ -172,11 +171,11 @@ class Table: if self.codepoints: return self.codepoints[self.current_idx] - def set_codepoints(self, codepoints: List[int], mode: str = HEX) -> None: + def set_codepoints(self, codepoints: List[int], mode: str = HEX, current_idx: int = 0) -> None: self.codepoints = codepoints self.mode = mode self.layout_dirty = True - self.current_idx = 0 + self.current_idx = current_idx def codepoint_at_hint(self, hint: str) -> int: return self.codepoints[decode_hint(hint)] @@ -270,8 +269,10 @@ class Table: def is_index(w: str) -> bool: + if w[0] != INDEX_CHAR: + return False try: - int(w.lstrip(INDEX_CHAR), 16) + int(w.lstrip(INDEX_CHAR), 36) return True except Exception: return False @@ -306,6 +307,7 @@ class UnicodeInput(Handler): def update_codepoints(self) -> None: codepoints = None + iindex_word = 0 if self.mode is HEX: q: Tuple[str, Optional[Union[str, Sequence[int]]]] = (self.mode, None) codepoints = self.recent @@ -324,14 +326,11 @@ class UnicodeInput(Handler): if index_words: index_word = words[index_words[0]] words = words[:index_words[0]] + iindex_word = int(index_word.lstrip(INDEX_CHAR), 36) codepoints = codepoints_matching_search(tuple(words)) - if index_words: - iindex_word = int(index_word.lstrip(INDEX_CHAR), 16) - if codepoints and iindex_word < len(codepoints): - codepoints = [codepoints[iindex_word]] if q != self.last_updated_code_point_at: self.last_updated_code_point_at = q - self.table.set_codepoints(codepoints or [], self.mode) + self.table.set_codepoints(codepoints or [], self.mode, iindex_word if iindex_word < len(codepoints) else 0) def update_current_char(self) -> None: self.update_codepoints() From 89c62377da0bb56f21350453a3d30caa08088773 Mon Sep 17 00:00:00 2001 From: purxiz Date: Wed, 6 Jan 2021 13:24:38 -0600 Subject: [PATCH 2/2] fix type error I moved the location of the check for an overflowing index to avoid a type error that was revealed by mypy. I also think this is a better place to put it from a code standpoint in general... --- kittens/unicode_input/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kittens/unicode_input/main.py b/kittens/unicode_input/main.py index e3395bff4..aeabc7a30 100644 --- a/kittens/unicode_input/main.py +++ b/kittens/unicode_input/main.py @@ -175,7 +175,7 @@ class Table: self.codepoints = codepoints self.mode = mode self.layout_dirty = True - self.current_idx = current_idx + self.current_idx = current_idx if current_idx < len(codepoints) else 0 def codepoint_at_hint(self, hint: str) -> int: return self.codepoints[decode_hint(hint)] @@ -330,7 +330,7 @@ class UnicodeInput(Handler): codepoints = codepoints_matching_search(tuple(words)) if q != self.last_updated_code_point_at: self.last_updated_code_point_at = q - self.table.set_codepoints(codepoints or [], self.mode, iindex_word if iindex_word < len(codepoints) else 0) + self.table.set_codepoints(codepoints or [], self.mode, iindex_word) def update_current_char(self) -> None: self.update_codepoints()