Skip to content

Commit d08db45

Browse files
committed
normalize unicode-boxes
1 parent 3a40b2d commit d08db45

File tree

2 files changed

+216
-7
lines changed

2 files changed

+216
-7
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
extension: .md
5+
format_name: myst
6+
kernelspec:
7+
display_name: Python 3 (ipykernel)
8+
language: python
9+
name: python3
10+
language_info:
11+
name: python
12+
nbconvert_exporter: python
13+
pygments_lexer: ipython3
14+
nbhosting:
15+
title: 'TP: des boites en Unicode'
16+
---
17+
18+
# Unicode boxes on a terminal
19+
20+
+++
21+
22+
Licence CC BY-NC-ND, Thierry Parmentelat
23+
24+
+++
25+
26+
````{admonition} nothing to prune
27+
:class: warning
28+
29+
there are no difference - apart from this very cell - between the teacher and the student version, but the notebook is duplicated in .teacher for consistency
30+
````
31+
32+
+++ {"tags": ["prune-cell"]}
33+
34+
````{admonition} no need for a zip
35+
to practise this exercise, you only need this HTML text, there are no zip attached
36+
````
37+
38+
+++
39+
40+
Unicode comes with a few characters that can be used to draw squares or rectangles on the terminal
41+
42+
See e.g. this page for a list
43+
44+
<https://en.wikipedia.org/wiki/Box-drawing_character>
45+
46+
**NOTE** these examples may look a little awkward on the notebook, but will look fine when run on a terminal, provided that it uses a fixed-width font, something that any decent terminal does
47+
48+
+++
49+
50+
## a first example
51+
52+
```{code-cell} ipython3
53+
print ("wide box")
54+
print ("\u250f\u2501\u2513") # 3 odd characters
55+
print ("\u2503 \u2503") # 2 oddities + 1 space in the middle
56+
print ("\u2517\u2501\u251b") # 3 odd characters
57+
```
58+
59+
for creating this example, all I had to do was to find the codepoint for each of the characters that I need, and insert them in a Python string using the '\unnnn' notation, where `nnnn` is the 4-digit hexadecimal codepoint.
60+
61+
```{code-cell} ipython3
62+
# an example with one single character
63+
"\u250f"
64+
```
65+
66+
## adding color
67+
68+
+++
69+
70+
optionnally, like we've seen for the evaluation, we can draw color on the terminal with the `colorama` library; note that it is useful mostly for Windows terminals (as other devices has standard ways to do color).
71+
72+
```{code-cell} ipython3
73+
from colorama import Fore, Style
74+
```
75+
76+
```{code-cell} ipython3
77+
def red_text(text):
78+
return f"{Fore.RED}{text}{Style.RESET_ALL}"
79+
```
80+
81+
```{code-cell} ipython3
82+
"hello " + red_text("to the") + " world"
83+
```
84+
85+
well here again, the output is awkward on the notebook, never mind...
86+
87+
+++
88+
89+
## putting it together
90+
91+
```{code-cell} ipython3
92+
print ("thin box")
93+
print ("\u250c\u2500\u2510")
94+
print ("\u2502 \u2502")
95+
print ("\u2514\u2500\u2518")
96+
```
97+
98+
```{code-cell} ipython3
99+
print ("wide connectable box")
100+
print ("\u254b\u2501\u2513")
101+
print ("\u2503 \u2523")
102+
print ("\u2517\u2501\u251b")
103+
```
104+
105+
```{code-cell} ipython3
106+
# of course we could also do this
107+
thin_box = """\u250f\u2501\u2513
108+
\u2503 \u2503
109+
\u2517\u2501\u251b"""
110+
print(thin_box)
111+
```
112+
113+
```{code-cell} ipython3
114+
print(red_text(thin_box))
115+
```
116+
117+
```{code-cell} ipython3
118+
# or that
119+
double_box = """\u2554\u2566\u2557
120+
\u2560\u256c\u2563
121+
\u255a\u2569\u255d"""
122+
print(double_box)
123+
```
124+
125+
```{code-cell} ipython3
126+
print(red_text(double_box))
127+
```
128+
129+
well, you get the picture..
130+
131+
+++
132+
133+
## assignment
134+
135+
+++
136+
137+
### v0
138+
139+
+++
140+
141+
We want to be able to write sentences like this
142+
143+
```{code-cell} ipython3
144+
from boxes import Box0
145+
```
146+
147+
```{code-cell} ipython3
148+
b0 = Box0(8, 4)
149+
print(b0.box())
150+
```
151+
152+
### v1
153+
154+
+++
155+
156+
or even better: add style
157+
158+
```{code-cell} ipython3
159+
from boxes import Box1
160+
```
161+
162+
```{code-cell} ipython3
163+
b11 = Box1(4, 3, style='thin')
164+
print(b11.box())
165+
```
166+
167+
and then color
168+
169+
```{code-cell} ipython3
170+
b12 = Box1(10, 4, style='thin', color=Fore.BLUE)
171+
print(b12.box())
172+
```
173+
174+
### v2
175+
176+
+++
177+
178+
or better still
179+
180+
```{code-cell} ipython3
181+
from boxes import Box2
182+
```
183+
184+
```{code-cell} ipython3
185+
b21 = Box2(4, 3, style='thin')
186+
print(b21)
187+
```
188+
189+
```{code-cell} ipython3
190+
b22 = Box2(10, 4, color=Fore.BLUE, style='thin')
191+
print(b22)
192+
```
193+
194+
## assignment - if you're done
195+
196+
+++
197+
198+
a few suggestions about how to improve
199+
* accept for the width and height: either an int (like before) or a **list of integers**
200+
so we can build **grids** instead of boxes
201+
```python
202+
b = Grid(10, [2, 2])
203+
```
204+
* make it reconnectable ? needs some thinking though ;-)
205+
* etc...

notebooks/tps/unicode-boxes/README-unicode-boxes-nb.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
---
22
jupytext:
3-
cell_metadata_filter: all, -hidden, -heading_collapsed, -run_control, -trusted
4-
notebook_metadata_filter: all, -jupytext.text_representation.jupytext_version, -jupytext.text_representation.format_version,
5-
-language_info.version, -language_info.codemirror_mode.version, -language_info.codemirror_mode,
6-
-language_info.file_extension, -language_info.mimetype, -toc
73
text_representation:
84
extension: .md
95
format_name: myst
@@ -23,16 +19,24 @@ nbhosting:
2319

2420
+++
2521

26-
Unicode comes with a few characters that can be used to draw squares or rectangles on the terminal
22+
Licence CC BY-NC-ND, Thierry Parmentelat
2723

2824
+++
2925

30-
See e.g. this page for a list
26+
````{admonition} nothing to prune
27+
:class: warning
3128
32-
<https://en.wikipedia.org/wiki/Box-drawing_character>
29+
there are no difference - apart from this very cell - between the teacher and the student version, but the notebook is duplicated in .teacher for consistency
30+
````
3331

3432
+++
3533

34+
Unicode comes with a few characters that can be used to draw squares or rectangles on the terminal
35+
36+
See e.g. this page for a list
37+
38+
<https://en.wikipedia.org/wiki/Box-drawing_character>
39+
3640
**NOTE** these examples may look a little awkward on the notebook, but will look fine when run on a terminal, provided that it uses a fixed-width font, something that any decent terminal does
3741

3842
+++

0 commit comments

Comments
 (0)