diff options
Diffstat (limited to '')
-rw-r--r-- | xpybar/config/myipaddress.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/xpybar/config/myipaddress.py b/xpybar/config/myipaddress.py new file mode 100644 index 0000000..424dfac --- /dev/null +++ b/xpybar/config/myipaddress.py @@ -0,0 +1,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] |