Skip to content

Commit

Permalink
Add human.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolasburr committed Nov 18, 2018
1 parent 8f38cdd commit 124f402
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/human.sh
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

###
### human.sh: Heuristics utility for man(1).
###

# Check if alias exists.
__is_alias() {
local ALIAS="$1"

alias "$ALIAS" >/dev/null 2>&1
return $?
}

# Extract command from alias.
__get_alias_value() {
local CUT="$(/usr/bin/which cut)"
local TR="$(/usr/bin/which tr)"

local ALIAS="$1"
local VALUE="$(alias "$ALIAS" | $CUT -d'=' -f2 | $TR -d "'")"

printf '%s\n' "$VALUE"
}

# Get usage information.
__get_human_usage() {
local USAGE

read -r -d '' USAGE <<EOS
Usage: human [OPTIONS] <NAME>
OPTIONS:
-x, --exclude Exclude aliases from lookup.
-h, --help Show usage information.
-v, --version Show current version.
EOS

printf '%s\n' "$USAGE"
}

# Get current version number.
__get_human_version() {
local VERSION="1.0.0"

printf '%s\n' "$VERSION"
}

human() {
local MAN="$(/usr/bin/which man)"

while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
__get_human_usage

return 0
;;
-v|--version)
__get_human_version

return 0
;;
*)
local NAME="$1"

shift
;;
esac
done

if __is_alias "$NAME"; then
local VALUE="$(__get_alias_value "$NAME")"

$MAN "$VALUE"
else
$MAN "$NAME"
fi
}

# Remove this alias if you would
# rather call `human` directly.
alias man='human'

0 comments on commit 124f402

Please sign in to comment.