Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
textui.clean for stripping colored cli output
Browse files Browse the repository at this point in the history
  • Loading branch information
star:Kenneth Reitz committed Mar 22, 2011
1 parent 2cd9bd5 commit 8b6eb54
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions clint/textui/colored.py
Expand Up @@ -11,11 +11,14 @@

from __future__ import absolute_import

from ..packages import colorama
import re
import sys

from ..packages import colorama


colorama.init(autoreset=True)
if sys.stdout.isatty():
colorama.init(autoreset=True)


__all__ = (
Expand All @@ -30,9 +33,16 @@ def __init__(self, color, s):
super(ColoredString, self).__init__()
self.s = s
self.color = color
self.color_str = '%s%s%s' % (
getattr(colorama.Fore, self.color), self.s, colorama.Fore.RESET)


@property
def color_str(self):
if sys.stdout.isatty():
return '%s%s%s' % (
getattr(colorama.Fore, self.color), self.s, colorama.Fore.RESET)
else:
return self.s


def __len__(self):
return len(self.s)

Expand All @@ -46,17 +56,34 @@ def __unicode__(self):
return self.color_str

def __add__(self, other):
return str(self.color_str) + str(other)
self.s += other
return self

def __radd__(self, other):
return str(other) + str(self.color_str)
self.s = other + self.s
return self

def __mul__(self, other):
return (self.color_str * other)

def split(self, x=' '):
return self.color_str.split(x)

# print map(self._new, self.s.split(x))
return map(self._new, self.s.split(x))

def _new(self, s):
return ColoredString(self.color, s)



def clean(s):
strip = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")
txt = strip.sub('', str(s))

strip = re.compile(r'\[\d+m')
txt = strip.sub('', txt)

return txt


def black(string):
Expand Down

0 comments on commit 8b6eb54

Please sign in to comment.