vpac / src / help.v
479 lines · 466 sloc · 20.36 KB · 6ecabf51a7e6b50dae0593172d6edd8378facdf3
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{'-b, --pkgbuild', 'after the repos, offer the installed packages the repos do not carry, from the pkgbuild repos, asked one by one'},
192 HelpFlag{'--nopkgbuildconfirm', 'take yes at the pkgbuild pass without asking, the review still shown'},
193 HelpFlag{'--disable-timeout', 'let slow downloads take their time'},
194 HelpFlag{'-p, --print', 'list the archives the upgrade would install, and upgrade nothing'},
195 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
196 ]
197 examples: [
198 HelpExample{'vpac u -s', 'refresh the databases and upgrade the system'},
199 HelpExample{'vpac u --ignore linux', 'upgrade everything but linux'},
200 HelpExample{'vpac u -s -w', 'refresh, then only download what the upgrade needs'},
201 HelpExample{'vpac u -s --pkgbuild', 'refresh, upgrade the repos, then ask about the pkgbuild packages'},
202 HelpExample{'vpac u -uu', 'bring packages newer than the repos back down to them'},
203 ]
204 },
205 HelpMode{
206 key: 'l'
207 name: 'files'
208 desc: 'list the files of an installed package'
209 usage: '[options] <package...>'
210 options: [
211 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
212 ]
213 examples: [
214 HelpExample{'vpac l bash', 'list the files bash installed'},
215 ]
216 },
217 HelpMode{
218 key: 'o'
219 name: 'owner'
220 desc: 'find the installed package that owns a file, or every one that owns a directory'
221 usage: '[options] <file...>'
222 options: [
223 HelpFlag{'-q, --quiet', 'print the package names only'},
224 ]
225 examples: [
226 HelpExample{'vpac o /usr/bin/ls', 'which package owns /usr/bin/ls'},
227 HelpExample{'vpac o -q /usr/bin/ls', 'only its name'},
228 HelpExample{'vpac o ls', 'a bare name the current directory lacks is looked up in PATH'},
229 ]
230 },
231 HelpMode{
232 key: 'd'
233 name: 'orphans'
234 desc: 'list packages installed as dependencies that nothing requires'
235 usage: '[options]'
236 options: [
237 HelpFlag{'-t', '-tt also lists the ones something names as an optional dependency'},
238 ]
239 examples: [
240 HelpExample{'vpac d', 'list the orphans'},
241 HelpExample{'vpac d -tt', 'and the dependencies only an optdepends line keeps'},
242 ]
243 },
244 HelpMode{
245 key: 'b'
246 name: 'build'
247 desc: 'build and install a package from an arbitrary repo'
248 usage: '[options] <repo...>'
249 options: [
250 HelpFlag{'--nopkgbuildconfirm', 'skip the build question; the review is still shown'},
251 HelpFlag{'--noconfirm', 'answer yes to the install prompts, and refuse to build unless --nopkgbuildconfirm'},
252 ]
253 examples: [
254 HelpExample{'vpac b ~/src/mything', 'build a local checkout, review included, and install it'},
255 HelpExample{'vpac b https://example.org/mything.git', 'clone the repo under the pkgbuild build dir and install'},
256 HelpExample{'vpac b -h', 'show the options of the mode'},
257 ]
258 },
259 HelpMode{
260 key: 'c'
261 name: 'clean'
262 desc: 'clean the package cache and the pkgbuild builds'
263 usage: '[options]'
264 options: [
265 HelpFlag{'-c', 'the default clean'},
266 HelpFlag{'-cc', 'removes every cached package'},
267 HelpFlag{'-a, --all', 'remove every cached file and every pkgbuild build'},
268 HelpFlag{'-k, --keep <n>', 'keep the n newest versions of every package'},
269 HelpFlag{'--keep-current', 'also keep the versions the sync dbs carry'},
270 HelpFlag{'-p, --print', 'list the files and builds the clean would remove, and remove nothing'},
271 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
272 ]
273 examples: [
274 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'},
275 HelpExample{'vpac c -cc', 'remove every cached package'},
276 HelpExample{'vpac c -k 2', 'keep the two newest versions of every cached package'},
277 ]
278 },
279 HelpMode{
280 key: 'a'
281 name: 'autoremove'
282 desc: 'remove every orphan, and the dependencies that become unneeded'
283 usage: '[options]'
284 options: [
285 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
286 ]
287 examples: [
288 HelpExample{'vpac a', 'remove all orphans'},
289 ]
290 },
291 HelpMode{
292 key: 'k'
293 name: 'rank'
294 desc: 'rank mirrors by latency and download speed'
295 usage: '[options] [os]'
296 options: [
297 HelpFlag{'-n, --top <n>', 'show the n fastest mirrors (default 10)'},
298 HelpFlag{'-w, --write', 'write the ranking to <os>.lip in the mirrors dir (/etc/vpac.d/mirrors), as root if needed'},
299 HelpFlag{'-m, --mirrorlist', 'print the result as mirrorlist Server lines'},
300 HelpFlag{'-o, --output <file>', 'write the Server lines to a file, as root if needed'},
301 HelpFlag{'--stdin', 'rank the mirrors listed on stdin, instead of the os mirror list'},
302 HelpFlag{'--path <file>', 'file to time for speed, under the mirror root'},
303 ]
304 examples: [
305 HelpExample{'vpac k -n 5', 'the five fastest mirrors of this system'},
306 HelpExample{'vpac k -w', 'rank and write the config mirror file for this system'},
307 HelpExample{'vpac k -n 5 cachyos', 'the five fastest CachyOS mirrors'},
308 HelpExample{'vpac k -m -o /tmp/mirrorlist arch', 'write the fastest Arch mirrors to a file'},
309 HelpExample{'cat list.txt | vpac k --stdin', 'rank the mirrors in a list, one url per line'},
310 ]
311 },
312 HelpMode{
313 key: 't'
314 name: 'tree'
315 desc: 'show the dependency tree of a package'
316 usage: '[options] <package...>'
317 options: [
318 HelpFlag{'-r, --reverse', 'show what requires the package instead'},
319 HelpFlag{'-o, --optional', 'include optional dependencies'},
320 HelpFlag{'-d, --depth <n>', 'show n levels only (default: all)'},
321 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
322 ]
323 examples: [
324 HelpExample{'vpac t -d 2 bash', 'dependencies of bash, two levels deep'},
325 HelpExample{'vpac t -r glibc', 'packages that require glibc'},
326 ]
327 },
328 HelpMode{
329 key: 'v'
330 name: 'verify'
331 desc: 'check the files of installed packages'
332 usage: '[options] [package...]'
333 options: [
334 HelpFlag{'-k, --checksums', 'also check that backed up config files are unmodified'},
335 HelpFlag{'-kk, --full', "check every file against the package's mtree: type, owner, mode, link, time, size, sha256"},
336 HelpFlag{'-D, --db', 'check the local db instead: its files, dependencies, conflicts, shared files'},
337 HelpFlag{'-DD', "check that every sync package's dependencies resolve"},
338 ]
339 examples: [
340 HelpExample{'vpac v', 'check every installed package'},
341 HelpExample{'vpac v glibc', 'check one package'},
342 HelpExample{'vpac v -k', 'also check the backup files'},
343 HelpExample{'vpac v -kk glibc', 'check every file of glibc; run as root to read root-only files'},
344 HelpExample{'vpac v -D', 'check the local package database for consistency'},
345 ]
346 },
347 HelpMode{
348 key: 'f'
349 name: 'find'
350 desc: 'find the sync package that provides a file, from the file dbs'
351 usage: '[options] <path...> | -l <package...>'
352 options: [
353 HelpFlag{'-l, --list', 'list the files of a sync package instead'},
354 HelpFlag{'-y, --refresh', 'refresh the file dbs first (as root)'},
355 HelpFlag{'-r, --repos <list>', 'comma separated repos to search (default: all)'},
356 HelpFlag{'-q, --quiet', 'repo/pkg for paths, file paths for -l'},
357 HelpFlag{'-x, --regex', 'the targets are regular expressions: with a slash matched against paths, without against file names'},
358 HelpFlag{'--machinereadable', 'one line a file, its repo, package, version and path parted by NUL bytes'},
359 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
360 ]
361 examples: [
362 HelpExample{'vpac f /usr/bin/vlc', 'which sync package provides /usr/bin/vlc'},
363 HelpExample{'vpac f -l dolphin', 'the files of an uninstalled package'},
364 HelpExample{"vpac f -x '^usr/bin/vl.*'", 'every package with a file the expression matches'},
365 HelpExample{'vpac f -y', 'refresh the file dbs first'},
366 ]
367 },
368]
369
370// find_mode matches a mode by its short key or its name
371fn find_mode(name string) ?HelpMode {
372 for m in help_modes {
373 if m.key == name || m.name == name {
374 return m
375 }
376 }
377 return none
378}
379
380// show_help prints a help text, or sends it as a help event
381fn show_help(text string) {
382 if ipc_on() {
383 emit(HelpEvent{
384 text: text
385 })
386 return
387 }
388 println(text)
389}
390
391// overview_examples are the overview's examples: some of the modes' own,
392// named by their command, so each is written once, in its mode
393const overview_examples = ['vpac s -r linux-lts', 'vpac i patchelf', 'vpac c -k 2', 'vpac t -d 2 bash']
394
395// overview_example_rows finds the overview's examples in the modes
396fn overview_example_rows() []Row {
397 mut rows := []Row{}
398 for cmd in overview_examples {
399 for m in help_modes {
400 for e in m.examples {
401 if e.cmd == cmd {
402 rows << row(e.cmd, e.desc)
403 }
404 }
405 }
406 }
407 return rows
408}
409
410fn help_text() string {
411 mut modes := []Row{}
412 for m in help_modes {
413 modes << row(heading(m.key), m.name, m.desc)
414 }
415 examples := overview_example_rows()
416
417 mut out := []string{}
418 out << bold_heading('usage') + ' vpac [flags] <mode> [options] [args]'
419 out << ' --config <file> read this config instead of ${config_path}'
420 out << ' --eval <blip> run blip after the config, extending it for this run'
421 out << ' --color <when> auto, always or never: auto styles a terminal only'
422 out << ' --ui <kind> tty, or ipc: json lines on stdout, answers on stdin'
423 out << ' --verbose say the housekeeping failures a run otherwise goes on from'
424 out << ' vpac <mode> -h show the options of a mode'
425 out << ''
426 out << bold_heading('modes')
427 out << table_text(modes, ' ', ' ')
428 out << ''
429 out << bold_heading('examples')
430 out << table_text(examples, ' ', ' ')
431 return out.join('\n')
432}
433
434// mode_help_text shows one mode's usage, options and examples
435fn mode_help_text(mode HelpMode) string {
436 mut out := []string{}
437 out << '${name_style(mode.name)} - ${mode.desc}'
438 out << ''
439 out << 'usage vpac ${mode.key} ${mode.usage}'.trim_right(' ')
440 if mode.options.len > 0 {
441 mut rows := []Row{}
442 for f in mode.options {
443 rows << row(f.name, f.desc)
444 }
445 out << ''
446 out << bold_heading('options')
447 out << table_text(rows, ' ', ' ')
448 }
449 if mode.name == 'search' || mode.name == 'info' {
450 out << ''
451 out << bold_heading('fields')
452 // as wide as the terminal the help is read on, the way info lays
453 // its own lists out, and info's fallback where there is none
454 cols, _ := term.get_terminal_size()
455 out << wrap_names(pkg_field_names, info_width(cols))
456 }
457 if mode.examples.len > 0 {
458 mut rows := []Row{}
459 for e in mode.examples {
460 rows << row(e.cmd, e.desc)
461 }
462 out << ''
463 out << bold_heading('examples')
464 out << table_text(rows, ' ', ' ')
465 }
466 return out.join('\n')
467}
468
469// wrap_names lays the field names out in lines no wider than width, as the
470// terminal counts width: a styled or multibyte name is as wide as it shows
471fn wrap_names(names []string, width int) string {
472 if names.len == 0 {
473 return ''
474 }
475 // the lines are wrap_items', info's own wrapping: here indented by two,
476 // and a line that goes on ends in its comma, which with the space after
477 // it is the room kept free of the width
478 return wrap_items(names, width - 4).map(' ' + it).join(',\n')
479}
480