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 * ...@@ -16,6 +16,7 @@ from PrettyPrinter import *
from PriorityCommand import PriorityCommand from PriorityCommand import PriorityCommand
import TodoFile import TodoFile
import TodoList import TodoList
from Utils import escape_ansi
def usage(): def usage():
""" Prints the usage of the todo.txt CLI """ """ Prints the usage of the todo.txt CLI """
...@@ -36,6 +37,17 @@ def arguments(p_start=2): ...@@ -36,6 +37,17 @@ def arguments(p_start=2):
return values 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): class CLIApplication(object):
def __init__(self): def __init__(self):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
...@@ -57,7 +69,6 @@ class CLIApplication(object): ...@@ -57,7 +69,6 @@ class CLIApplication(object):
if archive.is_dirty(): if archive.is_dirty():
archive_file.write(str(archive)) archive_file.write(str(archive))
def run(self): def run(self):
""" Main entry function. """ """ Main entry function. """
todofile = TodoFile.TodoFile(Config.FILENAME) todofile = TodoFile.TodoFile(Config.FILENAME)
...@@ -92,8 +103,8 @@ class CLIApplication(object): ...@@ -92,8 +103,8 @@ class CLIApplication(object):
args = arguments(1) args = arguments(1)
command = subcommand_map[subcommand](args, self.todolist, command = subcommand_map[subcommand](args, self.todolist,
lambda o: sys.stdout.write(o + "\n"), lambda o: write(sys.stdout, o + "\n"),
lambda e: sys.stderr.write(e + "\n"), lambda e: write(sys.stderr, e + "\n"),
raw_input) raw_input)
if command.execute() == False: if command.execute() == False:
......
...@@ -40,3 +40,8 @@ def convert_todo_number(p_number): ...@@ -40,3 +40,8 @@ def convert_todo_number(p_number):
def is_valid_priority(p_priority): def is_valid_priority(p_priority):
return p_priority != None and re.match(r'^[A-Z]$', p_priority) != None 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 import unittest
from Utils import escape_ansi
class CommandTest(unittest.TestCase): class CommandTest(unittest.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(CommandTest, self).__init__(*args, **kwargs) super(CommandTest, self).__init__(*args, **kwargs)
...@@ -7,7 +9,7 @@ class CommandTest(unittest.TestCase): ...@@ -7,7 +9,7 @@ class CommandTest(unittest.TestCase):
self.errors = "" self.errors = ""
def out(self, p_output): def out(self, p_output):
self.output += p_output + "\n"; self.output += escape_ansi(p_output + "\n")
def error(self, p_error): 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