Mercurial > jouvence
comparison 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 |
comparison
equal
deleted
inserted
replaced
6:486c2349598e | 7:e3d52edde00b |
---|---|
1 import os | 1 import os |
2 import sys | |
3 import colorama | 2 import colorama |
4 from .renderer import BaseDocumentRenderer, BaseTextRenderer | 3 from .renderer import BaseDocumentRenderer, BaseTextRenderer |
5 | 4 |
6 | 5 |
7 def _w(style, text, reset_all=False): | 6 def _w(out, style, text, reset_all=False): |
8 f = sys.stdout.write | 7 f = out.write |
9 f(style) | 8 f(style) |
10 f(text) | 9 f(text) |
11 if not reset_all: | 10 if not reset_all: |
12 f(colorama.Style.NORMAL) | 11 f(colorama.Style.NORMAL) |
13 else: | 12 else: |
19 def __init__(self, width=80): | 18 def __init__(self, width=80): |
20 super().__init__(ConsoleTextRenderer()) | 19 super().__init__(ConsoleTextRenderer()) |
21 self.width = width | 20 self.width = width |
22 colorama.init() | 21 colorama.init() |
23 | 22 |
24 def write_title_heading(self, text): | 23 def write_title_heading(self, text, out): |
25 print("") | 24 print("", file=out) |
26 for line in text.split('\n'): | 25 for line in text.split('\n'): |
27 print(line.center(self.width)) | 26 print(line.center(self.width), file=out) |
28 print("") | 27 print("", file=out) |
29 print("") | 28 print("", file=out) |
30 | 29 |
31 def write_title_footer(self, text): | 30 def write_title_footer(self, text, out): |
32 _w(colorama.Style.DIM, text) | 31 _w(out, colorama.Style.DIM, text) |
33 print("") | 32 print("", file=out) |
34 _w(colorama.Style.DIM, 80 * '=') | 33 _w(out, colorama.Style.DIM, 80 * '=') |
35 | 34 |
36 def write_scene_heading(self, text): | 35 def write_scene_heading(self, text, out): |
37 print("") | 36 print("", file=out) |
38 _w(colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True) | 37 _w(out, colorama.Fore.WHITE + colorama.Style.BRIGHT, text, True) |
39 | 38 |
40 def write_action(self, text): | 39 def write_action(self, text, out): |
41 print(text) | 40 print(text, file=out) |
42 | 41 |
43 def write_centeredaction(self, text): | 42 def write_centeredaction(self, text, out): |
44 print("") | 43 print("", file=out) |
45 for line in text.split('\n'): | 44 for line in text.split('\n'): |
46 print(line.center(self.width)) | 45 print(line.center(self.width), file=out) |
47 | 46 |
48 def write_character(self, text): | 47 def write_character(self, text, out): |
49 print("") | 48 print("", file=out) |
50 _w(colorama.Fore.WHITE, "\t\t\t" + text, True) | 49 _w(out, colorama.Fore.WHITE, "\t\t\t" + text, True) |
51 | 50 |
52 def write_dialog(self, text): | 51 def write_dialog(self, text, out): |
53 for line in text.split('\n'): | 52 for line in text.split('\n'): |
54 print("\t" + line) | 53 print("\t" + line, file=out) |
55 | 54 |
56 def write_parenthetical(self, text): | 55 def write_parenthetical(self, text, out): |
57 for line in text.split('\n'): | 56 for line in text.split('\n'): |
58 print("\t\t" + line) | 57 print("\t\t" + line, file=out) |
59 | 58 |
60 def write_transition(self, text): | 59 def write_transition(self, text, out): |
61 print("") | 60 print("", file=out) |
62 print("\t\t\t\t" + text) | 61 print("\t\t\t\t" + text, file=out) |
63 | 62 |
64 def write_lyrics(self, text): | 63 def write_lyrics(self, text, out): |
65 print("") | 64 print("", file=out) |
66 _w(colorama.Fore.MAGENTA, text, True) | 65 _w(out, colorama.Fore.MAGENTA, text, True) |
67 | 66 |
68 def write_pagebreak(self): | 67 def write_pagebreak(self, out): |
69 print("") | 68 print("", file=out) |
70 _w(colorama.Style.DIM, 80 * '=') | 69 _w(out, colorama.Style.DIM, 80 * '=') |
71 | 70 |
72 | 71 |
73 class ConsoleTextRenderer(BaseTextRenderer): | 72 class ConsoleTextRenderer(BaseTextRenderer): |
74 def _writeStyled(self, style, text): | 73 def _writeStyled(self, style, text): |
75 return style + text + colorama.Style.NORMAL | 74 return style + text + colorama.Style.NORMAL |