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