File: tldr.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # tldr [options...] [tools...]
  27 #
  28 #
  29 # Lookup examples from the `tldr` set, showing how to use command-line tools.
  30 # When not given options to look into OS-specific entries, all entries are
  31 # looked-up from the `common` set.
  32 #
  33 #
  34 # Options, also available starting with double-dashes
  35 #
  36 #   -f        look only for freebsd-specific entries
  37 #   -freebsd  look only for freebsd-specific entries
  38 #
  39 #   -h        show this help message
  40 #   -help     show this help message
  41 #
  42 #   -l        look only for linux-specific entries
  43 #   -linux    look only for linux-specific entries
  44 #
  45 #   -m        look only for macos-specific entries
  46 #   -macos    look only for macos-specific entries
  47 #
  48 #   -o        look only for openbsd-specific entries
  49 #   -openbsd  look only for openbsd-specific entries
  50 #
  51 #   -w        look only for windows-specific entries
  52 #   -win      look only for windows-specific entries
  53 #   -windows  look only for windows-specific entries
  54 
  55 
  56 case "$1" in
  57     -h|--h|-help|--help)
  58         awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  59         exit 0
  60     ;;
  61     -f|--f|-freebsd|--freebsd)
  62         kind="freebsd"
  63         shift
  64     ;;
  65     -l|--l|-linux|--linux)
  66         kind="linux"
  67         shift
  68     ;;
  69     -m|--m|-mac|--mac|-macos|--macos|-macosx|--macosx|-osx|--osx)
  70         kind="osx"
  71         shift
  72     ;;
  73     -o|--o|-openbsd|--openbsd)
  74         kind="openbsd"
  75         shift
  76     ;;
  77     -w|--w|-win|--win|-windows|--windows)
  78         kind="windows"
  79         shift
  80     ;;
  81     --) shift;;
  82     -*)
  83         kind="$(echo "$1" | sed -E 's/--?//')"
  84         shift
  85     ;;
  86 esac
  87 
  88 if [ $# -eq 0 ]; then
  89     awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  90     exit 1
  91 fi
  92 
  93 base='https://raw.githubusercontent.com/tldr-pages/tldr/main/pages'
  94 kind="${kind:-common}"
  95 
  96 options="-MKiCRS"
  97 if [ $# -eq 1 ]; then
  98     options="-MKiCRS --header=1"
  99 fi
 100 
 101 got=0
 102 for arg in "$@"; do
 103     [ "${got}" -gt 0 ] && printf "\n"
 104     got=1
 105 
 106     if echo "${arg}" | grep -q -v / ; then
 107         arg="${kind}/${arg}"
 108     fi
 109 
 110     printf "\e[7m%-80s\e[0m\n" "${arg}"
 111     curl -s --show-error "${base}/${arg}.md" 2>&1 | awk '
 112         NR == 1 && /^404: / { printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0; next }
 113         NR <= 2 || /^>/ { printf "\x1b[38;2;164;164;164m%s\x1b[0m\n", $0; next }
 114         /^`/ { printf "\x1b[48;2;224;224;224m%s\x1b[0m\n", $0; next }
 115         1
 116     '
 117 done | less "${options}"