diff options
Diffstat (limited to 'poezio/windows/inputs.py')
-rw-r--r-- | poezio/windows/inputs.py | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/poezio/windows/inputs.py b/poezio/windows/inputs.py index 84b95599..01b94ac0 100644 --- a/poezio/windows/inputs.py +++ b/poezio/windows/inputs.py @@ -5,13 +5,14 @@ Text inputs. import curses import logging import string -from typing import List, Dict, Callable, Optional +from typing import List, Dict, Callable, Optional, ClassVar from poezio import keyboard from poezio import common from poezio import poopt -from poezio.windows.base_wins import Win, format_chars -from poezio.windows.funcs import find_first_format_char +from poezio.windows.base_wins import Win +from poezio.ui.consts import FORMAT_CHARS +from poezio.ui.funcs import find_first_format_char from poezio.config import config from poezio.theming import to_curses_attr @@ -40,7 +41,7 @@ class Input(Win): # it easy cut and paste text between various input def __init__(self) -> None: - self.key_func = { + self.key_func: Dict[str, Callable] = { "KEY_LEFT": self.key_left, "KEY_RIGHT": self.key_right, "KEY_END": self.key_end, @@ -65,7 +66,7 @@ class Input(Win): '^?': self.key_backspace, "M-^?": self.delete_word, # '^J': self.add_line_break, - } # type: Dict[str, Callable] + } Win.__init__(self) self.text = '' self.pos = 0 # The position of the “cursor” in the text @@ -75,8 +76,8 @@ class Input(Win): # screen self.on_input = DEFAULT_ON_INPUT # callback called on any key pressed self.color = None # use this color on addstr - self.last_completion = None # type: Optional[str] - self.hit_list = [] # type: List[str] + self.last_completion: Optional[str] = None + self.hit_list: List[str] = [] def on_delete(self) -> None: """ @@ -109,7 +110,7 @@ class Input(Win): """ if self.pos == 0: return True - separators = string.punctuation + ' ' + separators = string.punctuation + ' ' + '\n' while self.pos > 0 and self.text[self.pos - 1] in separators: self.key_left() while self.pos > 0 and self.text[self.pos - 1] not in separators: @@ -122,7 +123,7 @@ class Input(Win): """ if self.is_cursor_at_end(): return True - separators = string.punctuation + ' ' + separators = string.punctuation + ' ' + '\n' while not self.is_cursor_at_end() and self.text[self.pos] in separators: self.key_right() while not self.is_cursor_at_end() and self.text[self. @@ -134,7 +135,7 @@ class Input(Win): """ Delete the word just before the cursor """ - separators = string.punctuation + ' ' + separators = string.punctuation + ' ' + '\n' while self.pos > 0 and self.text[self.pos - 1] in separators: self.key_backspace() while self.pos > 0 and self.text[self.pos - 1] not in separators: @@ -145,7 +146,7 @@ class Input(Win): """ Delete the word just after the cursor """ - separators = string.punctuation + ' ' + separators = string.punctuation + ' ' + '\n' while not self.is_cursor_at_end() and self.text[self.pos] in separators: self.key_dc() while not self.is_cursor_at_end() and self.text[self. @@ -408,12 +409,14 @@ class Input(Win): Normal completion """ pos = self.pos - if pos < len( - self.text) and after.endswith(' ') and self.text[pos] == ' ': + if pos < len(self.text) and after.endswith(' ') and self.text[pos] in ' \n': after = after[: -1] # remove the last space if we are already on a space if not self.last_completion: space_before_cursor = self.text.rfind(' ', 0, pos) + line_before_cursor = self.text.rfind('\n', 0, pos) + if line_before_cursor > space_before_cursor: + space_before_cursor = line_before_cursor if space_before_cursor != -1: begin = self.text[space_before_cursor + 1:pos] else: @@ -487,7 +490,7 @@ class Input(Win): (\x0E to \x19 instead of \x19 + attr). We do not use any } char in this version """ - chars = format_chars + '\n' + chars = FORMAT_CHARS + '\n' if y is not None and x is not None: self.move(y, x) format_char = find_first_format_char(text, chars) @@ -497,7 +500,7 @@ class Input(Win): if text[format_char] == '\n': attr_char = '|' else: - attr_char = self.text_attributes[format_chars.index( + attr_char = self.text_attributes[FORMAT_CHARS.index( text[format_char])] self.addstr(text[:format_char]) self.addstr(attr_char, curses.A_REVERSE) @@ -589,9 +592,10 @@ class HistoryInput(Input): An input with colors and stuff, plus an history ^R allows to search inside the history (as in a shell) """ - __slots__ = ('help_message', 'histo_pos', 'current_completed', 'search') + __slots__ = ('help_message', 'histo_pos', 'current_completed', 'search', + 'history') - history = [] # type: List[str] + global_history: ClassVar[List[str]] = [] def __init__(self) -> None: Input.__init__(self) @@ -600,9 +604,10 @@ class HistoryInput(Input): self.current_completed = '' self.key_func['^R'] = self.toggle_search self.search = False - if config.get('separate_history'): - # pylint: disable=assigning-non-slot - self.history = [] # type: List[str] + if config.getbool('separate_history'): + self.history: List[str] = [] + else: + self.history = self.__class__.global_history def toggle_search(self) -> None: if self.help_message: @@ -679,7 +684,7 @@ class MessageInput(HistoryInput): Also letting the user enter colors or other text markups """ # The history is common to all MessageInput - history = [] # type: List[str] + global_history: ClassVar[List[str]] = [] def __init__(self) -> None: HistoryInput.__init__(self) @@ -696,7 +701,7 @@ class MessageInput(HistoryInput): def cb(attr_char): if attr_char in self.text_attributes: - char = format_chars[self.text_attributes.index(attr_char)] + char = FORMAT_CHARS[self.text_attributes.index(attr_char)] self.do_command(char, False) self.rewrite_text() @@ -725,7 +730,7 @@ class CommandInput(HistoryInput): HelpMessage when a command is started The on_input callback """ - history = [] # type: List[str] + global_history: ClassVar[List[str]] = [] def __init__(self, help_message: str, on_abort, on_success, on_input=None) -> None: HistoryInput.__init__(self) |