aboutsummaryrefslogblamecommitdiffstats
path: root/xpybar/config/myii.py
blob: 77bf813f1adc1a3a858f6a79eb91df574053fb88 (plain) (tree)
















































































































                                                                                                                     
# -*- 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