summaryrefslogtreecommitdiff
path: root/poezio/windows/bookmark_forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'poezio/windows/bookmark_forms.py')
-rw-r--r--poezio/windows/bookmark_forms.py96
1 files changed, 56 insertions, 40 deletions
diff --git a/poezio/windows/bookmark_forms.py b/poezio/windows/bookmark_forms.py
index 2940ef04..a0e57cc7 100644
--- a/poezio/windows/bookmark_forms.py
+++ b/poezio/windows/bookmark_forms.py
@@ -4,22 +4,23 @@ Windows used inthe bookmarkstab
import curses
from typing import List, Tuple, Optional
-from poezio.windows import base_wins
+from slixmpp import JID, InvalidJID
+
from poezio.windows.base_wins import Win
from poezio.windows.inputs import Input
from poezio.windows.data_forms import FieldInput, FieldInputMixin
from poezio.theming import to_curses_attr, get_theme
-from poezio.common import safeJID
from poezio.bookmarks import Bookmark, BookmarkList
class BookmarkNameInput(FieldInput, Input):
- def __init__(self, field) -> None:
+ def __init__(self, field: Bookmark) -> None:
FieldInput.__init__(self, field)
Input.__init__(self)
self.text = field.name
- self.pos = len(self.text)
+ self.pos = 0
+ self.view_pos = 0
self.color = get_theme().COLOR_NORMAL_TEXT
def save(self) -> None:
@@ -30,17 +31,24 @@ class BookmarkNameInput(FieldInput, Input):
class BookmarkJIDInput(FieldInput, Input):
- def __init__(self, field) -> None:
+ def __init__(self, field: Bookmark) -> None:
FieldInput.__init__(self, field)
Input.__init__(self)
- jid = safeJID(field.jid)
+ try:
+ jid = JID(field.jid)
+ except InvalidJID:
+ jid = JID('')
jid.resource = field.nick or None
self.text = jid.full
- self.pos = len(self.text)
+ self.pos = 0
+ self.view_pos = 0
self.color = get_theme().COLOR_NORMAL_TEXT
def save(self) -> None:
- jid = safeJID(self.get_text())
+ try:
+ jid = JID(self.get_text())
+ except InvalidJID:
+ jid = JID('')
self._field.jid = jid.bare
self._field.nick = jid.resource
@@ -49,14 +57,14 @@ class BookmarkJIDInput(FieldInput, Input):
class BookmarkMethodInput(FieldInputMixin):
- def __init__(self, field) -> None:
+ def __init__(self, field: Bookmark) -> None:
FieldInput.__init__(self, field)
Win.__init__(self)
self.options = ('local', 'remote')
# val_pos is the position of the currently selected option
self.val_pos = self.options.index(field.method)
- def do_command(self, key: str) -> None:
+ def do_command(self, key: str, raw: bool = False) -> None:
if key == 'KEY_LEFT':
if self.val_pos > 0:
self.val_pos -= 1
@@ -89,7 +97,7 @@ class BookmarkMethodInput(FieldInputMixin):
class BookmarkPasswordInput(FieldInput, Input):
- def __init__(self, field) -> None:
+ def __init__(self, field: Bookmark) -> None:
FieldInput.__init__(self, field)
Input.__init__(self)
self.text = field.password or ''
@@ -119,13 +127,13 @@ class BookmarkPasswordInput(FieldInput, Input):
class BookmarkAutojoinWin(FieldInputMixin):
- def __init__(self, field) -> None:
+ def __init__(self, field: Bookmark) -> None:
FieldInput.__init__(self, field)
Win.__init__(self)
self.last_key = 'KEY_RIGHT'
self.value = field.autojoin
- def do_command(self, key: str) -> None:
+ def do_command(self, key: str, raw: bool = False) -> None:
if key == 'KEY_LEFT' or key == 'KEY_RIGHT':
self.value = not self.value
self.last_key = key
@@ -155,14 +163,14 @@ class BookmarksWin(Win):
__slots__ = ('scroll_pos', '_current_input', 'current_horizontal_input',
'_bookmarks', 'lines')
- 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)
+ def __init__(self, bookmarks: BookmarkList) -> None:
+ Win.__init__(self)
self.scroll_pos = 0
self._current_input = 0
self.current_horizontal_input = 0
self._bookmarks = list(bookmarks)
- self.lines = [] # type: List[Tuple[BookmarkNameInput, BookmarkJIDInput, BookmarkPasswordInput, BookmarkAutojoinWin, BookmarkMethodInput]]
- for bookmark in sorted(self._bookmarks, key=lambda x: x.jid):
+ self.lines: List[Tuple[BookmarkNameInput, BookmarkJIDInput, BookmarkPasswordInput, BookmarkAutojoinWin, BookmarkMethodInput]] = []
+ for bookmark in sorted(self._bookmarks, key=lambda x: str(x.jid)):
self.lines.append((BookmarkNameInput(bookmark),
BookmarkJIDInput(bookmark),
BookmarkPasswordInput(bookmark),
@@ -190,11 +198,13 @@ class BookmarksWin(Win):
BookmarkPasswordInput(bookmark),
BookmarkAutojoinWin(bookmark),
BookmarkMethodInput(bookmark)))
- self.lines[self.current_input][
- self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ if len(self.lines) > 1:
+ self.lines[self.current_input][
+ self.current_horizontal_input].set_color(
+ get_theme().COLOR_NORMAL_TEXT)
self.current_horizontal_input = 0
- self.current_input = len(self.lines) - 1
+ if len(self.lines) > 1:
+ self.current_input = len(self.lines) - 1
if self.current_input - self.scroll_pos > self.height - 1:
self.scroll_pos = self.current_input - self.height + 1
self.refresh()
@@ -212,9 +222,7 @@ class BookmarksWin(Win):
return bm
def resize(self, height: int, width: int, y: int, x: int) -> None:
- self.height = height
- self.width = width
- self._win = base_wins.TAB_WIN.derwin(height, width, y, x)
+ super().resize(height, width, y, x)
# Adjust the scroll position, if resizing made the window too small
# for the cursor to be visible
while self.current_input - self.scroll_pos > self.height - 1:
@@ -245,9 +253,10 @@ class BookmarksWin(Win):
return
if self.current_input == 0:
return
+ theme = get_theme()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ theme.COLOR_NORMAL_TEXT)
self.current_input -= 1
# Adjust the scroll position if the current_input would be outside
# of the visible area
@@ -256,20 +265,21 @@ class BookmarksWin(Win):
self.refresh()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_SELECTED_ROW)
+ theme.COLOR_SELECTED_ROW)
def go_to_next_horizontal_input(self) -> None:
if not self.lines:
return
+ theme = get_theme()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ theme.COLOR_NORMAL_TEXT)
self.current_horizontal_input += 1
- if self.current_horizontal_input > 3:
+ if self.current_horizontal_input > 4:
self.current_horizontal_input = 0
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_SELECTED_ROW)
+ theme.COLOR_SELECTED_ROW)
def go_to_next_page(self) -> bool:
if not self.lines:
@@ -278,9 +288,10 @@ class BookmarksWin(Win):
if self.current_input == len(self.lines) - 1:
return False
+ theme = get_theme()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ theme.COLOR_NORMAL_TEXT)
inc = min(self.height, len(self.lines) - self.current_input - 1)
if self.current_input + inc - self.scroll_pos > self.height - 1:
@@ -291,7 +302,7 @@ class BookmarksWin(Win):
self.current_input += inc
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_SELECTED_ROW)
+ theme.COLOR_SELECTED_ROW)
return True
def go_to_previous_page(self) -> bool:
@@ -301,9 +312,10 @@ class BookmarksWin(Win):
if self.current_input == 0:
return False
+ theme = get_theme()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ theme.COLOR_NORMAL_TEXT)
dec = min(self.height, self.current_input)
self.current_input -= dec
@@ -314,7 +326,7 @@ class BookmarksWin(Win):
self.refresh()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_SELECTED_ROW)
+ theme.COLOR_SELECTED_ROW)
return True
def go_to_previous_horizontal_input(self) -> None:
@@ -322,19 +334,20 @@ class BookmarksWin(Win):
return
if self.current_horizontal_input == 0:
return
+ theme = get_theme()
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_NORMAL_TEXT)
+ theme.COLOR_NORMAL_TEXT)
self.current_horizontal_input -= 1
self.lines[self.current_input][
self.current_horizontal_input].set_color(
- get_theme().COLOR_SELECTED_ROW)
+ theme.COLOR_SELECTED_ROW)
- def on_input(self, key: str) -> None:
+ def on_input(self, key: str, raw: bool = False) -> None:
if not self.lines:
return
self.lines[self.current_input][
- self.current_horizontal_input].do_command(key)
+ self.current_horizontal_input].do_command(key, raw=raw)
def refresh(self) -> None:
# store the cursor status
@@ -356,7 +369,7 @@ class BookmarksWin(Win):
continue
if i >= self.height + self.scroll_pos:
break
- for j in range(4):
+ for j in range(5):
inp[j].refresh()
if self.lines and self.current_input < self.height - 1:
@@ -377,5 +390,8 @@ class BookmarksWin(Win):
def save(self) -> None:
for line in self.lines:
- for item in line:
- item.save()
+ line[0].save()
+ line[1].save()
+ line[2].save()
+ line[3].save()
+ line[4].save()