summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurie Clark-Michalek <bluepeppers@archlinux.us>2010-11-22 15:08:55 +0000
committerLaurie Clark-Michalek <bluepeppers@archlinux.us>2010-11-22 15:08:55 +0000
commite9e54dcfea7b370642acb6503e31b8695ab81e9a (patch)
tree4a27bb33d1387cb47c2c13c2dc5426a096bb306d
parentbfb4574c23208a536e9f879006fd320d96483840 (diff)
Added color to fs display function, and added a _color function for BaseDisplay class.
-rw-r--r--archey.new31
1 files changed, 28 insertions, 3 deletions
diff --git a/archey.new b/archey.new
index edae67e..e0ae9f5 100644
--- a/archey.new
+++ b/archey.new
@@ -14,7 +14,13 @@
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
# Import libraries
-import os, sys, subprocess, optparse, re, linecache
+import os
+import sys
+import subprocess
+import optparse
+import re
+import linecache
+import collections
from optparse import OptionParser
from getpass import getuser
from time import ctime, sleep
@@ -215,7 +221,12 @@ class Output(list):
>>>out._get_results()
{'Foo': 'Bar'}
"""
- return {key:value for key, value in [disp.key_value_pair() for disp in self] if key and value}
+ od = collections.OrderedDict()
+ for key, value in (disp.key_value_pair() for disp in self):
+ if not (key and value):
+ continue
+ od[key] = value
+ return od
def align_results(self, results, maxsize=17):
alignment_builtins = ['top', 'bottom', 'center', 'custom']
@@ -283,6 +294,14 @@ class BaseDisplay():
def __repr__(self):
return '<{0}: key={1}, value={2}>'.format(self.__class__.__name__, self.key, self.value)
+ def _color(self, color, bold=False):
+ """
+ Returns a shell escape code for the color given as a string.
+ """
+ colors = dict(zip(('BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAGENTA', 'CYAN', 'WHITE'), range(8)))
+ p = colors.get(color.upper(), None)
+ return '\x1b[{0};3{1}m'.format(int(bold), p) if p else '\x1b[0m'
+
def get_key(self):
"""
Return the value of the class' key attribute. If classes wish to customise key generation,
@@ -477,8 +496,14 @@ class FileSystem(BaseDisplay):
used = line[3]
total = line[2]
fstype = line[1]
+ if persentage < (1/3) * 100:
+ color = self._color('green', bold=True)
+ elif persentage < (2/3) * 100:
+ color = self._color('blue', bold=True)
+ else:
+ color = self._color('red', bold=True)
- return '{used}/{total} ({fstype})'.format(used=used, total=total, fstype=fstype)
+ return '{color}{used}{clear}/{total} ({fstype})'.format(color=color, used=used, clear=self._color('clear'), total=total, fstype=fstype)
#class CPU():
# def __init__(self):