windows.bookmark_forms: Type almost everything in this module.

This commit is contained in:
Emmanuel Gil Peyrot 2018-08-17 19:43:35 +01:00
parent ede00f7862
commit 2abbf8274c

View file

@ -2,32 +2,35 @@
Windows used inthe bookmarkstab Windows used inthe bookmarkstab
""" """
import curses import curses
from typing import List, Tuple, Optional
from poezio.windows import base_wins from poezio.windows import base_wins
from poezio.windows.base_wins import Win from poezio.windows.base_wins import Win
from poezio.windows.inputs import Input from poezio.windows.inputs import Input
from poezio.windows.data_forms import FieldInput, FieldInputMixin from poezio.windows.data_forms import FieldInput, FieldInputMixin
from poezio.theming import to_curses_attr, get_theme from poezio.theming import to_curses_attr, get_theme
from poezio.common import safeJID from poezio.common import safeJID
from poezio.bookmarks import Bookmark, BookmarkList
class BookmarkNameInput(FieldInput, Input): class BookmarkNameInput(FieldInput, Input):
def __init__(self, field): def __init__(self, field) -> None:
FieldInput.__init__(self, field) FieldInput.__init__(self, field)
Input.__init__(self) Input.__init__(self)
self.text = field.name self.text = field.name
self.pos = len(self.text) self.pos = len(self.text)
self.color = get_theme().COLOR_NORMAL_TEXT self.color = get_theme().COLOR_NORMAL_TEXT
def save(self): def save(self) -> None:
self._field.name = self.get_text() self._field.name = self.get_text()
def get_help_message(self): def get_help_message(self) -> str:
return 'Edit the text' return 'Edit the text'
class BookmarkJIDInput(FieldInput, Input): class BookmarkJIDInput(FieldInput, Input):
def __init__(self, field): def __init__(self, field) -> None:
FieldInput.__init__(self, field) FieldInput.__init__(self, field)
Input.__init__(self) Input.__init__(self)
jid = safeJID(field.jid) jid = safeJID(field.jid)
@ -36,24 +39,24 @@ class BookmarkJIDInput(FieldInput, Input):
self.pos = len(self.text) self.pos = len(self.text)
self.color = get_theme().COLOR_NORMAL_TEXT self.color = get_theme().COLOR_NORMAL_TEXT
def save(self): def save(self) -> None:
jid = safeJID(self.get_text()) jid = safeJID(self.get_text())
self._field.jid = jid.bare self._field.jid = jid.bare
self._field.nick = jid.resource self._field.nick = jid.resource
def get_help_message(self): def get_help_message(self) -> str:
return 'Edit the text' return 'Edit the text'
class BookmarkMethodInput(FieldInputMixin): class BookmarkMethodInput(FieldInputMixin):
def __init__(self, field): def __init__(self, field) -> None:
FieldInput.__init__(self, field) FieldInput.__init__(self, field)
Win.__init__(self) Win.__init__(self)
self.options = ('local', 'remote') self.options = ('local', 'remote')
# val_pos is the position of the currently selected option # val_pos is the position of the currently selected option
self.val_pos = self.options.index(field.method) self.val_pos = self.options.index(field.method)
def do_command(self, key): def do_command(self, key: str) -> None:
if key == 'KEY_LEFT': if key == 'KEY_LEFT':
if self.val_pos > 0: if self.val_pos > 0:
self.val_pos -= 1 self.val_pos -= 1
@ -64,7 +67,7 @@ class BookmarkMethodInput(FieldInputMixin):
return return
self.refresh() self.refresh()
def refresh(self): def refresh(self) -> None:
self._win.erase() self._win.erase()
self._win.attron(to_curses_attr(self.color)) self._win.attron(to_curses_attr(self.color))
self.addnstr(0, 0, ' ' * self.width, self.width) self.addnstr(0, 0, ' ' * self.width, self.width)
@ -78,22 +81,22 @@ class BookmarkMethodInput(FieldInputMixin):
self._win.attroff(to_curses_attr(self.color)) self._win.attroff(to_curses_attr(self.color))
self._refresh() self._refresh()
def save(self): def save(self) -> None:
self._field.method = self.options[self.val_pos] self._field.method = self.options[self.val_pos]
def get_help_message(self): def get_help_message(self) -> str:
return '←, →: Select a value amongst the others' return '←, →: Select a value amongst the others'
class BookmarkPasswordInput(FieldInput, Input): class BookmarkPasswordInput(FieldInput, Input):
def __init__(self, field): def __init__(self, field) -> None:
FieldInput.__init__(self, field) FieldInput.__init__(self, field)
Input.__init__(self) Input.__init__(self)
self.text = field.password or '' self.text = field.password or ''
self.pos = len(self.text) self.pos = len(self.text)
self.color = get_theme().COLOR_NORMAL_TEXT self.color = get_theme().COLOR_NORMAL_TEXT
def rewrite_text(self): def rewrite_text(self) -> None:
self._win.erase() self._win.erase()
if self.color: if self.color:
self._win.attron(to_curses_attr(self.color)) self._win.attron(to_curses_attr(self.color))
@ -108,27 +111,27 @@ class BookmarkPasswordInput(FieldInput, Input):
self._win.attroff(to_curses_attr(self.color)) self._win.attroff(to_curses_attr(self.color))
self._refresh() self._refresh()
def save(self): def save(self) -> None:
self._field.password = self.get_text() or None self._field.password = self.get_text() or None
def get_help_message(self): def get_help_message(self) -> str:
return 'Edit the secret text' return 'Edit the secret text'
class BookmarkAutojoinWin(FieldInputMixin): class BookmarkAutojoinWin(FieldInputMixin):
def __init__(self, field): def __init__(self, field) -> None:
FieldInput.__init__(self, field) FieldInput.__init__(self, field)
Win.__init__(self) Win.__init__(self)
self.last_key = 'KEY_RIGHT' self.last_key = 'KEY_RIGHT'
self.value = field.autojoin self.value = field.autojoin
def do_command(self, key): def do_command(self, key: str) -> None:
if key == 'KEY_LEFT' or key == 'KEY_RIGHT': if key == 'KEY_LEFT' or key == 'KEY_RIGHT':
self.value = not self.value self.value = not self.value
self.last_key = key self.last_key = key
self.refresh() self.refresh()
def refresh(self): def refresh(self) -> None:
self._win.erase() self._win.erase()
self._win.attron(to_curses_attr(self.color)) self._win.attron(to_curses_attr(self.color))
format_string = '{:^%s}→' % 7 format_string = '{:^%s}→' % 7
@ -141,21 +144,21 @@ class BookmarkAutojoinWin(FieldInputMixin):
self._win.attroff(to_curses_attr(self.color)) self._win.attroff(to_curses_attr(self.color))
self._refresh() self._refresh()
def save(self): def save(self) -> None:
self._field.autojoin = self.value self._field.autojoin = self.value
def get_help_message(self): def get_help_message(self) -> str:
return '← and →: change the value between True and False' return '← and →: change the value between True and False'
class BookmarksWin(Win): class BookmarksWin(Win):
def __init__(self, bookmarks, height, width, y, x): def __init__(self, bookmarks: BookmarkList, height: int, width: int, y: int, x: int) -> None:
self._win = base_wins.TAB_WIN.derwin(height, width, y, x) self._win = base_wins.TAB_WIN.derwin(height, width, y, x)
self.scroll_pos = 0 self.scroll_pos = 0
self._current_input = 0 self._current_input = 0
self.current_horizontal_input = 0 self.current_horizontal_input = 0
self._bookmarks = list(bookmarks) self._bookmarks = list(bookmarks)
self.lines = [] self.lines = [] # type: List[Tuple[BookmarkNameInput, BookmarkJIDInput, BookmarkPasswordInput, BookmarkAutojoinWin, BookmarkMethodInput]]
for bookmark in sorted(self._bookmarks, key=lambda x: x.jid): for bookmark in sorted(self._bookmarks, key=lambda x: x.jid):
self.lines.append((BookmarkNameInput(bookmark), self.lines.append((BookmarkNameInput(bookmark),
BookmarkJIDInput(bookmark), BookmarkJIDInput(bookmark),
@ -164,11 +167,11 @@ class BookmarksWin(Win):
BookmarkMethodInput(bookmark))) BookmarkMethodInput(bookmark)))
@property @property
def current_input(self): def current_input(self) -> int:
return self._current_input return self._current_input
@current_input.setter @current_input.setter
def current_input(self, value): def current_input(self, value: int) -> None:
if 0 <= self._current_input < len(self.lines): if 0 <= self._current_input < len(self.lines):
if 0 <= value < len(self.lines): if 0 <= value < len(self.lines):
self.lines[self._current_input][ self.lines[self._current_input][
@ -178,7 +181,7 @@ class BookmarksWin(Win):
else: else:
self._current_input = 0 self._current_input = 0
def add_bookmark(self, bookmark): def add_bookmark(self, bookmark: Bookmark) -> None:
self.lines.append((BookmarkNameInput(bookmark), self.lines.append((BookmarkNameInput(bookmark),
BookmarkJIDInput(bookmark), BookmarkJIDInput(bookmark),
BookmarkPasswordInput(bookmark), BookmarkPasswordInput(bookmark),
@ -193,18 +196,19 @@ class BookmarksWin(Win):
self.scroll_pos = self.current_input - self.height + 1 self.scroll_pos = self.current_input - self.height + 1
self.refresh() self.refresh()
def del_current_bookmark(self): def del_current_bookmark(self) -> Optional[Bookmark]:
if self.lines: if not self.lines:
bm = self.lines[self.current_input][0]._field return None
to_delete = self.current_input bm = self.lines[self.current_input][0]._field
self.current_input -= 1 to_delete = self.current_input
del self.lines[to_delete] self.current_input -= 1
if self.scroll_pos: del self.lines[to_delete]
self.scroll_pos -= 1 if self.scroll_pos:
self.refresh() self.scroll_pos -= 1
return bm self.refresh()
return bm
def resize(self, height, width, y, x): def resize(self, height: int, width: int, y: int, x: int) -> None:
self.height = height self.height = height
self.width = width self.width = width
self._win = base_wins.TAB_WIN.derwin(height, width, y, x) self._win = base_wins.TAB_WIN.derwin(height, width, y, x)
@ -213,7 +217,7 @@ class BookmarksWin(Win):
while self.current_input - self.scroll_pos > self.height - 1: while self.current_input - self.scroll_pos > self.height - 1:
self.scroll_pos += 1 self.scroll_pos += 1
def go_to_next_line_input(self): def go_to_next_line_input(self) -> None:
if not self.lines: if not self.lines:
return return
if self.current_input == len(self.lines) - 1: if self.current_input == len(self.lines) - 1:
@ -233,7 +237,7 @@ class BookmarksWin(Win):
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
def go_to_previous_line_input(self): def go_to_previous_line_input(self) -> None:
if not self.lines: if not self.lines:
return return
if self.current_input == 0: if self.current_input == 0:
@ -251,7 +255,7 @@ class BookmarksWin(Win):
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
def go_to_next_horizontal_input(self): def go_to_next_horizontal_input(self) -> None:
if not self.lines: if not self.lines:
return return
self.lines[self.current_input][ self.lines[self.current_input][
@ -264,12 +268,12 @@ class BookmarksWin(Win):
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
def go_to_next_page(self): def go_to_next_page(self) -> bool:
if not self.lines: if not self.lines:
return return False
if self.current_input == len(self.lines) - 1: if self.current_input == len(self.lines) - 1:
return return False
self.lines[self.current_input][ self.lines[self.current_input][
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
@ -287,12 +291,12 @@ class BookmarksWin(Win):
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
return True return True
def go_to_previous_page(self): def go_to_previous_page(self) -> bool:
if not self.lines: if not self.lines:
return return False
if self.current_input == 0: if self.current_input == 0:
return return False
self.lines[self.current_input][ self.lines[self.current_input][
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
@ -310,7 +314,7 @@ class BookmarksWin(Win):
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
return True return True
def go_to_previous_horizontal_input(self): def go_to_previous_horizontal_input(self) -> None:
if not self.lines: if not self.lines:
return return
if self.current_horizontal_input == 0: if self.current_horizontal_input == 0:
@ -323,13 +327,13 @@ class BookmarksWin(Win):
self.current_horizontal_input].set_color( self.current_horizontal_input].set_color(
get_theme().COLOR_SELECTED_ROW) get_theme().COLOR_SELECTED_ROW)
def on_input(self, key): def on_input(self, key: str) -> None:
if not self.lines: if not self.lines:
return return
self.lines[self.current_input][ self.lines[self.current_input][
self.current_horizontal_input].do_command(key) self.current_horizontal_input].do_command(key)
def refresh(self): def refresh(self) -> None:
# store the cursor status # store the cursor status
self._win.erase() self._win.erase()
y = -self.scroll_pos y = -self.scroll_pos
@ -363,12 +367,12 @@ class BookmarksWin(Win):
else: else:
curses.curs_set(1) curses.curs_set(1)
def refresh_current_input(self): def refresh_current_input(self) -> None:
if self.lines: if self.lines:
self.lines[self.current_input][ self.lines[self.current_input][
self.current_horizontal_input].refresh() self.current_horizontal_input].refresh()
def save(self): def save(self) -> None:
for line in self.lines: for line in self.lines:
for item in line: for item in line:
item.save() item.save()