Skip to content

Commit bba5b53

Browse files
committed
tool output nicely formatted
1 parent ed5330e commit bba5b53

File tree

9 files changed

+34
-22
lines changed

9 files changed

+34
-22
lines changed

styles/colors.sty

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
\colorlet{listing-string}{red!60!black}%
4040
\colorlet{listing-numbers}{green!60!black}%
4141
\colorlet{listing-comment}{gray!60!black}%
42+
%
43+
\colorlet{listing-tool-command}{red!50!black}%
44+
\colorlet{listing-tool-result}{blue!50!black}%
4245
%%
4346
%% Title Page Colors
4447
%%

styles/listing.sty

+14-5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ language=bash,%
123123
}%
124124
%
125125
%
126+
\lstdefinestyle{tool_style}{%
127+
style=text_style,%
128+
morecomment=[f][\color{listing-tool-command}][0]{\$ },
129+
morecomment=[f][\color{listing-tool-result}][0]{\# },
130+
breaklines=true%
131+
}%
132+
%
133+
%
126134
%% the box around the inline listings
127135
\protected\gdef\inlinelistingbox#1{\bgroup%
128136
\fboxsep=0.5pt%
@@ -199,20 +207,21 @@ language=bash,%
199207
%% #3 the command
200208
%% #4 the label (exec: will be pre-pended)
201209
%% #5 the additional parameters, if any
202-
\protected\gdef\gitOutputRaw#1#2#3#4#5{%
210+
%% #6 the style to use
211+
\protected\gdef\gitOutputWithStyle#1#2#3#4#5#6{%
203212
\gitExec{#1}{#2}{#3}%
204213
\expandafter\expandafter\expandafter\edef\expandafter\csname @pwp@gitFile:exec:#4\endcsname{\gitFile}%
205-
\lstinputlisting[style=text_style#5]{\csname @pwp@gitFile:exec:#4\endcsname}%
214+
\lstinputlisting[style=#6#5]{\csname @pwp@gitFile:exec:#4\endcsname}%
206215
}%
207216
%
208-
%% Use latexgit to place a listing with program output.
217+
%% Use latexgit to place a listing with tool output.
209218
%% #1 the git repository
210219
%% #2 the local path
211220
%% #3 the command
212221
%% #4 the label (exec: will be pre-pended)
213222
%% #5 the caption
214-
\protected\gdef\gitOutput#1#2#3#4#5{%
215-
\gitOutputRaw{#1}{#2}{#3}{#4}{,label={exec:#4},caption={#5},float}%
223+
\protected\gdef\gitOutputTool#1#2#3#4#5{%
224+
\gitOutputWithStyle{#1}{#2}{#3}{#4}{,label={exec:#4},caption={#5},float}{tool_style}%
216225
}%
217226
%
218227
%% Use latexgit to place a listing with code and the program output.

text/front/preface/preface.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ \chapter*{Preface}%
6969
%
7070
\strut\vfill\strut%
7171
This book was built using the following software:%
72-
\gitOutputRaw{}{}{scripts/dependencyVersions.sh}{versions}{}%
72+
\gitOutputWithStyle{}{}{scripts/dependencyVersions.sh}{versions}{}{text_style}%

text/main/basics/collections/lists/lists.tex

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
It does not have any \emph{error} in the strict sense.
154154
We can execute it just fine and it will produce the output \textil{[1, 2, 3]} as shown in \cref{exec:lists:lists_error}.
155155

156-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections lists_error.py}{lists:lists_error:mypy}{%
156+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections lists_error.py}{lists:lists_error:mypy}{%
157157
The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_error}.}
158158

159159
However, upon closer inspection, we discover some issues.
@@ -179,7 +179,7 @@
179179
We provide a script for using \ruff\ with a reasonable default configuration in \cref{lst:bash:ruff} on \cpageref{lst:bash:ruff}.%
180180
}%
181181
%
182-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 02_collections lists_error.py}{lists:lists_error:ruff}{%
182+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 02_collections lists_error.py}{lists:lists_error:ruff}{%
183183
The results of linting with \ruff\ of the program given in \cref{lst:lists:lists_error}. (We used the script given in \cref{lst:bash:ruff} on \cpageref{lst:bash:ruff} to apply \ruff.)}%
184184
%
185185
Let us apply \ruff\ to the program \textil{lists_error.py} given in \cref{lst:lists:lists_error}, which produces the output \cref{exec:lists:lists_error:ruff}.
@@ -220,10 +220,10 @@
220220
%
221221
\FloatBarrier%
222222
%
223-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections lists_fixed.py}{lists:lists_fixed:mypy}{%
223+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections lists_fixed.py}{lists:lists_fixed:mypy}{%
224224
The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_fixed}.}%
225225
%
226-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 02_collections lists_fixed.py}{lists:lists_fixed:ruff}{%
226+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 02_collections lists_fixed.py}{lists:lists_fixed:ruff}{%
227227
The results of static type checking with \ruff\ of the program given in \cref{lst:lists:lists_fixed}.}
228228

