Last active
December 20, 2018 18:10
-
-
Save waldyrious/805d6357fa0b55135354ea4b996e0818 to your computer and use it in GitHub Desktop.
simple shell client for tldr pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# shellcheck disable=SC2002 | |
# 1) Put this file into your local clone of git://github.com/tldr-pages/tldr: | |
# curl -L https://gist.github.com/waldyrious/805d6357fa0b55135354ea4b996e0818/raw/ -o path/to/tldr-repo/tldr.sh | |
# 2) Mark it as executable: | |
# chmod +x path/to/tldr-repo/tldr.sh | |
# 3) symlink it into /usr/local/bin: | |
# sudo ln -s /absolute/path/to/tldr-repo/tldr.sh /usr/local/bin/tldr | |
command_args="$*" | |
if [[ -z "${command_args// }" ]]; | |
then | |
$0 tldr # If no parameters are passed, default to the "tldr.md" page | |
# HTTP redirection status code 303 = See Other. | |
# This will actually exit with error code 47, because exit codes are modulo 256. | |
exit 303; | |
fi | |
# Merge multiple arguments into a single hyphenated argument | |
command=${command_args// /-} | |
len=${#command} | |
heading=$(printf '=%.0s' $(seq 1 "$len")) | |
# Convert the local platorm name to tldr's version. | |
# (Copied from https://github.com/raylee/tldr) | |
get_platform() { | |
case $(uname -s) in | |
Darwin) echo "osx" ;; | |
Linux) echo "linux" ;; | |
SunOS) echo "sunos" ;; | |
*) echo "common" ;; | |
esac | |
} | |
platform=$(get_platform) | |
# Define a way to get this script's absolute path, | |
# since realpath() is not universally available (e.g. on macOS) | |
# source: http://stackoverflow.com/a/30795461/266309 | |
script_abspath=$(perl -e 'use Cwd "abs_path"; print abs_path(@ARGV[0])' -- "$0") | |
# Change to the git repository's root | |
cd "$(dirname "$script_abspath")" || { | |
echo "Error: couldn't enter the tldr directory."; | |
# HTTP client error status code 502 = Bad Gateway | |
# (This script received an invalid response from an upstream resource it needed to fulfill the request.) | |
# This will actually exit with error code 246, because exit codes are modulo 256. | |
exit 502; | |
} | |
if [[ "${command_args}" == "--update" ]] | |
then | |
echo "Updating local tldr-pages repository..." | |
git pull --rebase >/dev/null 2>&1 | |
exit 0; | |
else | |
# If the local clone is more than a day old, update before proceeding. | |
find .git/FETCH_HEAD -mtime -7 2>/dev/null | grep FETCH_HEAD >/dev/null || { | |
echo -e "Local tldr-pages repository last updated over a week ago; auto-updating...\n"; | |
git pull --rebase >/dev/null 2>&1; | |
} | |
fi | |
cd pages || { | |
echo "Error: couldn't enter the pages directory"; | |
# See comment above about error code 502. | |
exit 502; | |
} | |
# Look for a page in the platform-specific folder | |
if [ -f "$platform/$command.md" ] | |
then | |
echo; # newline | |
cat "$platform/$command.md" | \ | |
sed -E -e "s/^# (.+)/\1º${heading}/" | tr 'º' '\n' | \ | |
sed -E -e "s/^- (.+)/\1/" | \ | |
sed -E -e "s/^\`(.+)\`/ \1/" | |
#pandoc -s -f markdown -t plain "$platform/$command.md" | |
# Look for a page in the common folder | |
elif [ -f "common/$command.md" ] | |
then | |
echo; # newline | |
cat "common/$command.md" | \ | |
sed -E -e "s/^# (.+)/\1º${heading}/" | tr 'º' '\n' | \ | |
sed -E -e "s/^- (.+)/\1/" | \ | |
sed -E -e "s/^\`(.+)\`/ \1/" | |
#pandoc -s -f markdown -t plain "common/$command.md" | |
else # No page was found, so list pages whose names start | |
echo "No page was found for the '$*' command. Did you spell it right?" | |
candidates=$(find common "$platform" -name "$command*" | sed -e "s|.*/||" -e "s|.md||") | |
if [[ $candidates ]] | |
then | |
echo -n "Maybe you meant one of these: " | |
echo "$candidates" | xargs | sed 's/ /, /g' | |
fi | |
echo -e "\nOtherwise, you could contribute the '$command.md' page! See \033[36mhttps://tldr-pages.github.io#contributing\033[0m." | |
# HTTP client error status code 404 = Not Found. | |
# This will actually exit with error code 148, because exit codes are modulo 256. | |
exit 404 | |
fi |
TODO: implement a basic search command using something like grep -rl clipboard ~/repos/tldr/pages/
TODO: perhaps calls to external commands (uname, perl...) could be replaced with pure-bash alternatives -- see https://github.com/dylanaraps/pure-bash-bible#get-the-name-of-the-operating-system--kernel for an alternative to uname
; not sure if realpath
is replacable in (concise) pure bash code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fetching procedure might need
--progress
or--verbose
to give some feedback in case there are network errors. It's probably also worth checking whether both STDIN and STDERR should be redirected to /dev/null. Ideally STDERR shouldn't.