vpac / src / help.v
476 lines · 463 sloc · 20.0 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import term
4
5struct HelpFlag {
6 name string
7 desc string
8}
9
10struct HelpExample {
11 cmd string
12 desc string
13}
14
15struct HelpMode {
16 key string
17 name string
18 desc string
19 usage string
20 options []HelpFlag
21 examples []HelpExample
22}
23
24const help_modes = [
25 HelpMode{
26 key: 's'
27 name: 'search'
28 desc: 'search packages in the local, sync and pkgbuild databases'
29 usage: '[options] <term...>'
30 options: [
31 HelpFlag{'-l, --local', 'search the installed packages'},
32 HelpFlag{'-r, --remote', 'search the sync databases (the default)'},
33 HelpFlag{'-b, --pkgbuild', 'search the pkgbuild repo'},
34 HelpFlag{'-f, --fuzzy', 'fuzzy matching instead of substring matching'},
35 HelpFlag{'-t, --thold <0-1>', 'fuzzy match threshold (default 0.9)'},
36 HelpFlag{'-q, --quiet', 'print package names only'},
37 HelpFlag{'-s, --sync', 'refresh the sync databases first'},
38 HelpFlag{'-F, --fields <list>', 'columns to show: all, or a comma list (see fields)'},
39 HelpFlag{'-d, --deps', 'installed packages installed as dependencies [filter]'},
40 HelpFlag{'-e, --explicit', 'installed packages installed explicitly [filter]'},
41 HelpFlag{'-m, --foreign', 'installed packages not in the sync dbs [filter]'},
42 HelpFlag{'-n, --native', 'installed packages found in the sync dbs [filter]'},
43 HelpFlag{'--unrequired', 'installed packages nothing requires [filter]'},
44 HelpFlag{'', 'twice keeps the ones only optionally required'},
45 HelpFlag{'-u, --upgrades', 'installed packages with a newer version in the sync dbs [filter]'},
46 HelpFlag{'-g, --groups', 'list the groups, or the members of the groups named'},
47 HelpFlag{'', 'with -gg every group with its members'},
48 HelpFlag{'', 'with -l the installed groups'},
49 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
50 HelpFlag{'-i, --installed', 'keep only packages that are installed locally'},
51 ]
52 examples: [
53 HelpExample{'vpac s -r linux-lts', 'search the sync databases for linux-lts'},
54 HelpExample{'vpac s -l -F name,version,size', 'installed packages with their sizes'},
55 HelpExample{'vpac s -l -e', 'installed packages installed explicitly'},
56 HelpExample{'vpac s -l -u', 'installed packages with an update available'},
57 HelpExample{'vpac s -r visual code', 'packages with both words in the name or description'},
58 HelpExample{'vpac s -l -f -t 0.8 patchelv', 'fuzzy search the installed packages'},
59 HelpExample{'vpac s -g base-devel', 'the members of a sync group'},
60 HelpExample{'vpac s -l -gg', 'every installed group with its members'},
61 ]
62 },
63 HelpMode{
64 key: 'q'
65 name: 'info'
66 desc: 'show package information, falling back to the sync databases'
67 usage: '[options] <package|file...>'
68 options: [
69 HelpFlag{'-F, --fields <list>', 'fields to show: all, or a comma list (see fields)'},
70 HelpFlag{'-c, --changelog', 'show the changelog of the package instead'},
71 HelpFlag{'-p, --file', 'the targets are package files, not installed or sync packages'},
72 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
73 ]
74 examples: [
75 HelpExample{'vpac q patchelf', 'show what is installed for patchelf'},
76 HelpExample{'vpac q -F name,version,depends bash', 'only those three fields'},
77 HelpExample{'vpac q extra/bash', 'the bash of one repo; local/bash is the installed one'},
78 HelpExample{"vpac q 'sh>=5'", 'whatever satisfies a dependency'},
79 HelpExample{'vpac q -c bash', 'the changelog bash was packaged with'},
80 HelpExample{'vpac q -p ./a.pkg.tar.zst ./b.pkg.tar.zst', 'package files on disk'},
81 ]
82 },
83 HelpMode{
84 key: 'w'
85 name: 'which'
86 desc: 'find the installed package that provides a dependency'
87 usage: '<dependency...>'
88 examples: [
89 HelpExample{'vpac w libpng.so', 'which package provides libpng.so'},
90 ]
91 },
92 HelpMode{
93 key: 'n'
94 name: 'deptest'
95 desc: 'print the dependencies no installed package satisfies, exit 127 if any'
96 usage: '<dependency...>'
97 examples: [
98 HelpExample{"vpac n 'glibc>=2.40' cowsay", 'print whichever of the two is not satisfied'},
99 ]
100 },
101 HelpMode{
102 key: 'm'
103 name: 'mark'
104 desc: 'change why an installed package is installed'
105 usage: '-r <reason> <package...>'
106 options: [
107 HelpFlag{'-r, --reason <reason>', 'explicit, depend or unknown'},
108 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
109 ]
110 examples: [
111 HelpExample{'vpac m -r depend patchelf', 'mark patchelf as installed as a dependency'},
112 ]
113 },
114 HelpMode{
115 key: 'i'
116 name: 'install'
117 desc: 'install from the repos, a local file or the pkgbuild repo (asked first)'
118 usage: '[options] <package|file...>'
119 options: [
120 HelpFlag{'--no-upgrade-deps', 'leave outdated dependencies and their dependents alone'},
121 HelpFlag{'--needed', 'skip the targets installed at the version they would install'},
122 HelpFlag{'--asdeps', 'mark the targets as installed as dependencies'},
123 HelpFlag{'--asexplicit', 'mark the targets as explicitly installed'},
124 HelpFlag{'-d, --nodeps', 'skip the dependency version checks'},
125 HelpFlag{'-dd', 'skips the dependency checks'},
126 HelpFlag{'--assume-installed <pkg=ver>', 'count a dependency as installed (repeatable, comma separated)'},
127 HelpFlag{'--overwrite <glob>', 'overwrite conflicting files that match (repeatable, comma separated)'},
128 HelpFlag{'--ignore <pkg>', 'leave a package out of this run (repeatable, comma separated)'},
129 HelpFlag{'--ignoregroup <group>', 'leave a group out of this run (repeatable, comma separated)'},
130 HelpFlag{'--dbonly', 'change the database only, not the files on disk'},
131 HelpFlag{'--disable-timeout', 'let slow downloads take their time'},
132 HelpFlag{'-b, --pkgbuild', 'build from the pkgbuild repo only, offering the PKGBUILD for review first'},
133 HelpFlag{'--nopkgbuildconfirm', 'skip the questions asked while installing from the pkgbuild repo'},
134 HelpFlag{'-w, --download-only', 'download the packages into the cache, and install nothing'},
135 HelpFlag{'-p, --print', 'list the archives the transaction would install, and install nothing'},
136 HelpFlag{'--noconfirm', 'answer yes to repo prompts, and refuse pkgbuild builds unless --nopkgbuildconfirm'},
137 ]
138 examples: [
139 HelpExample{'vpac i patchelf', 'install patchelf, upgrading its outdated dependencies'},
140 HelpExample{'vpac i ./foo-1.0-1-x86_64.pkg.tar.zst', 'install a local package file'},
141 HelpExample{'vpac i patchelf bash', 'install both in one transaction, confirmed once'},
142 HelpExample{'vpac i --needed --asdeps patchelf', 'install patchelf as a dependency, unless it is up to date'},
143 HelpExample{'vpac i extra/patchelf', 'install it from that repo and no other'},
144 HelpExample{"vpac i 'bash>=5' sh", 'install what satisfies a version, or provides a name'},
145 HelpExample{'vpac i base-devel', 'install a group: its members are listed, to pick from'},
146 ]
147 },
148 HelpMode{
149 key: 'r'
150 name: 'remove'
151 desc: 'remove an installed package'
152 usage: '[options] <package...>'
153 options: [
154 HelpFlag{'-s, --recursive', 'also remove the dependencies the removal leaves unneeded'},
155 HelpFlag{'-ss', 'the explicitly installed ones too'},
156 HelpFlag{'-u, --unneeded', 'leave the targets something still requires, instead of failing'},
157 HelpFlag{'-c, --cascade', 'also remove the packages that depend on the targets'},
158 HelpFlag{'-n, --nosave', 'remove changed config files too, instead of keeping .pacsave copies'},
159 HelpFlag{'-d, --nodeps', 'skip the dependency version checks'},
160 HelpFlag{'-dd', 'skips the dependency checks'},
161 HelpFlag{'--assume-installed <pkg=ver>', 'count a dependency as installed (repeatable, comma separated)'},
162 HelpFlag{'--dbonly', 'change the database only, not the files on disk'},
163 HelpFlag{'-p, --print', 'list the packages the removal would take, as name-version, and remove nothing'},
164 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
165 ]
166 examples: [
167 HelpExample{'vpac r patchelf', 'remove patchelf'},
168 HelpExample{'vpac r -s patchelf', 'remove patchelf and the dependencies only it needed'},
169 HelpExample{'vpac r -c libfoo', 'remove libfoo and every package that needs it'},
170 HelpExample{'vpac r -ss patchelf', 'also the dependencies that were installed explicitly'},
171 HelpExample{'vpac r -u libfoo libbar', 'remove whichever of them nothing needs'},
172 HelpExample{'vpac r xorg-apps', 'remove the installed members of a group'},
173 ]
174 },
175 HelpMode{
176 key: 'u'
177 name: 'upgrade'
178 desc: 'upgrade the whole system'
179 usage: '[options]'
180 options: [
181 HelpFlag{'-s, --sync', 'refresh the sync databases first'},
182 HelpFlag{'-w, --download-only', 'download the upgrades without installing them'},
183 HelpFlag{'-uu, --downgrade', 'also downgrade packages newer than the sync dbs carry'},
184 HelpFlag{'--ignore <pkg>', 'hold a package back this time (repeatable, comma separated)'},
185 HelpFlag{'--ignoregroup <group>', 'hold a group back this time (repeatable, comma separated)'},
186 HelpFlag{'-d, --nodeps', 'skip the dependency version checks'},
187 HelpFlag{'-dd', 'skips the dependency checks'},
188 HelpFlag{'--assume-installed <pkg=ver>', 'count a dependency as installed (repeatable, comma separated)'},
189 HelpFlag{'--overwrite <glob>', 'overwrite conflicting files that match (repeatable, comma separated)'},
190 HelpFlag{'--dbonly', 'change the database only, not the files on disk'},
191 HelpFlag{'--disable-timeout', 'let slow downloads take their time'},
192 HelpFlag{'-p, --print', 'list the archives the upgrade would install, and upgrade nothing'},
193 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
194 ]
195 examples: [
196 HelpExample{'vpac u -s', 'refresh the databases and upgrade the system'},
197 HelpExample{'vpac u --ignore linux', 'upgrade everything but linux'},
198 HelpExample{'vpac u -s -w', 'refresh, then only download what the upgrade needs'},
199 HelpExample{'vpac u -uu', 'bring packages newer than the repos back down to them'},
200 ]
201 },
202 HelpMode{
203 key: 'l'
204 name: 'files'
205 desc: 'list the files of an installed package'
206 usage: '[options] <package...>'
207 options: [
208 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
209 ]
210 examples: [
211 HelpExample{'vpac l bash', 'list the files bash installed'},
212 ]
213 },
214 HelpMode{
215 key: 'o'
216 name: 'owner'
217 desc: 'find the installed package that owns a file, or every one that owns a directory'
218 usage: '[options] <file...>'
219 options: [
220 HelpFlag{'-q, --quiet', 'print the package names only'},
221 ]
222 examples: [
223 HelpExample{'vpac o /usr/bin/ls', 'which package owns /usr/bin/ls'},
224 HelpExample{'vpac o -q /usr/bin/ls', 'only its name'},
225 HelpExample{'vpac o ls', 'a bare name the current directory lacks is looked up in PATH'},
226 ]
227 },
228 HelpMode{
229 key: 'd'
230 name: 'orphans'
231 desc: 'list packages installed as dependencies that nothing requires'
232 usage: '[options]'
233 options: [
234 HelpFlag{'-t', '-tt also lists the ones something names as an optional dependency'},
235 ]
236 examples: [
237 HelpExample{'vpac d', 'list the orphans'},
238 HelpExample{'vpac d -tt', 'and the dependencies only an optdepends line keeps'},
239 ]
240 },
241 HelpMode{
242 key: 'b'
243 name: 'build'
244 desc: 'build and install a package from an arbitrary repo'
245 usage: '[options] <repo...>'
246 options: [
247 HelpFlag{'--nopkgbuildconfirm', 'skip the build question; the review is still shown'},
248 HelpFlag{'--noconfirm', 'answer yes to the install prompts, and refuse to build unless --nopkgbuildconfirm'},
249 ]
250 examples: [
251 HelpExample{'vpac b ~/src/mything', 'build a local checkout, review included, and install it'},
252 HelpExample{'vpac b https://example.org/mything.git', 'clone the repo under the pkgbuild build dir and install'},
253 HelpExample{'vpac b -h', 'show the options of the mode'},
254 ]
255 },
256 HelpMode{
257 key: 'c'
258 name: 'clean'
259 desc: 'clean the package cache and the pkgbuild builds'
260 usage: '[options]'
261 options: [
262 HelpFlag{'-c', 'the default clean'},
263 HelpFlag{'-cc', 'removes every cached package'},
264 HelpFlag{'-a, --all', 'remove every cached file and every pkgbuild build'},
265 HelpFlag{'-k, --keep <n>', 'keep the n newest versions of every package'},
266 HelpFlag{'--keep-current', 'also keep the versions the sync dbs carry'},
267 HelpFlag{'-p, --print', 'list the files and builds the clean would remove, and remove nothing'},
268 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
269 ]
270 examples: [
271 HelpExample{'vpac c', 'remove cached packages that are not installed or are old, the builds of packages that are not installed, and the sync dbs of repos the config dropped'},
272 HelpExample{'vpac c -cc', 'remove every cached package'},
273 HelpExample{'vpac c -k 2', 'keep the two newest versions of every cached package'},
274 ]
275 },
276 HelpMode{
277 key: 'a'
278 name: 'autoremove'
279 desc: 'remove every orphan, and the dependencies that become unneeded'
280 usage: '[options]'
281 options: [
282 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
283 ]
284 examples: [
285 HelpExample{'vpac a', 'remove all orphans'},
286 ]
287 },
288 HelpMode{
289 key: 'k'
290 name: 'rank'
291 desc: 'rank mirrors by latency and download speed'
292 usage: '[options] [os]'
293 options: [
294 HelpFlag{'-n, --top <n>', 'show the n fastest mirrors (default 10)'},
295 HelpFlag{'-w, --write', 'write the ranking to <os>.lip in the mirrors dir (/etc/vpac.d/mirrors), as root if needed'},
296 HelpFlag{'-m, --mirrorlist', 'print the result as mirrorlist Server lines'},
297 HelpFlag{'-o, --output <file>', 'write the Server lines to a file, as root if needed'},
298 HelpFlag{'--stdin', 'rank the mirrors listed on stdin, instead of the os mirror list'},
299 HelpFlag{'--path <file>', 'file to time for speed, under the mirror root'},
300 ]
301 examples: [
302 HelpExample{'vpac k -n 5', 'the five fastest mirrors of this system'},
303 HelpExample{'vpac k -w', 'rank and write the config mirror file for this system'},
304 HelpExample{'vpac k -n 5 cachyos', 'the five fastest CachyOS mirrors'},
305 HelpExample{'vpac k -m -o /tmp/mirrorlist arch', 'write the fastest Arch mirrors to a file'},
306 HelpExample{'cat list.txt | vpac k --stdin', 'rank the mirrors in a list, one url per line'},
307 ]
308 },
309 HelpMode{
310 key: 't'
311 name: 'tree'
312 desc: 'show the dependency tree of a package'
313 usage: '[options] <package...>'
314 options: [
315 HelpFlag{'-r, --reverse', 'show what requires the package instead'},
316 HelpFlag{'-o, --optional', 'include optional dependencies'},
317 HelpFlag{'-d, --depth <n>', 'show n levels only (default: all)'},
318 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
319 ]
320 examples: [
321 HelpExample{'vpac t -d 2 bash', 'dependencies of bash, two levels deep'},
322 HelpExample{'vpac t -r glibc', 'packages that require glibc'},
323 ]
324 },
325 HelpMode{
326 key: 'v'
327 name: 'verify'
328 desc: 'check the files of installed packages'
329 usage: '[options] [package...]'
330 options: [
331 HelpFlag{'-k, --checksums', 'also check that backed up config files are unmodified'},
332 HelpFlag{'-kk, --full', "check every file against the package's mtree: type, owner, mode, link, time, size, sha256"},
333 HelpFlag{'-D, --db', 'check the local db instead: its files, dependencies, conflicts, shared files'},
334 HelpFlag{'-DD', "check that every sync package's dependencies resolve"},
335 ]
336 examples: [
337 HelpExample{'vpac v', 'check every installed package'},
338 HelpExample{'vpac v glibc', 'check one package'},
339 HelpExample{'vpac v -k', 'also check the backup files'},
340 HelpExample{'vpac v -kk glibc', 'check every file of glibc; run as root to read root-only files'},
341 HelpExample{'vpac v -D', 'check the local package database for consistency'},
342 ]
343 },
344 HelpMode{
345 key: 'f'
346 name: 'find'
347 desc: 'find the sync package that provides a file, from the file dbs'
348 usage: '[options] <path...> | -l <package...>'
349 options: [
350 HelpFlag{'-l, --list', 'list the files of a sync package instead'},
351 HelpFlag{'-y, --refresh', 'refresh the file dbs first (as root)'},
352 HelpFlag{'-r, --repos <list>', 'comma separated repos to search (default: all)'},
353 HelpFlag{'-q, --quiet', 'repo/pkg for paths, file paths for -l'},
354 HelpFlag{'-x, --regex', 'the targets are regular expressions: with a slash matched against paths, without against file names'},
355 HelpFlag{'--machinereadable', 'one line a file, its repo, package, version and path parted by NUL bytes'},
356 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
357 ]
358 examples: [
359 HelpExample{'vpac f /usr/bin/vlc', 'which sync package provides /usr/bin/vlc'},
360 HelpExample{'vpac f -l dolphin', 'the files of an uninstalled package'},
361 HelpExample{"vpac f -x '^usr/bin/vl.*'", 'every package with a file the expression matches'},
362 HelpExample{'vpac f -y', 'refresh the file dbs first'},
363 ]
364 },
365]
366
367// find_mode matches a mode by its short key or its name
368fn find_mode(name string) ?HelpMode {
369 for m in help_modes {
370 if m.key == name || m.name == name {
371 return m
372 }
373 }
374 return none
375}
376
377// show_help prints a help text, or sends it as a help event
378fn show_help(text string) {
379 if ipc_on() {
380 emit(HelpEvent{
381 text: text
382 })
383 return
384 }
385 println(text)
386}
387
388// overview_examples are the overview's examples: some of the modes' own,
389// named by their command, so each is written once, in its mode
390const overview_examples = ['vpac s -r linux-lts', 'vpac i patchelf', 'vpac c -k 2', 'vpac t -d 2 bash']
391
392// overview_example_rows finds the overview's examples in the modes
393fn overview_example_rows() []Row {
394 mut rows := []Row{}
395 for cmd in overview_examples {
396 for m in help_modes {
397 for e in m.examples {
398 if e.cmd == cmd {
399 rows << row(e.cmd, e.desc)
400 }
401 }
402 }
403 }
404 return rows
405}
406
407fn help_text() string {
408 mut modes := []Row{}
409 for m in help_modes {
410 modes << row(heading(m.key), m.name, m.desc)
411 }
412 examples := overview_example_rows()
413
414 mut out := []string{}
415 out << bold_heading('usage') + ' vpac [flags] <mode> [options] [args]'
416 out << ' --config <file> read this config instead of ${config_path}'
417 out << ' --eval <blip> run blip after the config, extending it for this run'
418 out << ' --color <when> auto, always or never: auto styles a terminal only'
419 out << ' --ui <kind> tty, or ipc: json lines on stdout, answers on stdin'
420 out << ' --verbose say the housekeeping failures a run otherwise goes on from'
421 out << ' vpac <mode> -h show the options of a mode'
422 out << ''
423 out << bold_heading('modes')
424 out << table_text(modes, ' ', ' ')
425 out << ''
426 out << bold_heading('examples')
427 out << table_text(examples, ' ', ' ')
428 return out.join('\n')
429}
430
431// mode_help_text shows one mode's usage, options and examples
432fn mode_help_text(mode HelpMode) string {
433 mut out := []string{}
434 out << '${name_style(mode.name)} - ${mode.desc}'
435 out << ''
436 out << 'usage vpac ${mode.key} ${mode.usage}'.trim_right(' ')
437 if mode.options.len > 0 {
438 mut rows := []Row{}
439 for f in mode.options {
440 rows << row(f.name, f.desc)
441 }
442 out << ''
443 out << bold_heading('options')
444 out << table_text(rows, ' ', ' ')
445 }
446 if mode.name == 'search' || mode.name == 'info' {
447 out << ''
448 out << bold_heading('fields')
449 // as wide as the terminal the help is read on, the way info lays
450 // its own lists out, and info's fallback where there is none
451 cols, _ := term.get_terminal_size()
452 out << wrap_names(pkg_field_names, info_width(cols))
453 }
454 if mode.examples.len > 0 {
455 mut rows := []Row{}
456 for e in mode.examples {
457 rows << row(e.cmd, e.desc)
458 }
459 out << ''
460 out << bold_heading('examples')
461 out << table_text(rows, ' ', ' ')
462 }
463 return out.join('\n')
464}
465
466// wrap_names lays the field names out in lines no wider than width, as the
467// terminal counts width: a styled or multibyte name is as wide as it shows
468fn wrap_names(names []string, width int) string {
469 if names.len == 0 {
470 return ''
471 }
472 // the lines are wrap_items', info's own wrapping: here indented by two,
473 // and a line that goes on ends in its comma, which with the space after
474 // it is the room kept free of the width
475 return wrap_items(names, width - 4).map(' ' + it).join(',\n')
476}
477