muc.py 4.88 KB
Newer Older
1 2 3 4 5 6 7
"""
Windows specific to a MUC
"""

import logging
import curses

8 9
from typing import List, Tuple, Optional

Link Mauve's avatar
Link Mauve committed
10
from poezio.windows.base_wins import Win
11

12 13 14
from poezio import poopt
from poezio.config import config
from poezio.theming import to_curses_attr, get_theme
15 16 17 18 19
from poezio.user import User

log = logging.getLogger(__name__)

CachedUser = Tuple[str, str, Optional[str], str, str]
20

mathieui's avatar
mathieui committed
21

22
def userlist_to_cache(userlist: List[User]) -> List[CachedUser]:
23 24
    result = []
    for user in userlist:
mathieui's avatar
mathieui committed
25 26
        result.append((user.nick, user.status, user.chatstate,
                       user.affiliation, user.role))
27 28
    return result

mathieui's avatar
mathieui committed
29

30
class UserList(Win):
31 32
    __slots__ = ('pos', 'cache')

33
    def __init__(self) -> None:
34 35
        Win.__init__(self)
        self.pos = 0
36
        self.cache = []  # type: List[CachedUser]
37

38
    def scroll_up(self) -> bool:
mathieui's avatar
mathieui committed
39
        self.pos += self.height - 1
40 41
        return True

42
    def scroll_down(self) -> bool:
43
        pos = self.pos
mathieui's avatar
mathieui committed
44
        self.pos -= self.height - 1
45 46 47 48
        if self.pos < 0:
            self.pos = 0
        return self.pos != pos

49
    def draw_plus(self, y: int) -> None:
mathieui's avatar
mathieui committed
50 51
        self.addstr(y, self.width - 2, '++',
                    to_curses_attr(get_theme().COLOR_MORE_INDICATOR))
52

53
    def refresh_if_changed(self, users: List[User]) -> None:
54
        old = self.cache
mathieui's avatar
mathieui committed
55
        new = userlist_to_cache(users[self.pos:self.pos + self.height])
56 57 58 59
        if len(old) != len(new):
            self.cache = new
            self.refresh(users)
            return
60 61
        for a, b in zip(old, new):
            if a != b:
62 63
                self.cache = new
                self.refresh(users)
64
                return
65

66
    def refresh(self, users: List[User]) -> None:
67
        log.debug('Refresh: %s', self.__class__.__name__)
68
        if config.get('hide_user_list'):
mathieui's avatar
mathieui committed
69
            return  # do not refresh if this win is hidden.
70 71 72 73
        if len(users) < self.height:
            self.pos = 0
        elif self.pos >= len(users) - self.height and self.pos != 0:
            self.pos = len(users) - self.height
74
        self._win.erase()
mathieui's avatar
mathieui committed
75 76
        asc_sort = (config.get('user_list_sort').lower() == 'asc')
        if asc_sort:
77
            y, _ = self._win.getmaxyx()
78 79 80 81
            y -= 1
        else:
            y = 0

mathieui's avatar
mathieui committed
82
        for user in users[self.pos:self.pos + self.height]:
83 84
            self.draw_role_affiliation(y, user)
            self.draw_status_chatstate(y, user)
mathieui's avatar
mathieui committed
85
            self.addstr(y, 2, poopt.cut_by_columns(user.nick, self.width - 2),
mathieui's avatar
mathieui committed
86
                        to_curses_attr(user.color))
mathieui's avatar
mathieui committed
87
            if asc_sort:
88 89
                y -= 1
            else:
90 91 92 93 94
                y += 1
            if y == self.height:
                break
        # draw indicators of position in the list
        if self.pos > 0:
mathieui's avatar
mathieui committed
95
            if asc_sort:
mathieui's avatar
mathieui committed
96
                self.draw_plus(self.height - 1)
97 98 99
            else:
                self.draw_plus(0)
        if self.pos + self.height < len(users):
mathieui's avatar
mathieui committed
100
            if asc_sort:
101 102
                self.draw_plus(0)
            else:
mathieui's avatar
mathieui committed
103
                self.draw_plus(self.height - 1)
104
        self._refresh()
105

106
    def draw_role_affiliation(self, y: int, user: User) -> None:
107 108 109 110 111
        theme = get_theme()
        color = theme.color_role(user.role)
        symbol = theme.char_affiliation(user.affiliation)
        self.addstr(y, 1, symbol, to_curses_attr(color))

112
    def draw_status_chatstate(self, y: int, user: User) -> None:
113 114 115 116 117 118 119 120 121 122 123
        show_col = get_theme().color_show(user.show)
        if user.chatstate == 'composing':
            char = get_theme().CHAR_CHATSTATE_COMPOSING
        elif user.chatstate == 'active':
            char = get_theme().CHAR_CHATSTATE_ACTIVE
        elif user.chatstate == 'paused':
            char = get_theme().CHAR_CHATSTATE_PAUSED
        else:
            char = get_theme().CHAR_STATUS
        self.addstr(y, 0, char, to_curses_attr(show_col))

mathieui's avatar
mathieui committed
124
    def resize(self, height: int, width: int, y: int, x: int) -> None:
125 126 127 128 129
        separator = to_curses_attr(get_theme().COLOR_VERTICAL_SEPARATOR)
        self._resize(height, width, y, x)
        self._win.attron(separator)
        self._win.vline(0, 0, curses.ACS_VLINE, self.height)
        self._win.attroff(separator)
130

mathieui's avatar
mathieui committed
131

132
class Topic(Win):
133 134
    __slots__ = ('_message')

135
    def __init__(self) -> None:
136 137 138
        Win.__init__(self)
        self._message = ''

139
    def refresh(self, topic: Optional[str] = None) -> None:
140
        log.debug('Refresh: %s', self.__class__.__name__)
141
        self._win.erase()
142
        if topic is not None:
mathieui's avatar
mathieui committed
143
            msg = topic[:self.width - 1]
144
        else:
mathieui's avatar
mathieui committed
145
            msg = self._message[:self.width - 1]
146
        self.addstr(0, 0, msg, to_curses_attr(get_theme().COLOR_TOPIC_BAR))
147
        _, x = self._win.getyx()
148 149
        remaining_size = self.width - x
        if remaining_size:
mathieui's avatar
mathieui committed
150
            self.addnstr(' ' * remaining_size, remaining_size,
151 152
                         to_curses_attr(get_theme().COLOR_INFORMATION_BAR))
        self._refresh()
153

154
    def set_message(self, message) -> None:
155
        self._message = message