From c9a24f32f56dc1dc5e1eb29c593b2daac6a83286 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 18 Sep 2014 07:44:04 +0200 Subject: [PATCH] Use new utils module for colors in asciidoc2html. --- scripts/asciidoc2html.py | 16 +++++---- scripts/utils.py | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 scripts/utils.py diff --git a/scripts/asciidoc2html.py b/scripts/asciidoc2html.py index 9fc5c78fe..e43173c02 100644 --- a/scripts/asciidoc2html.py +++ b/scripts/asciidoc2html.py @@ -21,10 +21,13 @@ """Generate the html documentation based on the asciidoc files.""" import os +import sys import subprocess import glob -import colorama as col +sys.path.insert(0, os.getcwd()) + +from scripts import utils def call_asciidoc(src, dst): @@ -34,8 +37,8 @@ def call_asciidoc(src, dst): src: The source .asciidoc file. dst: The destination .html file, or None to auto-guess. """ - print("{}Calling asciidoc for {}...{}".format( - col.Fore.CYAN, os.path.basename(src), col.Fore.RESET)) + utils.print_col("Calling asciidoc for {}...".format( + os.path.basename(src)), 'cyan') if os.name == 'nt': # FIXME this is highly specific to my machine args = [r'C:\Python27\python', r'J:\bin\asciidoc-8.6.9\asciidoc.py'] @@ -47,11 +50,12 @@ def call_asciidoc(src, dst): try: subprocess.check_call(args) except subprocess.CalledProcessError as e: - print(''.join([col.Fore.RED, str(e), col.Fore.RESET])) + utils.print_col(str(e), 'red') sys.exit(1) -def main(): +def main(colors=False): + utils.use_color = colors asciidoc_files = [ ('doc/qutebrowser.1.asciidoc', None), ('README.asciidoc', None), @@ -70,4 +74,4 @@ def main(): if __name__ == '__main__': - main() + main(colors=True) diff --git a/scripts/utils.py b/scripts/utils.py new file mode 100644 index 000000000..503b7b39d --- /dev/null +++ b/scripts/utils.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014 Florian Bruhin (The Compiler) + +# This file is part of qutebrowser. +# +# qutebrowser 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 3 of the License, or +# (at your option) any later version. +# +# qutebrowser 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 qutebrowser. If not, see . + +"""Utility functions for scripts.""" + + +use_color = True + + +fg_colors = { + 'black': 30, + 'red': 31, + 'green': 32, + 'yellow': 33, + 'blue': 34, + 'magenta': 35, + 'cyan': 36, + 'white': 37, + 'reset': 39, +} + + +bg_colors = {name: col + 10 for name, col in fg_colors.items()} + + +term_attributes = { + 'bright': 1, + 'dim': 2, + 'normal': 22, + 'reset': 0, +} + + +def _esc(code): + return '\033[{}m'.format(code) + + +def print_col(text, color): + """Print a colorized text.""" + if use_color: + fg = _esc(fg_colors[color.lower()]) + reset = _esc(fg_colors['reset']) + print(''.join([fg, text, reset])) + else: + print(text) + + +def print_bold(text): + """Print a bold text.""" + if use_color: + bold = _esc(term_attributes['bright']) + reset = _esc(term_attributes['reset']) + print(''.join([bold, text, reset])) + else: + print(text)