-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslides.py
128 lines (98 loc) · 3.54 KB
/
slides.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
"""An implementation of the slide "presentation" software used by
dbeazley in the video from the PyOhio talk found here:
https://youtu.be/j6VSAsKAj98
Great talk, by the way. Go see it!
Usage is:
>>> from slides import Slide
>>> a_text = '''# A Header
...
... Some text with **ordinary** markup _inside_
... spanning multiple lines
...
... and with paragraphs
'''
>>> a = Slide( a_text )
>>> list_of_slides = [a]
>>> i = iter( list_of_slides )
>>> next(i)
Following the invocation of the last command, the REPL will clear the
screen and print out the formatted text inside an ascii box.
"""
class Slide(object):
"""The `Slide` class works in a REPL, by first clearing the terminal,
then printing the next slide object text formatted in a box.
One thing dbeazley did was:
+--------------+
| Slide header |
| |
| slide text |
| slide text |
+--------------+
>>> reasons = ['everywhere', 'flexible', 'performance', 'fun']
>>> i = iter(reasons)
>>> next(i)
On the execution of the last statement, the screen clears and the
slide referenced by 'everywhere' shows.
Ideas:
- use the `textwrap` module to format the box
- use http://misc.flogisoft.com/bash/tip_colors_and_formatting
for formatting text to most terms
How:
- did dbeazley increase the header? I suspect that iTerm,
being a *graphical* terminal has some functionality for
this, probably along the lines sketched out in
http://superuser.com/questions/877010/increase-decrease-font-size-in-iterm2
"""
def __init__(self, heading, text):
"""
... text for slide goes here
"""
self.patterns = {'*': '\33[1m',
'_': '\33[4m',
'END': '\33[0m'}
self.heading = heading
self.text = text
self.formatted = self._format_text(text)
self.slide_text = self._draw_box()
def _format_text(self, text):
"""
takes the text as input and converts
*text* to ascii bold text
/text/ to ascii italic text
_text_ to ascii underlined text
returns the formatted text string
"""
ret_text = []
in_it = False
for c in text:
if c in self.patterns:
if in_it:
c = self.patterns.get('END')
in_it = False
else:
in_it = True
c = self.patterns.get(c)
ret_text.append(self.patterns.get(c, c))
ret_text = (''.join(ret_text)).split('\n')
return ret_text
def _split_line(self, line, width):
return [line[i:i+width] for i in range(0, len(line), width)]
def _split_msg(self, msg, width):
lines = msg
split_lines = [self._split_line(line, width-2) for line in lines]
return [item for sublist in split_lines for item in sublist]
def _draw_box(self, max_width=80):
count = len(max(self.text.split('\n'), key=len)) + 2
if count > max_width:
count = max_width
tb = "+" + "-" * count + "+"
hdr = "\n| \33[7m{0}\33[0m |\n|{1}|".format(self.heading.ljust(count-2), "".ljust(count))
msg = "\n".join("| " + x.ljust(count-2) + " |" for x in self._split_msg(self.formatted, count))
return "{0}{1}\n{2}\n{0}".format(tb, hdr, msg)
def __repr__(self):
"""
formatting out goes here
"""
os.system('clear');
return self.slide_text