Skip to content

Commit

Permalink
fix readline colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Grudko committed Nov 20, 2012
1 parent 6bada74 commit 25e700b
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions n3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import termios
import signal
import pexpect
from termcolor import colored
import termcolor
from ConfigParser import ConfigParser
from optparse import OptionParser
from datetime import datetime
Expand Down Expand Up @@ -66,8 +66,8 @@ def stage_colored(self, stage):
stage_name = self.stage_name(stage)
if stage_name is not None:
return "%s %s" % (
colored(stage, 'green'),
colored(stage_name, 'white'))
readline_colored(stage, 'green'),
readline_colored(stage_name, 'white'))

def update_prompt(self):
self.prompt = "stage | cur: %s | next: %s > " % (
Expand All @@ -79,9 +79,13 @@ def cmdloop(self, intro=None, options=None):
return cmd.Cmd.cmdloop(self, intro)

def sigwinch_passthrough(self, sig, data):
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912
s = struct.pack("HHHH", 0, 0, 0, 0)
a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(),
termios.TIOCGWINSZ, s))
TIOCGWINSZ, s))
self.p.setwinsize(a[0], a[1])

def pexpect_filter(self, line):
Expand Down Expand Up @@ -265,7 +269,9 @@ def write(self, lines):
if self.partline[-1] == '\n':
self.logger.log(self.level, self.partline.strip())
if self.partline[-1] in ('\r', '\n'):
self.lastline = self.partline
self.partline = ''
self.logger.log(self.level, self.lastline.strip())

def flush(self):
pass
Expand All @@ -285,7 +291,7 @@ class EnvFIFO(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
self.fifo_name=os.path.join(os.environ.get('HOME'), 'deploy.cmd')
self.fifo_name = os.path.join(os.environ.get('HOME'), 'deploy.cmd')
if os.path.exists(self.fifo_name):
os.unlink(self.fifo_name)
os.mkfifo(self.fifo_name)
Expand Down Expand Up @@ -314,6 +320,23 @@ def close(self):
os.unlink(self.fifo_name)


def readline_colored(text, color=None, on_color=None, attrs=None):
if os.getenv('ANSI_COLORS_DISABLED') is None:
fmt_str = '\001\033[%dm\002%s'
if color is not None:
text = fmt_str % (termcolor.COLORS[color], text)

if on_color is not None:
text = fmt_str % (termcolor.HIGHLIGHTS[on_color], text)

if attrs is not None:
for attr in attrs:
text = fmt_str % (termcolor.ATTRIBUTES[attr], text)

text += '\001\033[0m\002'
return text


class ColoredFormatter(logging.Formatter):

colors = {
Expand All @@ -327,7 +350,7 @@ class ColoredFormatter(logging.Formatter):
def format(self, record):
result = logging.Formatter.format(self, record)
if result is not None:
return colored(result, self.colors[record.levelname])
return termcolor.colored(result, self.colors[record.levelname])


def main():
Expand Down

0 comments on commit 25e700b

Please sign in to comment.