diff options
-rw-r--r-- | archey.new | 104 |
1 files changed, 83 insertions, 21 deletions
@@ -39,24 +39,45 @@ output = [ #---------------Dictionaries---------------# -colorDict = { +class NoDeleteDict(dict): + """ + This dict silently disables deletions. This is because allthough we will want users to be able to + edit these dicts from their config files, we don't want them to muck up somthing important, so we + disable deletions to protect them from themselves. + + >>>dic = NoDeleteDict({'a':1, 'b':2}) + + We can still access items normally: + >>>dic['a'] + 1 + + But when we delete items, nothing happens: + >>>del dic['b'] + >>>dic['b'] + 2 + """ + + def __delitem__(self, name): + return + +COLOR_DICT = NoDeleteDict({ 'Arch Linux': ['\x1b[0;34m', '\x1b[1;34m'], 'Ubuntu': ['\x1b[0;31m', '\x1b[1;31m', '\x1b[0;33m'], 'Debian': ['\x1b[0;31m', '\x1b[1;31m'], 'Mint': ['\x1b[0;32m', '\x1b[1;37m'], 'Crunchbang': ['\x1b[1;37m'], 'Fedora': ['\x1b[0;34m', '\x1b[1;37m'], - 'Clear': ['\x1b[0m'] - } + 'Clear': '\x1b[0m' + }) -deDict = { +DE_DICT = NoDeleteDict({ 'gnome-session': 'GNOME', 'ksmserver': 'KDE', 'xfce4-session': 'Xfce', 'lxsession': 'LXDE' - } + }) -wmDict = { +WM_DICT = NoDeleteDict({ 'awesome': 'Awesome', 'beryl': 'Beryl', 'blackbox': 'Blackbox', @@ -79,9 +100,10 @@ wmDict = { 'wmii': 'wmii', 'xfwm4': 'Xfwm', 'xmonad': 'xmonad' - } + }) + -logosDict = {'Arch Linux': '''{color[1]} +LOGO_DICT = NoDeleteDict({'Arch Linux': '''{color[1]} {color[1]} + {results[0]} {color[1]} # {results[1]} {color[1]} ### {results[2]} @@ -100,10 +122,9 @@ logosDict = {'Arch Linux': '''{color[1]} {color[0]} ;#### ####; {results[15]} {color[0]} ##' '## {results[16]} {color[0]} #' `# {results[17]} -\x1b[0m''' -} +\x1b[0m'''}) -processes = str(subprocess.check_output(('ps', '-u', getuser(), '-o', 'comm', +PROCESSES = str(subprocess.check_output(('ps', '-u', getuser(), '-o', 'comm', '--no-headers')), encoding='utf8').rstrip('\n').split('\n') @@ -115,7 +136,6 @@ class ArcheyException(Exception): pass class Output(): results = [] - results.extend(['']*(13)) def __init__(self): self.distro = self.__detectDistro() @@ -125,12 +145,54 @@ class Output(): return 'Arch Linux' else: sys.exit("Unsupported distro") - + + def _color(self, index): + """ + Returns the escape code for either: + a) The color scheme of the distro value stored in self.distro + or + b) The value of the entry in COLOR_DICT for the key passed + + >>out = Output() + >>out._color(1) == COLOR_DICT[out.distro][1] + >>out._color(out.distro) == COLOR_DICT[out.distro] + """ + + if isinstance(index, str): + return COLOR_DICT[index] + return COLOR_DICT[self.distro][index] + + def _get_logo(self): + return LOGO_DICT[self.distro] + + def _center_results(self, results, max_length=17): + """ + Centers a list of results. Length of desired list can be given via max_length kwarg. + + >>>out = Output() + >>>out._center_results([1]) + [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 1, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + >>>out._center_results([1], max_length=3) + [' ', 1, ' '] + """ + + length = len(results) + if length > max_length: + return results[:max_length + 1] + + center = int(max_length/2) + start = int(center - length/2) + new_results = list(' ' * max_length) + new_results[start:start + length - 1] = results + + return new_results + def append(self, display): - self.results.append('%s%s: %s%s' % (colorDict[self.distro][1], display.key, colorDict['Clear'][0], display.value)) + self.results.append('%s%s: %s%s' % (self._color(1), display.key, self._color('Clear'), display.value)) def output(self): - print(logosDict[self.distro].format(color = colorDict[self.distro], results = self.results)) + results = self._center_results(self.results) + print(self._get_logo().format(color=self._color(self.distro), results=results)) class BaseDisplay(): """ @@ -183,9 +245,9 @@ class Uptime(BaseDisplay): class WindowManager(BaseDisplay): def __init__(self): wm = '' - for key in wmDict.keys(): - if key in processes: - wm = wmDict[key] + for key in WM_DICT.keys(): + if key in PROCESSES: + wm = WM_DICT[key] break self.key = 'Window Manager' @@ -194,9 +256,9 @@ class WindowManager(BaseDisplay): class DesktopEnvironment(BaseDisplay): def __init__(self): de = '' - for key in deDict.keys(): - if key in processes: - wm = wmDict[key] + for key in DE_DICT.keys(): + if key in PROCESSES: + wm = WM_DICT[key] break self.key = 'Desktop Environment' |