Commit ad505816 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Remove ANSI codes when not connected to a TTY.

parent 1ed16cd0
......@@ -16,6 +16,7 @@ from PrettyPrinter import *
from PriorityCommand import PriorityCommand
import TodoFile
import TodoList
from Utils import escape_ansi
def usage():
""" Prints the usage of the todo.txt CLI """
......@@ -36,6 +37,17 @@ def arguments(p_start=2):
return values
def write(p_file, p_string):
"""
Write p_string to file p_file, trailed by a newline character.
ANSI codes are removed when the file is not a TTY.
"""
if not p_file.isatty():
p_string = escape_ansi(p_string)
p_file.write(p_string + "\n")
class CLIApplication(object):
def __init__(self):
self.todolist = TodoList.TodoList([])
......@@ -57,7 +69,6 @@ class CLIApplication(object):
if archive.is_dirty():
archive_file.write(str(archive))
def run(self):
""" Main entry function. """
todofile = TodoFile.TodoFile(Config.FILENAME)
......@@ -92,8 +103,8 @@ class CLIApplication(object):
args = arguments(1)
command = subcommand_map[subcommand](args, self.todolist,
lambda o: sys.stdout.write(o + "\n"),
lambda e: sys.stderr.write(e + "\n"),
lambda o: write(sys.stdout, o + "\n"),
lambda e: write(sys.stderr, e + "\n"),
raw_input)
if command.execute() == False:
......
......@@ -40,3 +40,8 @@ def convert_todo_number(p_number):
def is_valid_priority(p_priority):
return p_priority != None and re.match(r'^[A-Z]$', p_priority) != None
def escape_ansi(p_string):
return escape_ansi.pattern.sub('', p_string)
escape_ansi.pattern = re.compile(r'\x1b[^m]*m')
import unittest
from Utils import escape_ansi
class CommandTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(CommandTest, self).__init__(*args, **kwargs)
......@@ -7,7 +9,7 @@ class CommandTest(unittest.TestCase):
self.errors = ""
def out(self, p_output):
self.output += p_output + "\n";
self.output += escape_ansi(p_output + "\n")
def error(self, p_error):
self.errors += p_error + "\n";
self.errors += escape_ansi(p_error + "\n")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment