Mercurial > jouvence
annotate fontaine/console.py @ 7:e3d52edde00b
Make renderers write to a provided output stream.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 04 Jan 2017 01:21:55 -0800 |
parents | 9053902c750e |
children | 02d2e4d8b0c1 |
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 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
23 def write_title_heading(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
24 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
|
25 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
|
26 print(line.center(self.width), file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
27 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
28 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
|
29 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
30 def write_title_footer(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
31 _w(out, colorama.Style.DIM, text) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
32 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
33 _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
|
34 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
35 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
|
36 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
37 _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
|
38 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
39 def write_action(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
40 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
|
41 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
42 def write_centeredaction(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
47 def write_character(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
48 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
49 _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
|
50 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
59 def write_transition(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
60 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
61 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
|
62 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
63 def write_lyrics(self, text, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
64 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
65 _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
|
66 |
7
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
67 def write_pagebreak(self, out): |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
68 print("", file=out) |
e3d52edde00b
Make renderers write to a provided output stream.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
69 _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
|
70 |
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 class ConsoleTextRenderer(BaseTextRenderer): |
9053902c750e
Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 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
|
74 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
|
75 |
9053902c750e
Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 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
|
77 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
|
78 |
9053902c750e
Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 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
|
80 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
|
81 |
9053902c750e
Add a console renderer and a command line utility to use it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 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
|
83 return self._writeStyled(colorama.Style.BRIGHT, text) |