229229
Well, this was only a two-line program.

text/main/basics/collections/tuples/tuples.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
\gitPythonAndErrorOutput{\programmingWithPythonCodeRepo}{02_collections}{tuples_3.py}{--args format}{tuples:tuples_3}{%
1111
A third example for using tuples in \python: testing the immutability property.}%
1212
%
13-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections tuples_3.py}{tuples:tuples_3:mypy}{%
13+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 02_collections tuples_3.py}{tuples:tuples_3:mypy}{%
1414
The results of static type checking with \mypy\ of the program given in \cref{lst:tuples:tuples_3}.}%
1515
%
1616
\pythonIdx{tuple}%

text/main/basics/variables/typesAndTypeHints/typesAndTypeHints.tex

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@
8686
\label{fig:pipInstallMypy}%
8787
\end{figure}%
8888
%
89-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong.py}{variables:variable_types_wrong:mypy}{%
89+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong.py}{variables:variable_types_wrong:mypy}{%
9090
The results of static type checking with \mypy\ of the program given in \cref{lst:variables:types_wrong}. %
9191
(This is actually output generated by the script~\cref{lst:bash:mypy} on \cpageref{lst:bash:mypy}.)}%
9292
%
93-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types.py}{variables:variable_types:mypy}{%
93+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types.py}{variables:variable_types:mypy}{%
9494
The results of static type checking with \mypy\ of the program given in \cref{lst:variables:types}. %
9595
(This is actually output generated by the script~\cref{lst:bash:mypy} on \cpageref{lst:bash:mypy}.)}%
9696
%
@@ -171,10 +171,10 @@
171171
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{01_variables}{variable_types_wrong_hints_2.py}{--args format}{variables:types_wrong_hints_2}{%
172172
\cref{lst:variables:types_wrong}, but with the variable explicitly hinted as either \pythonil{int} or \pythonil{float} and named appropriately.}%
173173
%
174-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong_hints_1.py}{variables:variable_types_wrong_hints_1:mypy}{%
174+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong_hints_1.py}{variables:variable_types_wrong_hints_1:mypy}{%
175175
The results of static type checking with \mypy\ of the program given in \cref{lst:variables:types_wrong_hints_1}.}%
176176
%
177-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong_hints_2.py}{variables:variable_types_wrong_hints_2:mypy}{%
177+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_wrong_hints_2.py}{variables:variable_types_wrong_hints_2:mypy}{%
178178
The results of static type checking with \mypy\ of the program given in \cref{lst:variables:types_wrong_hints_2}.}%%
179179

180180
If the author of \cref{lst:variables:types_wrong} had used type hints, they could have written their program differently, as illustrated in \cref{lst:variables:types_wrong_hints_1,lst:variables:types_wrong_hints_2}.
@@ -207,7 +207,7 @@
207207
\gitPython{\programmingWithPythonCodeRepo}{01_variables/variable_types_hints.py}{--args format}{variables:types_hints}{%
208208
A variant of \cref{lst:variables:types} which has been improved by adding type annotations.}%
209209
%
210-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_hints.py}{variables:variable_types_hints:mypy}{%
210+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/mypy.sh 01_variables variable_types_hints.py}{variables:variable_types_hints:mypy}{%
211211
The results of static type checking with \mypy\ of the program given in \cref{lst:variables:types_hints}.}
212212

213213
For the sake of completeness, let us also annotate \cref{lst:variables:types} with type hints as a small exercise.

