diff --git a/README.md b/README.md index 73cbf16..7555e45 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ will create the symbolic link for each script in the \ directory. ## Shell +- [cnf](scripts/cnf) is a command line interface for [command-not-found website](https://command-not-found.com). - [dus](scripts/dus) shows the directories' sizes sorted in descending order (see also [dust](https://github.com/bootandy/dust)). - [ext](scripts/ext) unpacks archives. - [pk](scripts/pk) creates archives of various types. diff --git a/scripts/cnf b/scripts/cnf new file mode 100755 index 0000000..b80f4f2 --- /dev/null +++ b/scripts/cnf @@ -0,0 +1,43 @@ +#!/bin/bash + +# CLI for https://command-not-found.com. + +trim() { + tr -d '\n' +} + +die() { + echo "$*" >&2 + exit 1 +} + +xpath() { + xmllint --html --xpath "$1" - 2>&- +} + +if [ -z "$1" ]; then + die "USAGE: $(basename "$0") COMMAND_NAME" +fi + +CNF_PAGE=$(curl -sq -L "command-not-found.com/$1") + +CMD_NAME=$(xpath '//*[contains(@class, "row-command-info")]/div/h2/text()' <<<"$CNF_PAGE" | trim) + +[ -z "$CMD_NAME" ] && die "Command '$1' not found." + +CMD_DESCRIPTION=$(xpath '(//*[contains(@class, "row-command-info")]/div/p)[1]/text()' <<<"$CNF_PAGE" \ + | trim | sed -E 's/(.)\.(.)/\1. \2/g' | fmt -s -w 60) + +OS_LIST=$(xpath '//*[contains(@class, "card-body")]/dl/div/dt/text()' <<<"$CNF_PAGE" \ + | sed -E '/^$/d; s/^[[:space:]]+//') + +INSTALL_CMDS=$(xpath '//div[@class="card-body"]/dl/div/dd/code' <<<"$CNF_PAGE" \ + | sed -E -e $'s/>\\\n//g') + +# ------------------------------------------------------------------------------ + +echo -e "# $CMD_NAME\n" +echo -e "$CMD_DESCRIPTION\n" + +paste <(echo "$OS_LIST") <(echo "$INSTALL_CMDS") --delimiter '@' | column -t -s '@' +