commit 658fd443fdcdd9d9e068b99d372b7269d6b8b23b parent e2cff2134fe26996d2cf25b1f61fad69b5b58b7d Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 12 Feb 2019 11:25:12 +0100 Refine define functionality and split into separate script Diffstat:
M | links/.commands.sh | | | 1 | - |
A | links/bin/define | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/links/.commands.sh b/links/.commands.sh @@ -140,7 +140,6 @@ news() { ## web services alias youtube-dl='youtube-dl --format mp4' weather() { curl "wttr.in/?m"; } -define() { curl --silent dict://dict.org/d:"$1"; } transfer() { # use transfer.sh to share files over the net if [ $# -eq 0 ]; then echo -e "No arguments specified. Usage: transfer.sh <file>" diff --git a/links/bin/define b/links/bin/define @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -e + +function show_help { + echo "usage: ${0##*/} [OPTIONS] TERM[S]" + echo "shows definitions for each TERM from dict.org" + echo + echo "Options:" + echo " -h, --help show this message" + echo " -np, --no-pager do not use a pager for long output" +} + +function define { + curl --silent dict://dict.org/d:"$1" +} + +function remove_comm_msgs { + grep -vE '[0-9][0-9] ' | \ + sed 's/^\.//' +} + +function die { + printf '%s\n' "$1" >&2 + exit 1 +} + +[[ $# -lt 1 ]] && (show_help && exit 1) +pager=1 +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -np|--no-pager) + pager=0 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'ERROR: Unknown option specified' + ;; + *) # no more options + break + esac + shift +done + +for word in "$@"; do + definition="$(define "$word" | remove_comm_msgs)" + if [[ $(echo "$definition" | wc -l) -gt $(tput lines) ]] && \ + [[ "$pager" = 1 ]]; then + echo "$definition" | less + else + echo "$definition" + fi +done