text/main/controlFlow/conditionals/conditionals.tex

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_elif_example.py}{--args format}{conditionals:if_elif}{%
139139
An example for using the \pythonil{if ... elif}\pythonIdx{if{\idxdots}elif{\idxdots}else} statement.}%
140140
%
141-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_nested.py}{conditionals:if_else_nested:ruff}{%
141+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_nested.py}{conditionals:if_else_nested:ruff}{%
142142
The results of linting with \ruff\ of the program given in \cref{lst:conditionals:if_else_nested}.}%
143143
%
144144
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{if_elif_nested.py}{--args format}{conditionals:if_elif_nested}{%
@@ -221,7 +221,7 @@
221221
An example for using the nested \pythonil{if ... else}\pythonIdx{if{\idxdots}elif{\idxdots}else} statements that could be inlined. %
222222
See \cref{lst:conditionals:inline_if_else} for the more compact inlined variant.}%
223223
%
224-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_could_be_inline.py}{conditionals:if_else_could_be_inline:ruff}{%
224+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/ruff.sh 03_conditionals if_else_could_be_inline.py}{conditionals:if_else_could_be_inline:ruff}{%
225225
The results of linting with \ruff\ of the program given in \cref{lst:conditionals:if_else_could_be_inline}.}%
226226
%
227227
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{03_conditionals}{inline_if_else.py}{--args format}{conditionals:inline_if_else}{%

text/main/controlFlow/functions/functions.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
\gitPython{\programmingWithPythonCodeRepo}{05_functions/test_my_math.py}{--args format}{functions:test_my_math}{%
317317
A small unit test suite for \cref{lst:functions:my_math}.}%
318318
%
319-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math.py}{functions:test_my_math:pytest}{%
319+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math.py}{functions:test_my_math:pytest}{%
320320
The output of the unit tests in \cref{lst:functions:test_my_math}: While the test of \pythonil{factorial} succeeds, our \pythonil{sqrt} function fails for input~\pythonil{0.0}.}%
321321

322322
In \cref{lst:functions:test_my_math} we do just that.
@@ -402,7 +402,7 @@
402402
\gitPython{\programmingWithPythonCodeRepo}{05_functions/test_my_math_2.py}{--args format}{functions:test_my_math_2}{%
403403
We use the same small unit test suite given in \cref{lst:functions:test_my_math} for \pythonil{sqrt} in \cref{lst:functions:my_math_2}.}%
404404
%
405-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math_2.py}{functions:test_my_math_2:pytest}{%
405+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math_2.py}{functions:test_my_math_2:pytest}{%
406406
The output of the unit tests in \cref{lst:functions:test_my_math_2}: This time, we hit the timeout because of an endless loop!}
407407

408408
In order to fix this problem, we introduce the check \pythonil{if number <= 0.0} into our \pythonil{sqrt} function in \cref{lst:functions:test_my_math_2}.
@@ -439,7 +439,7 @@
439439
\gitPython{\programmingWithPythonCodeRepo}{05_functions/test_my_math_3.py}{--args format}{functions:test_my_math_3}{%
440440
We use the same small unit test suite given in \cref{lst:functions:test_my_math} for \pythonil{sqrt} in \cref{lst:functions:my_math_3}.}%
441441
%
442-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math_3.py}{functions:test_my_math_3:pytest}{%
442+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/pytest.sh 05_functions test_my_math_3.py}{functions:test_my_math_3:pytest}{%
443443
The output of the successful unit tests in \cref{lst:functions:test_my_math_3}.}%
444444
%
445445
\begin{sloppypar}%

text/main/controlFlow/loops/loops.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330
We provide a script for using \pylint\ with a reasonable default configuration in \cref{lst:bash:pylint} on \cpageref{lst:bash:pylint}.%
331331
}%
332332
%
333-
\gitOutput{\programmingWithPythonCodeRepo}{.}{scripts/pylint.sh 04_loops for_loop_no_enumerate.py}{loops:for_loop_no_enumerate:pylint}{%
333+
\gitOutputTool{\programmingWithPythonCodeRepo}{.}{scripts/pylint.sh 04_loops for_loop_no_enumerate.py}{loops:for_loop_no_enumerate:pylint}{%
334334
The results of static code analysis using the \pylint\ \pgls{linter} for the program given in \cref{lst:loops:for_loop_no_enumerate}.}%
335335
%
336336
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{04_loops}{for_loop_enumerate.py}{--args format}{loops:for_loop_enumerate}{%

0 commit comments

Comments
 (0)