File: tldr.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2026 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 # Lookup examples from the `tldr` set, showing how to use command-line tools.
  29 # When not given options to look into OS-specific entries, all entries are
  30 # looked-up from the `common` set.
  31 #
  32 # All options, which are also available starting with double-dashes
  33 #
  34 #   -f, -free, -freebsd    look for freebsd-specific entries by default
  35 #   -h, -help              show this help message
  36 #   -l, -linux             look for linux-specific entries by default
  37 #   -m, -mac, -macos       look for (modern) macos-specific entries by default
  38 #   -o, -open, -openbsd    look for openbsd-specific entries by default
  39 #   -w, -win, -windows     look for windows-specific entries by default
  40 
  41 
  42 case "$1" in
  43     -h|--h|-help|--help)
  44         awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  45         exit 0
  46     ;;
  47     -f|--f|-free|--free|-freebsd|--freebsd)
  48         kind="freebsd"
  49         shift
  50     ;;
  51     -l|--l|-linux|--linux)
  52         kind="linux"
  53         shift
  54     ;;
  55     -m|--m|-mac|--mac|-macos|--macos|-macosx|--macosx|-osx|--osx)
  56         kind="osx"
  57         shift
  58     ;;
  59     -o|--o|-open|--open|-openbsd|--openbsd)
  60         kind="openbsd"
  61         shift
  62     ;;
  63     -w|--w|-win|--win|-windows|--windows)
  64         kind="windows"
  65         shift
  66     ;;
  67     --) shift;;
  68     -*)
  69         kind="$(echo "$1" | sed -E 's/--?//')"
  70         shift
  71     ;;
  72 esac
  73 
  74 if [ $# -eq 0 ]; then
  75     awk '/^# +tldr /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  76     exit 1
  77 fi
  78 
  79 base='https://raw.githubusercontent.com/tldr-pages/tldr/main/pages'
  80 kind="${kind:-common}"
  81 
  82 options='-MKiCRS'
  83 if [ $# -eq 1 ]; then
  84     options='-MKiCRS --header=1'
  85 fi
  86 
  87 got=0
  88 for arg in "$@"; do
  89     [ "${got}" -gt 0 ] && printf "\n"
  90     got=1
  91 
  92     # auto-complete partial names
  93     if echo "${arg}" | grep -q -v / ; then
  94         arg="${kind}/${arg}"
  95     fi
  96 
  97     printf "\e[7m# %-80s\e[0m\n" "${arg}"
  98 
  99     curl --silent --show-error "${base}/${arg}.md" 2>&1 | awk '
 100         NR == 1 && /^404: / {
 101             printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0
 102             next
 103         }
 104 
 105         NR == 1 { next }
 106 
 107         NR <= 2 || /^>/ {
 108             printf "\x1b[38;2;164;164;164m%s\x1b[0m\n", $0
 109             next
 110         }
 111 
 112         /^`/ {
 113             s = substr($0, 2, length - 2)
 114             printf "`\x1b[48;2;224;224;224m%s\x1b[0m`\n", s
 115             next
 116         }
 117 
 118         1
 119     '
 120 done | less ${options}