aboutsummaryrefslogblamecommitdiffstats
path: root/xpybar/config/mymemory.py
blob: e0eb76b92ff02520e57c9bae646b137bd4777e95 (plain) (tree)




















































































































                                                                                                  
# -*- python -*-
from plugins.mem   import Memory
from plugins.swaps import Swaps

from common import *

class MyMemory(Entry):
    def __init__(self, *args, **kwargs):
        self.coloured = tuple(self.colourise(i) for i in range(101))
        self.show_labels = True
        self.show_value = -1
        self.show_default = False
        self.show_swaps = False
        self.show_swap = 0
        self.labels = list(sorted(Memory().keys))
        swaps = Swaps()
        self.prio = swaps.header_map['Priority']
        self.used = swaps.header_map['Used']
        self.size = swaps.header_map['Size']
        self.swap = swaps.header_map['Filename']
        Entry.__init__(self, *args, **kwargs)
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            if self.show_value >= 0:
                self.show_default = not self.show_default
                self.invalidate()
        elif button == MIDDLE_BUTTON:
            self.show_labels = not self.show_labels
            self.invalidate()
        elif button == RIGHT_BUTTON:
            self.show_swaps = not self.show_swaps
            self.invalidate()
        elif button == SCROLL_UP:
            if self.show_swaps:
                self.show_swap += 1
                self.invalidate()
            else:
                n = self.show_value + 1
                if n < len(self.labels):
                    self.show_value = n
                    self.invalidate()
        elif button == SCROLL_DOWN:
            if self.show_swaps:
                n = self.show_swap
                if n > 0:
                    self.show_swap = n - 1
                    self.invalidate()
            else:
                self.show_value -= 1
                if self.show_value < -1:
                    self.show_value = -1
                    self.invalidate()
    
    def u(self, value):
        units = 'KMGTPE'
        unit = 0
        while unit + 1 < len(units) and value >= 1024:
            unit += 1
            value /= 1024
        return '%.1f%s' % (value, units[unit])
    
    def colourise(self, value):
        if value >= 100:
            return '\033[31m100\033[0m'
        colour = '39'
        if value > 30:  colour = '32'
        if value > 50:  colour = '33'
        if value > 80:  colour = '31'
        return '\033[%sm%i\033[0m%%' % (colour, value)
    
    def function(self):
        if self.show_swaps:
            swaps = Swaps()
            while True:
                if len(swaps.swaps) == 0:
                    return 'no swaps available'
                try:
                    swap = swaps.swaps[self.show_swap]
                    used, size = int(swap[self.used]), int(swap[self.size])
                    swap, prio = swap[self.swap], int(swap[self.prio])
                    return '%s: %s (%sB/%sB, prio %i)' % (swap, self.colourise(used * 100 / size),
                                                          self.u(used), self.u(size), prio)
                except:
                    self.show_swap -= 1
                    if self.show_swap < 0:
                        self.show_swaps = False
                        return self.function()
        else:
            memory = Memory()
            if not self.show_default and self.show_value > 0:
                label = self.labels[self.show_value]
                try:
                    value = memory[label]
                    unit = 0
                    units = ['K', 'M', 'G', 'T', 'P', 'E']
                    while (unit + 1 < len(units)) and (value >= 1024):
                        value /= 1024
                        unit += 1
                    return '%s: %.1f%sB' % (label, value, units[unit])
                except:
                    self.show_value = -1
            if memory.mem_total == 0:
                mem = '---'
                shm = '---'
            else:
                mem = self.coloured[limited(memory.mem_used * 100 / memory.mem_total)]
                shm = self.coloured[limited(memory.shmem * 100 / memory.mem_total)]
            if memory.swap_total == 0:
                swp = 'n/a'
            else:
                swp = self.coloured[limited(memory.swap_used * 100 / memory.swap_total)]
            if self.show_labels:
                mem = 'Mem: %s%sSwp: %s%sShm: %s' % (mem, SEPARATOR, swp, SEPARATOR, shm)
            else:
                mem = 'Mem: %s %s %s' % (mem, swp, shm)
            return mem