view fontaine/console.py @ 10:2cea36073188

Move core CLI tool code into the package.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Jan 2017 08:46:27 -0800
parents 02d2e4d8b0c1
children
line wrap: on
line source

import os
import colorama
from .renderer import BaseDocumentRenderer, BaseTextRenderer


def _w(out, style, text, reset_all=False):
    f = out.write
    f(style)
    f(text)
    if not reset_all:
        f(colorama.Style.NORMAL)
    else:
        f(colorama.Style.RESET_ALL)
    f(os.linesep)


class ConsoleDocumentRenderer(BaseDocumentRenderer):
    def __init__(self, width=80):
        super().__init__(ConsoleTextRenderer())
        self.width = width
        colorama.init()

    def write_title_page(self, values, out):
        known = ['title', 'credit', 'author', 'source']
        center_values = [values.get(i) for i in known
                         if i is not None]

        print("", file=out)
        for val in center_values:
            for l in val.split('\n'):
                print(l.center(self.width), file=out)
            print("", file=out)
        print("", file=out)
        print("", file=out)

        ddate = values.get('date') or values.get('draft date')
        contact = values.get('contact')
        bottom_lines = [i for i in [ddate, contact]
                        if i is not None]

        _w(out, colorama.Style.DIM, '\n\n'.join(bottom_lines))
        print("", file=out)
        _w(out, colorama.Style.DIM, 80 * '=')

    def write_scene_heading(self, text, out):
        print("", file=out)
        _w(out, colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True)

    def write_action(self, text, out):
        print(text, file=out)

    def write_centeredaction(self, text, out):
        print("", file=out)
        for line in text.split('\n'):
            print(line.center(self.width), file=out)

    def write_character(self, text, out):
        print("", file=out)
        _w(out, colorama.Fore.WHITE, "\t\t\t" + text, True)

    def write_dialog(self, text, out):
        for line in text.split('\n'):
            print("\t" + line, file=out)

    def write_parenthetical(self, text, out):
        for line in text.split('\n'):
            print("\t\t" + line, file=out)

    def write_transition(self, text, out):
        print("", file=out)
        print("\t\t\t\t" + text, file=out)

    def write_lyrics(self, text, out):
        print("", file=out)
        _w(out, colorama.Fore.MAGENTA, text, True)

    def write_pagebreak(self, out):
        print("", file=out)
        _w(out, colorama.Style.DIM, 80 * '=')


class ConsoleTextRenderer(BaseTextRenderer):
    def _writeStyled(self, style, text):
        return style + text + colorama.Style.NORMAL

    def make_italics(self, text):
        return self._writeStyled(colorama.Style.BRIGHT, text)

    def make_bold(self, text):
        return self._writeStyled(colorama.Style.BRIGHT, text)

    def make_underline(self, text):
        return self._writeStyled(colorama.Style.BRIGHT, text)