File: tldr.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 for freebsd-specific entries by default
  37 #   -free     look for freebsd-specific entries by default
  38 #   -freebsd  look for freebsd-specific entries by default
  39 #
  40 #   -h        show this help message
  41 #   -help     show this help message
  42 #
  43 #   -l        look for linux-specific entries by default
  44 #   -linux    look for linux-specific entries by default
  45 #
  46 #   -m        look for (modern) macos-specific entries by default
  47 #   -mac      look for (modern) macos-specific entries by default
  48 #   -macos    look for (modern) macos-specific entries by default
  49 #
  50 #   -o        look for openbsd-specific entries by default
  51 #   -open     look for openbsd-specific entries by default
  52 #   -openbsd  look for openbsd-specific entries by default
  53 #
  54 #   -w        look for windows-specific entries by default
  55 #   -win      look for windows-specific entries by default
  56 #   -windows  look for windows-specific entries by default
  57 
  58 
  59 case "$1" in
  60     -h|--h|-help|--help)
  61         awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  62         exit 0
  63     ;;
  64     -f|--f|-free|--free|-freebsd|--freebsd)
  65         kind="freebsd"
  66         shift
  67     ;;
  68     -l|--l|-linux|--linux)
  69         kind="linux"
  70         shift
  71     ;;
  72     -m|--m|-mac|--mac|-macos|--macos|-macosx|--macosx|-osx|--osx)
  73         kind="osx"
  74         shift
  75     ;;
  76     -o|--o|-open|--open|-openbsd|--openbsd)
  77         kind="openbsd"
  78         shift
  79     ;;
  80     -w|--w|-win|--win|-windows|--windows)
  81         kind="windows"
  82         shift
  83     ;;
  84     --) shift;;
  85     -*)
  86         kind="$(echo "$1" | sed -E 's/--?//')"
  87         shift
  88     ;;
  89 esac
  90 
  91 if [ $# -eq 0 ]; then
  92     awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  93     exit 1
  94 fi
  95 
  96 base='https://raw.githubusercontent.com/tldr-pages/tldr/main/pages'
  97 kind="${kind:-common}"
  98 
  99 options='-MKiCRS'
 100 if [ $# -eq 1 ]; then
 101     options='-MKiCRS --header=1'
 102 fi
 103 
 104 got=0
 105 for arg in "$@"; do
 106     [ "${got}" -gt 0 ] && printf "\n"
 107     got=1
 108 
 109     # auto-complete partial names
 110     if echo "${arg}" | grep -q -v / ; then
 111         arg="${kind}/${arg}"
 112     fi
 113 
 114     printf "\e[7m# %-80s\e[0m\n" "${arg}"
 115 
 116     curl --silent --show-error "${base}/${arg}.md" 2>&1 | awk '
 117         NR == 1 && /^404: / {
 118             printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0
 119             next
 120         }
 121 
 122         NR == 1 { next }
 123 
 124         NR <= 2 || /^>/ {
 125             printf "\x1b[38;2;164;164;164m%s\x1b[0m\n", $0
 126             next
 127         }
 128 
 129         /^`/ {
 130             s = substr($0, 2, length - 2)
 131             printf "`\x1b[48;2;224;224;224m%s\x1b[0m`\n", s
 132             next
 133         }
 134 
 135         1
 136     '
 137 done | less ${options}