comparison fontaine/console.py @ 4:9053902c750e

Add a console renderer and a command line utility to use it.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 03 Jan 2017 09:06:05 -0800
parents
children e3d52edde00b
comparison
equal deleted inserted replaced
3:6019eee799bf 4:9053902c750e
1 import os
2 import sys
3 import colorama
4 from .renderer import BaseDocumentRenderer, BaseTextRenderer
5
6
7 def _w(style, text, reset_all=False):
8 f = sys.stdout.write
9 f(style)
10 f(text)
11 if not reset_all:
12 f(colorama.Style.NORMAL)
13 else:
14 f(colorama.Style.RESET_ALL)
15 f(os.linesep)
16
17
18 class ConsoleDocumentRenderer(BaseDocumentRenderer):
19 def __init__(self, width=80):
20 super().__init__(ConsoleTextRenderer())
21 self.width = width
22 colorama.init()
23
24 def write_title_heading(self, text):
25 print("")
26 for line in text.split('\n'):
27 print(line.center(self.width))
28 print("")
29 print("")
30
31 def write_title_footer(self, text):
32 _w(colorama.Style.DIM, text)
33 print("")
34 _w(colorama.Style.DIM, 80 * '=')
35
36 def write_scene_heading(self, text):
37 print("")
38 _w(colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True)
39
40 def write_action(self, text):
41 print(text)
42
43 def write_centeredaction(self, text):
44 print("")
45 for line in text.split('\n'):
46 print(line.center(self.width))
47
48 def write_character(self, text):
49 print("")
50 _w(colorama.Fore.WHITE, "\t\t\t" + text, True)
51
52 def write_dialog(self, text):
53 for line in text.split('\n'):
54 print("\t" + line)
55
56 def write_parenthetical(self, text):
57 for line in text.split('\n'):
58 print("\t\t" + line)
59
60 def write_transition(self, text):
61 print("")
62 print("\t\t\t\t" + text)
63
64 def write_lyrics(self, text):
65 print("")
66 _w(colorama.Fore.MAGENTA, text, True)
67
68 def write_pagebreak(self):
69 print("")
70 _w(colorama.Style.DIM, 80 * '=')
71
72
73 class ConsoleTextRenderer(BaseTextRenderer):
74 def _writeStyled(self, style, text):
75 return style + text + colorama.Style.NORMAL
76
77 def make_italics(self, text):
78 return self._writeStyled(colorama.Style.BRIGHT, text)
79
80 def make_bold(self, text):
81 return self._writeStyled(colorama.Style.BRIGHT, text)
82
83 def make_underline(self, text):
84 return self._writeStyled(colorama.Style.BRIGHT, text)