# Copyright (C) 2004 Nicolas Delon # All Rights Reserved # # This file is part of the Prelude program. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. import os import time def time_to_string(t): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t)) # term codes from http://www.demonseed.net/~jp/code/ansi.py _FOREGROUND_COLORS = { 'black': '\033[30m', 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[3;34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m' } _BACKGROUND_COLORS = { 'black': '\033[40m', 'red': '\033[41m', 'green': '\033[42m', 'yellow': '\033[43m', 'blue': '\033[44m', 'magenta': '\033[45m', 'cyan': '\033[46m', 'white': '\033[47m' } _RESET = '\033[0;0m' _BOLD = '\033[1m' _REVERSE = '\033[2m' def stylize(content, foreground=None, background=None, bold=False, reverse=False): if not os.isatty(1): return content output = "" if foreground: output += _FOREGROUND_COLORS[foreground] if background: output += _BACKGROUND_COLORS[background] if bold: output += _BOLD if reverse: output += _REVERSE output += content if foreground or background or bold: output += _RESET return output