aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/myipaddress.py
blob: 424dfacf0c452c5f1c01e92ba242812c0d0ed5d3 (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
# -*- python -*-
from plugins.ipaddress import IPAddress
from plugins.clock     import Clock

from common import *

# TODO add IPv6 address
class MyIPAddress(Entry):
    def __init__(self, *args, ignore = None, public = True, both = False, **kwargs):
        self.ignore = ['lo'] if ignore is None else ignore
        self.text = None
        self.show_both = both
        self.show_public = public
        self.show_nic = None
        Entry.__init__(self, *args, **kwargs)
        def init():
            self.refresh()
            xasync(lambda : Clock(sync_to = 10 * Clock.MINUTES).continuous_sync(t(self.refresh)), name = 'ipaddress')
        xasync(init, name = 'ipaddress')
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            self.show_public = not self.show_public
            self.invalidate()
        elif button == MIDDLE_BUTTON:
            self.show_both = not self.show_both
            self.invalidate()
        elif button == RIGHT_BUTTON:
            xasync(self.refresh, name = 'ipaddress')
        elif button in (SCROLL_UP, SCROLL_DOWN):
            nic = self.show_nic
            if nic is None:
                return
            nics = list(sorted(self.text[0].keys()))
            try:
                nic = nics.index(nic)
            except:
                if len(nics) == 0:
                    self.show_nic = None
                    self.invalidate()
                else:
                    self.show_nic = nics[0]
                    self.invalidate()
            nic += +1 if button == SCROLL_UP else -1
            if 0 <= nic < len(nics):
                self.show_nic = nics[nic]
                self.invalidate()
    
    def refresh(self):
        text_private, text_public, text_both = {}, {}, {}
        ipa = IPAddress(*(self.ignore))
        for nic in ipa.nics.keys():
            (state, a4, a6, a) = ipa.nics[nic]
            if state == IPAddress.DOWN:
                label = '31'
            elif state == IPAddress.UP:
                label = '32'
            elif state == IPAddress.UNKNOWN:
                label = '33'
            else:
                label = '39'
            label = '\033[%sm%s\033[0m' % (label, nic)
            prv = pub = ''
            if a4 is not None:
                prv += ' ' + a4
            if a6 is not None:
                prv += ' ' + a6
            if a is not None:
                pub += ' ' + a
            finalise = lambda addrs : '%s:%s' % (label, (addrs if addrs != '' else ' no address'))
            text_private[nic] = finalise(prv)
            text_public[nic]  = finalise(pub)
            text_both[nic]    = finalise(pub + prv)
        self.text = (text_private, text_public, text_both)
        self.invalidate()
    
    def function(self):
        text = self.text
        if text is None:
            return '...'
        text = text[2 if self.show_both else (1 if self.show_public else 0)]
        nic = self.show_nic
        if nic is None or nic not in text:
            if len(text.keys()) == 0:
                self.show_nic = None
                return 'No NIC available'
            nic = sorted(text.keys())[0]
            self.show_nic = nic
        return text[nic]