module main import term struct HelpFlag { name string desc string } struct HelpExample { cmd string desc string } struct HelpMode { key string name string desc string usage string options []HelpFlag examples []HelpExample } const help_modes = [ HelpMode{ key: 's' name: 'search' desc: 'search packages in the local, sync and pkgbuild databases' usage: '[options] ' options: [ HelpFlag{'-l, --local', 'search the installed packages'}, HelpFlag{'-r, --remote', 'search the sync databases (the default)'}, HelpFlag{'-b, --pkgbuild', 'search the pkgbuild repo'}, HelpFlag{'-f, --fuzzy', 'fuzzy matching instead of substring matching'}, HelpFlag{'-t, --thold <0-1>', 'fuzzy match threshold (default 0.9)'}, HelpFlag{'-q, --quiet', 'print package names only'}, HelpFlag{'-s, --sync', 'refresh the sync databases first'}, HelpFlag{'-F, --fields ', 'columns to show: all, or a comma list (see fields)'}, HelpFlag{'-d, --deps', 'installed packages installed as dependencies [filter]'}, HelpFlag{'-e, --explicit', 'installed packages installed explicitly [filter]'}, HelpFlag{'-m, --foreign', 'installed packages not in the sync dbs [filter]'}, HelpFlag{'-n, --native', 'installed packages found in the sync dbs [filter]'}, HelpFlag{'--unrequired', 'installed packages nothing requires [filter]'}, HelpFlag{'', 'twice keeps the ones only optionally required'}, HelpFlag{'-u, --upgrades', 'installed packages with a newer version in the sync dbs [filter]'}, HelpFlag{'-g, --groups', 'list the groups, or the members of the groups named'}, HelpFlag{'', 'with -gg every group with its members'}, HelpFlag{'', 'with -l the installed groups'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, HelpFlag{'-i, --installed', 'keep only packages that are installed locally'}, ] examples: [ HelpExample{'vpac s -r linux-lts', 'search the sync databases for linux-lts'}, HelpExample{'vpac s -l -F name,version,size', 'installed packages with their sizes'}, HelpExample{'vpac s -l -e', 'installed packages installed explicitly'}, HelpExample{'vpac s -l -u', 'installed packages with an update available'}, HelpExample{'vpac s -r visual code', 'packages with both words in the name or description'}, HelpExample{'vpac s -l -f -t 0.8 patchelv', 'fuzzy search the installed packages'}, HelpExample{'vpac s -g base-devel', 'the members of a sync group'}, HelpExample{'vpac s -l -gg', 'every installed group with its members'}, ] }, HelpMode{ key: 'q' name: 'info' desc: 'show package information, falling back to the sync databases' usage: '[options] ' options: [ HelpFlag{'-F, --fields ', 'fields to show: all, or a comma list (see fields)'}, HelpFlag{'-c, --changelog', 'show the changelog of the package instead'}, HelpFlag{'-p, --file', 'the targets are package files, not installed or sync packages'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac q patchelf', 'show what is installed for patchelf'}, HelpExample{'vpac q -F name,version,depends bash', 'only those three fields'}, HelpExample{'vpac q extra/bash', 'the bash of one repo; local/bash is the installed one'}, HelpExample{"vpac q 'sh>=5'", 'whatever satisfies a dependency'}, HelpExample{'vpac q -c bash', 'the changelog bash was packaged with'}, HelpExample{'vpac q -p ./a.pkg.tar.zst ./b.pkg.tar.zst', 'package files on disk'}, ] }, HelpMode{ key: 'w' name: 'which' desc: 'find the installed package that provides a dependency' usage: '' examples: [ HelpExample{'vpac w libpng.so', 'which package provides libpng.so'}, ] }, HelpMode{ key: 'n' name: 'deptest' desc: 'print the dependencies no installed package satisfies, exit 127 if any' usage: '' examples: [ HelpExample{"vpac n 'glibc>=2.40' cowsay", 'print whichever of the two is not satisfied'}, ] }, HelpMode{ key: 'm' name: 'mark' desc: 'change why an installed package is installed' usage: '-r ' options: [ HelpFlag{'-r, --reason ', 'explicit, depend or unknown'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac m -r depend patchelf', 'mark patchelf as installed as a dependency'}, ] }, HelpMode{ key: 'i' name: 'install' desc: 'install from the repos, a local file or the pkgbuild repo (asked first)' usage: '[options] ' options: [ HelpFlag{'--no-upgrade-deps', 'leave outdated dependencies and their dependents alone'}, HelpFlag{'--needed', 'skip the targets installed at the version they would install'}, HelpFlag{'--asdeps', 'mark the targets as installed as dependencies'}, HelpFlag{'--asexplicit', 'mark the targets as explicitly installed'}, HelpFlag{'-d, --nodeps', 'skip the dependency version checks'}, HelpFlag{'-dd', 'skips the dependency checks'}, HelpFlag{'--assume-installed ', 'count a dependency as installed (repeatable, comma separated)'}, HelpFlag{'--overwrite ', 'overwrite conflicting files that match (repeatable, comma separated)'}, HelpFlag{'--ignore ', 'leave a package out of this run (repeatable, comma separated)'}, HelpFlag{'--ignoregroup ', 'leave a group out of this run (repeatable, comma separated)'}, HelpFlag{'--dbonly', 'change the database only, not the files on disk'}, HelpFlag{'--disable-timeout', 'let slow downloads take their time'}, HelpFlag{'-b, --pkgbuild', 'build from the pkgbuild repo only, offering the PKGBUILD for review first'}, HelpFlag{'--nopkgbuildconfirm', 'skip the questions asked while installing from the pkgbuild repo'}, HelpFlag{'-w, --download-only', 'download the packages into the cache, and install nothing'}, HelpFlag{'-p, --print', 'list the archives the transaction would install, and install nothing'}, HelpFlag{'--noconfirm', 'answer yes to repo prompts, and refuse pkgbuild builds unless --nopkgbuildconfirm'}, ] examples: [ HelpExample{'vpac i patchelf', 'install patchelf, upgrading its outdated dependencies'}, HelpExample{'vpac i ./foo-1.0-1-x86_64.pkg.tar.zst', 'install a local package file'}, HelpExample{'vpac i patchelf bash', 'install both in one transaction, confirmed once'}, HelpExample{'vpac i --needed --asdeps patchelf', 'install patchelf as a dependency, unless it is up to date'}, HelpExample{'vpac i extra/patchelf', 'install it from that repo and no other'}, HelpExample{"vpac i 'bash>=5' sh", 'install what satisfies a version, or provides a name'}, HelpExample{'vpac i base-devel', 'install a group: its members are listed, to pick from'}, ] }, HelpMode{ key: 'r' name: 'remove' desc: 'remove an installed package' usage: '[options] ' options: [ HelpFlag{'-s, --recursive', 'also remove the dependencies the removal leaves unneeded'}, HelpFlag{'-ss', 'the explicitly installed ones too'}, HelpFlag{'-u, --unneeded', 'leave the targets something still requires, instead of failing'}, HelpFlag{'-c, --cascade', 'also remove the packages that depend on the targets'}, HelpFlag{'-n, --nosave', 'remove changed config files too, instead of keeping .pacsave copies'}, HelpFlag{'-d, --nodeps', 'skip the dependency version checks'}, HelpFlag{'-dd', 'skips the dependency checks'}, HelpFlag{'--assume-installed ', 'count a dependency as installed (repeatable, comma separated)'}, HelpFlag{'--dbonly', 'change the database only, not the files on disk'}, HelpFlag{'-p, --print', 'list the packages the removal would take, as name-version, and remove nothing'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac r patchelf', 'remove patchelf'}, HelpExample{'vpac r -s patchelf', 'remove patchelf and the dependencies only it needed'}, HelpExample{'vpac r -c libfoo', 'remove libfoo and every package that needs it'}, HelpExample{'vpac r -ss patchelf', 'also the dependencies that were installed explicitly'}, HelpExample{'vpac r -u libfoo libbar', 'remove whichever of them nothing needs'}, HelpExample{'vpac r xorg-apps', 'remove the installed members of a group'}, ] }, HelpMode{ key: 'u' name: 'upgrade' desc: 'upgrade the whole system' usage: '[options]' options: [ HelpFlag{'-s, --sync', 'refresh the sync databases first'}, HelpFlag{'-w, --download-only', 'download the upgrades without installing them'}, HelpFlag{'-uu, --downgrade', 'also downgrade packages newer than the sync dbs carry'}, HelpFlag{'--ignore ', 'hold a package back this time (repeatable, comma separated)'}, HelpFlag{'--ignoregroup ', 'hold a group back this time (repeatable, comma separated)'}, HelpFlag{'-d, --nodeps', 'skip the dependency version checks'}, HelpFlag{'-dd', 'skips the dependency checks'}, HelpFlag{'--assume-installed ', 'count a dependency as installed (repeatable, comma separated)'}, HelpFlag{'--overwrite ', 'overwrite conflicting files that match (repeatable, comma separated)'}, HelpFlag{'--dbonly', 'change the database only, not the files on disk'}, HelpFlag{'-b, --pkgbuild', 'after the repos, offer the installed packages the repos do not carry, from the pkgbuild repos, asked one by one'}, HelpFlag{'--nopkgbuildconfirm', 'take yes at the pkgbuild pass without asking, the review still shown'}, HelpFlag{'--disable-timeout', 'let slow downloads take their time'}, HelpFlag{'-p, --print', 'list the archives the upgrade would install, and upgrade nothing'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac u -s', 'refresh the databases and upgrade the system'}, HelpExample{'vpac u --ignore linux', 'upgrade everything but linux'}, HelpExample{'vpac u -s -w', 'refresh, then only download what the upgrade needs'}, HelpExample{'vpac u -s --pkgbuild', 'refresh, upgrade the repos, then ask about the pkgbuild packages'}, HelpExample{'vpac u -uu', 'bring packages newer than the repos back down to them'}, ] }, HelpMode{ key: 'l' name: 'files' desc: 'list the files of an installed package' usage: '[options] ' options: [ HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac l bash', 'list the files bash installed'}, ] }, HelpMode{ key: 'o' name: 'owner' desc: 'find the installed package that owns a file, or every one that owns a directory' usage: '[options] ' options: [ HelpFlag{'-q, --quiet', 'print the package names only'}, ] examples: [ HelpExample{'vpac o /usr/bin/ls', 'which package owns /usr/bin/ls'}, HelpExample{'vpac o -q /usr/bin/ls', 'only its name'}, HelpExample{'vpac o ls', 'a bare name the current directory lacks is looked up in PATH'}, ] }, HelpMode{ key: 'd' name: 'orphans' desc: 'list packages installed as dependencies that nothing requires' usage: '[options]' options: [ HelpFlag{'-t', '-tt also lists the ones something names as an optional dependency'}, ] examples: [ HelpExample{'vpac d', 'list the orphans'}, HelpExample{'vpac d -tt', 'and the dependencies only an optdepends line keeps'}, ] }, HelpMode{ key: 'b' name: 'build' desc: 'build and install a package from an arbitrary repo' usage: '[options] ' options: [ HelpFlag{'--nopkgbuildconfirm', 'skip the build question; the review is still shown'}, HelpFlag{'--noconfirm', 'answer yes to the install prompts, and refuse to build unless --nopkgbuildconfirm'}, ] examples: [ HelpExample{'vpac b ~/src/mything', 'build a local checkout, review included, and install it'}, HelpExample{'vpac b https://example.org/mything.git', 'clone the repo under the pkgbuild build dir and install'}, HelpExample{'vpac b -h', 'show the options of the mode'}, ] }, HelpMode{ key: 'c' name: 'clean' desc: 'clean the package cache and the pkgbuild builds' usage: '[options]' options: [ HelpFlag{'-c', 'the default clean'}, HelpFlag{'-cc', 'removes every cached package'}, HelpFlag{'-a, --all', 'remove every cached file and every pkgbuild build'}, HelpFlag{'-k, --keep ', 'keep the n newest versions of every package'}, HelpFlag{'--keep-current', 'also keep the versions the sync dbs carry'}, HelpFlag{'-p, --print', 'list the files and builds the clean would remove, and remove nothing'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ 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'}, HelpExample{'vpac c -cc', 'remove every cached package'}, HelpExample{'vpac c -k 2', 'keep the two newest versions of every cached package'}, ] }, HelpMode{ key: 'a' name: 'autoremove' desc: 'remove every orphan, and the dependencies that become unneeded' usage: '[options]' options: [ HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac a', 'remove all orphans'}, ] }, HelpMode{ key: 'k' name: 'rank' desc: 'rank mirrors by latency and download speed' usage: '[options] [os]' options: [ HelpFlag{'-n, --top ', 'show the n fastest mirrors (default 10)'}, HelpFlag{'-w, --write', 'write the ranking to the config’s mirror file in the mirrors dir (/etc/vpac.d/mirrors), as root if needed'}, HelpFlag{'-m, --mirrorlist', 'print the result as mirrorlist Server lines'}, HelpFlag{'-o, --output ', 'write the Server lines to a file, as root if needed'}, HelpFlag{'--stdin', 'rank the mirrors listed on stdin, instead of the os mirror list'}, HelpFlag{'--path ', 'file to time for speed, under the mirror root'}, ] examples: [ HelpExample{'vpac k -n 5', 'the five fastest mirrors of this system'}, HelpExample{'vpac k -w', 'rank and write the config mirror file for this system'}, HelpExample{'vpac k -n 5 cachyos', 'the five fastest CachyOS mirrors'}, HelpExample{'vpac k -m -o /tmp/mirrorlist arch', 'write the fastest Arch mirrors to a file'}, HelpExample{'cat list.txt | vpac k --stdin', 'rank the mirrors in a list, one url per line'}, ] }, HelpMode{ key: 't' name: 'tree' desc: 'show the dependency tree of a package' usage: '[options] ' options: [ HelpFlag{'-r, --reverse', 'show what requires the package instead'}, HelpFlag{'-o, --optional', 'include optional dependencies'}, HelpFlag{'-d, --depth ', 'show n levels only (default: all)'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac t -d 2 bash', 'dependencies of bash, two levels deep'}, HelpExample{'vpac t -r glibc', 'packages that require glibc'}, ] }, HelpMode{ key: 'v' name: 'verify' desc: 'check the files of installed packages' usage: '[options] [package...]' options: [ HelpFlag{'-k, --checksums', 'also check that backed up config files are unmodified'}, HelpFlag{'-kk, --full', "check every file against the package's mtree: type, owner, mode, link, time, size, sha256"}, HelpFlag{'-D, --db', 'check the local db instead: its files, dependencies, conflicts, shared files'}, HelpFlag{'-DD', "check that every sync package's dependencies resolve"}, ] examples: [ HelpExample{'vpac v', 'check every installed package'}, HelpExample{'vpac v glibc', 'check one package'}, HelpExample{'vpac v -k', 'also check the backup files'}, HelpExample{'vpac v -kk glibc', 'check every file of glibc; run as root to read root-only files'}, HelpExample{'vpac v -D', 'check the local package database for consistency'}, ] }, HelpMode{ key: 'f' name: 'find' desc: 'find the sync package that provides a file, from the file dbs' usage: '[options] | -l ' options: [ HelpFlag{'-l, --list', 'list the files of a sync package instead'}, HelpFlag{'-y, --refresh', 'refresh the file dbs first (as root)'}, HelpFlag{'-r, --repos ', 'comma separated repos to search (default: all)'}, HelpFlag{'-q, --quiet', 'repo/pkg for paths, file paths for -l'}, HelpFlag{'-x, --regex', 'the targets are regular expressions: with a slash matched against paths, without against file names'}, HelpFlag{'--machinereadable', 'one line a file, its repo, package, version and path parted by NUL bytes'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, ] examples: [ HelpExample{'vpac f /usr/bin/vlc', 'which sync package provides /usr/bin/vlc'}, HelpExample{'vpac f -l dolphin', 'the files of an uninstalled package'}, HelpExample{"vpac f -x '^usr/bin/vl.*'", 'every package with a file the expression matches'}, HelpExample{'vpac f -y', 'refresh the file dbs first'}, ] }, ] // find_mode matches a mode by its short key or its name fn find_mode(name string) ?HelpMode { for m in help_modes { if m.key == name || m.name == name { return m } } return none } // show_help prints a help text, or sends it as a help event fn show_help(text string) { if ipc_on() { emit(HelpEvent{ text: text }) return } println(text) } // overview_examples are the overview's examples: some of the modes' own, // named by their command, so each is written once, in its mode const overview_examples = ['vpac s -r linux-lts', 'vpac i patchelf', 'vpac c -k 2', 'vpac t -d 2 bash'] // overview_example_rows finds the overview's examples in the modes fn overview_example_rows() []Row { mut rows := []Row{} for cmd in overview_examples { for m in help_modes { for e in m.examples { if e.cmd == cmd { rows << row(e.cmd, e.desc) } } } } return rows } fn help_text() string { mut modes := []Row{} for m in help_modes { modes << row(heading(m.key), m.name, m.desc) } examples := overview_example_rows() mut out := []string{} out << bold_heading('usage') + ' vpac [flags] [options] [args]' out << ' --config read this config instead of ${config_path}' out << ' --eval evaluate an override in the config file’s syntax' out << ' --color auto, always or never: auto styles a terminal only' out << ' --ui tty, or ipc: json lines on stdout, answers on stdin' out << ' --verbose say the housekeeping failures a run otherwise goes on from' out << ' vpac -h show the options of a mode' out << '' out << bold_heading('modes') out << table_text(modes, ' ', ' ') out << '' out << bold_heading('examples') out << table_text(examples, ' ', ' ') return out.join('\n') } // mode_help_text shows one mode's usage, options and examples fn mode_help_text(mode HelpMode) string { mut out := []string{} out << '${name_style(mode.name)} - ${mode.desc}' out << '' out << 'usage vpac ${mode.key} ${mode.usage}'.trim_right(' ') if mode.options.len > 0 { mut rows := []Row{} for f in mode.options { rows << row(f.name, f.desc) } out << '' out << bold_heading('options') out << table_text(rows, ' ', ' ') } if mode.name == 'search' || mode.name == 'info' { out << '' out << bold_heading('fields') // as wide as the terminal the help is read on, the way info lays // its own lists out, and info's fallback where there is none cols, _ := term.get_terminal_size() out << wrap_names(pkg_field_names, info_width(cols)) } if mode.examples.len > 0 { mut rows := []Row{} for e in mode.examples { rows << row(e.cmd, e.desc) } out << '' out << bold_heading('examples') out << table_text(rows, ' ', ' ') } return out.join('\n') } // wrap_names lays the field names out in lines no wider than width, as the // terminal counts width: a styled or multibyte name is as wide as it shows fn wrap_names(names []string, width int) string { if names.len == 0 { return '' } // the lines are wrap_items', info's own wrapping: here indented by two, // and a line that goes on ends in its comma, which with the space after // it is the room kept free of the width return wrap_items(names, width - 4).map(' ' + it).join(',\n') }