aboutsummaryrefslogtreecommitdiff
path: root/2018-linuxdays/pres.tex
blob: 71739f12eff4134da575eb817fac1e7a53fc3bf7 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
\documentclass[aspectratio=169]{beamer}
\usetheme{metropolis}
\usepackage{lmodern}
\usepackage[czech]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{color}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{epstopdf}
\usepackage{amsmath}
\usepackage{minted}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\usepackage{pdflscape}

\title{Jak se píše doplňování v shellu?!}
\author{Kar(t)el Kočí}
\date{7.10.2018}

\begin{document}

\frame{\titlepage}

% nejaky uvodni slide!

\begin{frame}[fragile]
	\frametitle{Příkladový program}
	\begin{verbatim}
$ sterm -h
sterm: invalid option -- 'h'
Usage: sterm [options] <device>
Options:
  -b <duration> send break signal
  -c        enter command mode
  -d[PULSE] make pulse on DTR
  -e        ignore '~.' escape sequence
  -n        do not switch the device to raw mode
  -r[PULSE] make pulse on RTS
  -s <baudrate>
  -v        verbose mode

\end{verbatim}
\end{frame}

\begin{frame}[fragile]
	\frametitle{bash: příkaz complete a doplňování}
	\begin{verbatim}
complete [-abcdefgjksuv] [-o comp-option] [-DE] [-A action] [-G globpat]
    [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix]
    [-S suffix] name [name …]
complete -pr [-DE] [name …]
\end{verbatim}
	\begin{minted}[frame=lines]{shell}
complete -G \*.pdf mupdf
\end{minted}
	\begin{minted}[frame=lines]{shell}
complete -W "9600 19200 38400 57600 115200" sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{bash: doplňovcí funkce, proměnné a compgen}
	\begin{verbatim}
compgen [option] [word]
\end{verbatim}

	\begin{minted}[frame=lines]{shell}
_sterm() {
	COMPREPLY+=($(compgen -W "9600 19200 38400 57600 115200" \
		-- "${COMP_WORDS[COMP_CWORD]}"))
}
complete -F _sterm sterm
\end{minted}

	\begin{itemize}
		\item COMPREPLY: výsledek doplňování
		\item COMP\_LINE: celý dosavadní vstup
		\item COMP\_WORDS: pole jednotlivých slov ze vstupu
		\item COMP\_CWORD: poslední index do COMP\_WORDS = slovo které doplňujeme
	\end{itemize}
\end{frame}

\begin{frame}[fragile]
	\frametitle{bash: \_init\_completion}
	\begin{minted}[frame=lines]{shell}
_sterm() {
	_init_completion || return
	COMPREPLY+=($(compgen -W "9600 19200 38400 57600 115200" \
		-- "$cur"))
}
complete -F _sterm sterm
\end{minted}

	Definuje
	\begin{itemize}
		\item cur: Aktuálně doplňované slovo
		\item prev: Předchozí argument
		\item words: Jako COMP\_WORDS z COMP\_LINE
		\item cword: To samé jako COMP\_CWORD ale pro words
	\end{itemize}
\end{frame}

\begin{frame}[fragile]
	\frametitle{bash: doplňování voleb}
	\begin{minted}[frame=lines]{shell}
_sterm() {
  local cur prev words cword
  _init_completion || return
  case "$prev" in
    -b|-d|-r) ;; # No completion for these
    -s) COMPREPLY=($(compgen -W "9600 19200 38400 57600 115200" \
      -- ${cur})) ;;
    *) COMPREPLY=($(compgen -W "-s -b -d -r" -- ${cur})) ;;
  esac
}
complete -o default -F _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{bash: doplňování cesty}
	\begin{minted}[frame=lines]{shell}
_sterm() {
  local cur prev words cword
  _init_completion || return
  case "$prev" in
    -b|-d|-r) ;; # No completion for these
    -s) COMPREPLY=($(compgen -W "9600 19200 38400 57600 115200" \
      -- ${cur})) ;;
    *) COMPREPLY+=($(compgen -W "-s -b -d -r" -- ${cur}))
      COMPREPLY+=($(compgen -G "/dev/tty*" -- ${cur})) ;;
  esac
}
complete -o default -F _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: compdef a \#compdef}
	\begin{minted}[frame=lines]{shell}
_sterm() {
	_values "Baudrate" "9600" "19200" "115200"
}
compdef _sterm sterm
\end{minted}

	\begin{minted}[frame=lines]{shell}
#compdef foobar
_sterm() {
	_values "Baudrate" "9600" "19200" "115200"
}
_sterm
\end{minted}

\begin{verbatim}
fpath=($(pwd) $fpath)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: doplňování cest}
	\begin{minted}[frame=lines]{shell}
_sterm() {
	_files
}
compdef _sterm sterm
\end{minted}

	\begin{minted}[frame=lines]{shell}
_sterm() {
	_values "tty" /dev/tty*
}
compdef _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: doplňování voleb}
	\begin{minted}[frame=lines]{shell}
_sterm_baudrate() {
	_values "Baudrate" "9600" "19200" "115200"
}
_sterm() {
	_arguments \
		"-s[Set baudrate]:baudrate:_sterm_baudrate" \
		"-b[Send break]" \
		"-c[Enter command mode]" \
		"-d[Make pulse on DTR]" \
		"-e[Ignore '~.' escape sequence]"
}
compdef _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: poziční argumenty}
	\begin{minted}[frame=lines]{shell}
_sterm_baudrate() {
	_values "Baudrate" "9600" "19200" "115200"
}
_sterm() {
	_arguments \
		"-s[Set baudrate]:baudrate:_sterm_baudrate" \
		"-b[Send break]" \
		"-c[Enter command mode]" \
		"-d[Make pulse on DTR]" \
		"-e[Ignore '~.' escape sequence]" \
		"1:device:_files"
}
compdef _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: argumenty a stavy}
	\begin{minted}[frame=lines]{shell}
_sterm_baudrate() {
	_values "Baudrate" "9600" "19200" "115200"
}
_sterm() {
	_arguments \
		"-s[Set baudrate]:baudrate:_sterm_baudrate" \
		"-b[Send break]:break:->ignore" \
		"-c[Enter command mode]" \
		"-d[Make pulse on DTR]:pulse:->ignore" \
		"-e[Ignore '~.' escape sequence]"
	[ "$state" = "ignore" ] || _values "tty" /dev/tty*
}
compdef _sterm sterm
\end{minted}
\end{frame}

\begin{frame}[fragile]
	\frametitle{zsh: doplňování bez námahy}
	\begin{minted}[frame=lines]{shell}
compdef _gnu_generic ct-ng
\end{minted}
\end{frame}

\section[Příklady]{}

\begin{frame}
	\frametitle{Zdroje}
	\begin{itemize}
		\item Bash: man complete (BASH\_BUILTINS)
		\item Bash: \url{https://www.gnu.org/software/bash/manual/html\_node/Programmable-Completion.html}
		\item Zsh lidsky: \url{http://zsh.sourceforge.net/Doc/Release/Completion-System.html\#Completion-System}
		\item Zsh dokumentace: \url{http://zsh.sourceforge.net/Doc/Release/Completion-System.html\#Completion-System}
	\end{itemize}
\end{frame}

\begin{frame}
	\includegraphics[width=\textwidth]{naborovy-letak-projekce-rgb-1920x1080.png}
\end{frame}

\begin{frame}
	\Large Děkuji za pozornost.

	\url{git.cynerd.cz}
\end{frame}

\end{document}