#!/usr/bin/env python2.6
# coding: utf-8


import types


def line_pad(linestr, padding=''):

    lines = linestr.split("\n")

    if type(padding) in types.StringTypes:
        lines = [padding + x for x in lines]

    elif callable(padding):
        lines = [padding(x) + x for x in lines]

    lines = "\n".join(lines)

    return lines


def format_line(items, sep=' ', aligns=''):

    listtype = (type([]), type(()))

    aligns = [x for x in aligns] + [''] * len(items)
    aligns = aligns[:len(items)]
    aligns = ['r' if x == 'r' else x for x in aligns]

    items = [(x if type(x) in listtype else [x])
             for x in items]

    items = [[str(y) for y in x]
             for x in items]

    maxHeight = max([len(x) for x in items])

    max_width = lambda x: max([len(y) for y in x])
    widths = [max_width(x) for x in items]

    items = [(x + [''] * maxHeight)[:maxHeight]
             for x in items]

    lines = []
    for i in range(maxHeight):
        line = []
        for j in range(len(items)):
            width = widths[j]
            elt = items[j][i]
            if aligns[j] == 'l':
                elt = elt.ljust(width)
            else:
                elt = elt.rjust(width)
            line.append(elt)
        line = sep.join(line)

        lines.append(line)

    return "\n".join(lines)

@@ -655,14 +655,67 @@
 = %5B%5B
-str(y)
+y if isinstance(y, ColoredString) else str(y)%0A             
  for
@@ -834,14 +834,50 @@
 ax(%5B
-len(y)
+y.__len__()%0A                              
  for
@@ -882,24 +882,25 @@
 or y in x%5D)%0A
+%0A
     widths =
@@ -1169,16 +1169,179 @@
 s%5Bj%5D%5Bi%5D%0A
+%0A            actualWidth = elt.__len__()%0A            elt = str(elt)%0A%0A            if actualWidth %3C width:%0A                padding = ' ' * (width - actualWidth)%0A    
         
@@ -1373,32 +1373,36 @@
                 
+  
+  
 elt = elt.ljust(
@@ -1398,22 +1398,23 @@
  elt
-.ljust(width)%0A
+ + padding%0A    
     
@@ -1447,30 +1447,32 @@
     
+ 
+   
 elt = 
-elt.rjust(width)
+padding + elt%0A
 %0A   
@@ -1497,16 +1497,17 @@
 nd(elt)%0A
+%0A
         
@@ -1553,24 +1553,24 @@
 pend(line)%0A%0A
-
     return %22
@@ -1569,24 +1569,1170 @@
 return %22%5Cn%22.join(lines)%0A
+%0A%0Adef colorize(v, total, ptn='%7B0%7D'):%0A    if total %3E 0:%0A        color = fading_color(v, total)%0A    else:%0A        color = fading_color(-total - v, -total)%0A    return ColoredString(ptn.format(v), color)%0A%0A%0Aclass ColoredString(object):%0A%0A    def __init__(self, v, color):%0A        self.v = v%0A        if type(color) == type(''):%0A            color = _named_colors%5Bcolor%5D%0A        self.color = color%0A%0A    def __str__(self):%0A        return '%5C033%5B38;5;' + str(self.color) + 'm' + str(self.v) + '%5C033%5B0m'%0A%0A    def __len__(self):%0A        return len(str(self.v))%0A%0A%0Adef fading_color(v, total):%0A    return _clrs%5Bfading_idx(v, total)%5D%0A%0A%0Adef fading_idx(v, total=100):%0A    l = len(_clrs)%0A    pos = int(v * l / total + 0.5)%0A    pos = min(pos, l - 1)%0A    pos = max(pos, 0)%0A    return pos%0A%0A%0A_clrs = %5B63, 67, 37, 36, 41, 46, 82, 118,%0A         154, 190, 226, 220, 214, 208, 202, 196%5D%0A_named_colors = %7B%0A    'danger': _clrs%5Bfading_idx(100)%5D,%0A    'warn': _clrs%5Bfading_idx(60)%5D,%0A    'loaded': _clrs%5Bfading_idx(30)%5D,%0A    'optimal': _clrs%5Bfading_idx(0)%5D,%0A    'dark': _clrs%5B1%5D,%0A%7D%0A%0A%0Aif __name__ == %22__main__%22:%0A    for i in range(len(_clrs)):%0A        print colorize(i, len(_clrs))%0A

add colorizing support