aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/myii.py
blob: 77bf813f1adc1a3a858f6a79eb91df574053fb88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- python -*-
from plugins.ii import II

from common import *

class MyII(Entry):
    def __init__(self, *args, channel = None, prefix = None, display = None, backlog = None, watch = None, **kwargs):
        self.semaphore = threading.Semaphore()
        self.log = []
        self.text = ''
        self.ii = II(channel, prefix = prefix)
        self.display = display
        self.backlog = backlog
        self.watch = (lambda s : False) if watch is None else watch
        self.show_line = 0
        Entry.__init__(self, *args, **kwargs)
    
    def start(self):
        xasync(self.wait, name = 'ii')
    
    def wait(self):
        self.refresh()
        def refresh_():
            time.sleep(0.1)
            self.refresh()
        while True:
            xasync(refresh_, name = 'ii')
            try:
                self.ii.wait()
            except:
                time.sleep(5)
            self.refresh()
    
    def refresh(self):
        on_last = False
        highlighted = 0
        self.semaphore.acquire()
        try:
            new = self.ii.read()
            if self.backlog is not None:
                new = new[-(self.backlog):]
                self.backlog = None
            for i in range(len(new)):
                if self.watch(new[i]):
                    new[i] = (new[i], True)
                    highlighted += 1
                else:
                    new[i] = (new[i], False)
            on_last = self.show_line >= len(self.log) - 1
            self.log.extend(new)
            if on_last and len(new) > 0:
                self.show_line = len(self.log) - 1
                text = self.log[self.show_line][0]
                if self.log[self.show_line][1]:
                    text = '\033[33m%s\033[39m' % text
                self.text = text
        finally:
            self.semaphore.release()
        if len(new) > 0:
            if on_last:
                self.invalidate()
            if highlighted > 0 and self.display is not None:
                self.display.adjust(highlighted)
    
    def action(self, col, button, x, y):
        if len(self.log) == 0:
            return
        update = False
        self.semaphore.acquire()
        try:
            if button == LEFT_BUTTON:
                if self.log[self.show_line][1]:
                    self.log[self.show_line] = (self.log[self.show_line][0], False)
                    self.text = self.log[self.show_line][0]
                    update = True
                    if self.display is not None:
                        self.display.adjust(-1)
                elif self.watch(self.log[self.show_line][0]):
                    self.log[self.show_line] = (self.log[self.show_line][0], True)
                    self.text = '\033[33m%s\033[39m' % self.log[self.show_line][0]
                    update = True
                    if self.display is not None:
                        self.display.adjust(-1)
            elif button == MIDDLE_BUTTON:
                adj = 0
                for i in range(self.show_line):
                    if self.log[i][1]:
                        adj -= 1
                self.log = self.log[self.show_line:]
                self.show_line = 0
                if adj != 0:
                    self.display.adjust(adj)
            elif button == RIGHT_BUTTON:
                for i, (text, marked) in enumerate(self.log):
                    if marked:
                        self.show_line = i
                        self.text = '\033[33m%s\033[39m' % text if marked else text
                        update = True
                        break
            elif button in (SCROLL_UP, SCROLL_DOWN):
                line = self.show_line + (-1 if button == SCROLL_UP else +1)
                if 0 <= line < len(self.log):
                    self.show_line = line
                    (text, marked) = self.log[line]
                    self.text = '\033[33m%s\033[39m' % text if marked else text
                    update = True
        finally:
            self.semaphore.release()
        if update:
            self.invalidate()
    
    def function(self):
        return self.text