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

import logging
log = logging.getLogger(__name__)

import curses

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 20 21
def userlist_to_cache(userlist):
    result = []
    for user in userlist:
        result.append((user.nick, user.status, user.chatstate, user.affiliation, user.role))
    return result

22 23 24 25
class UserList(Win):
    def __init__(self):
        Win.__init__(self)
        self.pos = 0
26
        self.cache = []
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    def scroll_up(self):
        self.pos += self.height-1
        return True

    def scroll_down(self):
        pos = self.pos
        self.pos -= self.height-1
        if self.pos < 0:
            self.pos = 0
        return self.pos != pos

    def draw_plus(self, y):
        self.addstr(y, self.width-2, '++', to_curses_attr(get_theme().COLOR_MORE_INDICATOR))

42 43 44 45 46 47 48 49 50 51 52 53 54

    def refresh_if_changed(self, users):
        old = self.cache
        new = userlist_to_cache(users[self.pos:self.pos+self.height])
        if len(old) != len(new):
            self.cache = new
            self.refresh(users)
            return
        for i in range(len(old)):
            if old[i] != new[i]:
                self.cache = new
                self.refresh(users)

55 56
    def refresh(self, users):
        log.debug('Refresh: %s', self.__class__.__name__)
57
        if config.get('hide_user_list'):
58
            return # do not refresh if this win is hidden.
59 60 61 62
        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
63
        self._win.erase()
mathieui's avatar
mathieui committed
64 65
        asc_sort = (config.get('user_list_sort').lower() == 'asc')
        if asc_sort:
66 67 68 69 70
            y, x = self._win.getmaxyx()
            y -= 1
        else:
            y = 0

71
        for user in users[self.pos:self.pos+self.height]:
72 73 74 75 76
            self.draw_role_affiliation(y, user)
            self.draw_status_chatstate(y, user)
            self.addstr(y, 2,
                    poopt.cut_by_columns(user.nick, self.width - 2),
                    to_curses_attr(user.color))
mathieui's avatar
mathieui committed
77
            if asc_sort:
78 79
                y -= 1
            else:
80 81 82 83 84
                y += 1
            if y == self.height:
                break
        # draw indicators of position in the list
        if self.pos > 0:
mathieui's avatar
mathieui committed
85
            if asc_sort:
86 87 88 89
                self.draw_plus(self.height-1)
            else:
                self.draw_plus(0)
        if self.pos + self.height < len(users):
mathieui's avatar
mathieui committed
90
            if asc_sort:
91 92 93 94
                self.draw_plus(0)
            else:
                self.draw_plus(self.height-1)
        self._refresh()
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    def draw_role_affiliation(self, y, user):
        theme = get_theme()
        color = theme.color_role(user.role)
        symbol = theme.char_affiliation(user.affiliation)
        self.addstr(y, 1, symbol, to_curses_attr(color))

    def draw_status_chatstate(self, y, user):
        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))

    def resize(self, height, width, y, x):
115 116 117 118 119
        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)
120 121 122 123 124 125 126 127

class Topic(Win):
    def __init__(self):
        Win.__init__(self)
        self._message = ''

    def refresh(self, topic=None):
        log.debug('Refresh: %s', self.__class__.__name__)
128 129 130 131 132 133 134 135 136 137 138 139
        self._win.erase()
        if topic:
            msg = topic[:self.width-1]
        else:
            msg = self._message[:self.width-1]
        self.addstr(0, 0, msg, to_curses_attr(get_theme().COLOR_TOPIC_BAR))
        (y, x) = self._win.getyx()
        remaining_size = self.width - x
        if remaining_size:
            self.addnstr(' '*remaining_size, remaining_size,
                         to_curses_attr(get_theme().COLOR_INFORMATION_BAR))
        self._refresh()
140 141 142 143

    def set_message(self, message):
        self._message = message