annotate fontaine/console.py @ 8:02d2e4d8b0c1

Make renderers responsible for formatting the title page.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Jan 2017 02:55:20 -0800
parents e3d52edde00b
children
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 colorama
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 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
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
6 def _w(out, style, text, reset_all=False):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
7 f = out.write
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 f(style)
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 f(text)
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 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
11 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
12 else:
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 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
14 f(os.linesep)
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
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 class ConsoleDocumentRenderer(BaseDocumentRenderer):
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 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
19 super().__init__(ConsoleTextRenderer())
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 self.width = width
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 colorama.init()
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
8
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
23 def write_title_page(self, values, out):
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
24 known = ['title', 'credit', 'author', 'source']
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
25 center_values = [values.get(i) for i in known
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
26 if i is not None]
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
27
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
28 print("", file=out)
8
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
29 for val in center_values:
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
30 for l in val.split('\n'):
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
31 print(l.center(self.width), file=out)
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
32 print("", file=out)
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
33 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
34 print("", file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
8
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
36 ddate = values.get('date') or values.get('draft date')
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
37 contact = values.get('contact')
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
38 bottom_lines = [i for i in [ddate, contact]
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
39 if i is not None]
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
40
02d2e4d8b0c1 Make renderers responsible for formatting the title page.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
41 _w(out, colorama.Style.DIM, '\n\n'.join(bottom_lines))
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
42 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
43 _w(out, colorama.Style.DIM, 80 * '=')
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
45 def write_scene_heading(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
46 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
47 _w(out, colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
49 def write_action(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
50 print(text, file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
52 def write_centeredaction(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
53 print("", file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 for line in text.split('\n'):
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
55 print(line.center(self.width), file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
57 def write_character(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
58 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
59 _w(out, colorama.Fore.WHITE, "\t\t\t" + text, True)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
61 def write_dialog(self, text, out):
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 for line in text.split('\n'):
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
63 print("\t" + line, file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
65 def write_parenthetical(self, text, out):
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 for line in text.split('\n'):
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
67 print("\t\t" + line, file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
69 def write_transition(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
70 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
71 print("\t\t\t\t" + text, file=out)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
73 def write_lyrics(self, text, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
74 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
75 _w(out, colorama.Fore.MAGENTA, text, True)
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
7
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
77 def write_pagebreak(self, out):
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
78 print("", file=out)
e3d52edde00b Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents: 4
diff changeset
79 _w(out, colorama.Style.DIM, 80 * '=')
4
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 class ConsoleTextRenderer(BaseTextRenderer):
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 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
84 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
85
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 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
87 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
88
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 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
90 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
91
9053902c750e Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 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
93 return self._writeStyled(colorama.Style.BRIGHT, text)