File: clamor.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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 # clamor
  27 # Command-Line Augmentation Module, Optional Refinements
  28 #
  29 # A few extras beyond what `clam` offers to interactive shell commands,
  30 # specifically commands which use my apps/scripts, unlike `clam`, which
  31 # depends only on commonly pre-installed tools.
  32 #
  33 # You're supposed to `source` this script, so its definitions stay for
  34 # your whole shell session: for that, you can run `source clamor` or
  35 # `. clamor` (no quotes either way), either directly or at shell startup.
  36 #
  37 #
  38 # Partial list of funcs/commands added
  39 #
  40 # can       CAlculate with Nice numbers, using my scripts `ca` and `nn`
  41 # cu        Change Units, using my scripts `bu` and `nn`
  42 # dt3       Date Time, 3 months, using `dt` from `clam`
  43 # fomo2cad  FOreign MOney to CAnadian Dollars; uses `cancur` from `clam`
  44 # lab       Like A Book, shows lines the way books do; uses my script `book`
  45 # nfs       Nice File Sizes, using my scripts `nn` and `cext`
  46 # ns        Nice Size, using my scripts `nn` and `cext`
  47 # sf        Show Files (and folders)
  48 # slp       Show Latest Podcasts, using my scripts `podfeed` and `si`
  49 # surprise  show a random command defined in `clam`
  50 # tsv2ssv   run my script `realign`, using TSV-input settings
  51 # wcp       Word-Count Plus runs `wc`, enriching its output using my tools
  52 #
  53 # blue
  54 # bold
  55 # gray
  56 # green
  57 # highlight
  58 # hilite
  59 # magenta
  60 # orange
  61 # red
  62 # underline
  63 
  64 
  65 # handle help options
  66 case "$1" in
  67     -h|--h|-help|--help)
  68         # show help message, extracting the info-comment at the start
  69         # of this file, and quit
  70         awk '/^# +clamor/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  71         exit 0
  72     ;;
  73 esac
  74 
  75 
  76 # use a simple shell prompt
  77 # PS1="\$ "
  78 # PS2="> "
  79 
  80 # use a simple shell prompt, showing the current folder in the title
  81 # PS1="\[\e]0;\w\a\]\$ "
  82 # PS2="> "
  83 
  84 # prevent `less` from saving commands
  85 # LESSHISTFILE="-"
  86 # LESSSECURE=1
  87 
  88 # prevent the shell from saving commands
  89 # unset HISTFILE
  90 
  91 
  92 # aliases for external scripts
  93 alias uncsv='decsv'
  94 
  95 
  96 # dashed aliases of multi-word commands defined later
  97 # alias pick-tsv='picktsv'
  98 
  99 
 100 # line-styling shortcuts, using my script `style`
 101 blue() { style blue "$@"; }
 102 bold() { style bold "$@"; }
 103 gray() { style gray "$@"; }
 104 green() { style green "$@"; }
 105 highlight() { style highlight "$@"; }
 106 hilite() { style hilite "$@"; }
 107 magenta() { style magenta "$@"; }
 108 orange() { style orange "$@"; }
 109 red() { style red "$@"; }
 110 underline() { style underline "$@"; }
 111 
 112 
 113 # shortcuts for my script `ca`, using various decimal precisions
 114 ca10() { ca "scale=10; $*"; }
 115 ca20() { ca "scale=20; $*"; }
 116 ca30() { ca "scale=30; $*"; }
 117 ca40() { ca "scale=40; $*"; }
 118 ca50() { ca "scale=50; $*"; }
 119 ca60() { ca "scale=60; $*"; }
 120 ca70() { ca "scale=70; $*"; }
 121 ca80() { ca "scale=80; $*"; }
 122 ca90() { ca "scale=90; $*"; }
 123 
 124 # CAlculator with Nice numbers runs my script `ca` and colors results with
 125 # my script `nn`, alternating styles to make long numbers easier to read
 126 can() {
 127     ca "$@" | nn
 128 }
 129 
 130 # Change Units turns common US units into international ones; uses my
 131 # scripts `bu` (Better Units) and `nn` (Nice Numbers)
 132 cu() {
 133     bu "$@" | awk '
 134         NF == 5 { print $(NF-1), $NF }
 135         NF == 4 && $NF == "s" { print $(NF-1), $NF }
 136         NF == 4 && $NF != "s" { print $NF }
 137     ' | nn
 138 }
 139 
 140 # FOreign MOney to CAnadian Dollars: uses `cancur` from `clam` to fetch
 141 # data from the Bank of Canada, filtering columns to only show exchange
 142 # rates from a few select currencies
 143 fomo2cad() {
 144     cancur |
 145     awk -F "\t" -v x="${1:-1}" 'END {
 146         printf "%12s%18sCAD\n", $1, ""
 147         printf "%12.2f  USD =  %12.2f\n", x, x * $26
 148         printf "%12.2f  EUR =  %12.2f\n", x, x * $5
 149         printf "%12.2f  MXN =  %12.2f\n", x, x * $11
 150         # printf "%12.2f  GBP =  %12.2f\n", x, x * $25
 151         # printf "%12.2f  INR =  %12.2f\n", x, x * $7
 152         # printf "%12.2f  BRL =  %12.2f\n", x, x * $3
 153         # printf "%12.2f  JPY =  %12.2f\n", x, x * $9
 154         # printf "%12.2f  KRW =  %12.2f\n", x, x * $19
 155         # printf "%12.2f  AUD =  %12.2f\n", x, x * $2
 156     }'
 157 }
 158 
 159 # Help Me Remember my custom shell commands
 160 hmr() {
 161     local cmd
 162     cmd="bat"
 163     # debian linux uses a different name for the `bat` app
 164     if [ -e "/usr/bin/batcat" ]; then
 165         cmd="batcat"
 166     fi
 167 
 168     "$cmd" \
 169         --style=plain,header,numbers --theme='Monokai Extended Light' \
 170         --wrap=never --color=always "$(which clam)" "$(which clamor)" |
 171             sed 's-\x1b\[38;5;70m-\x1b\[38;5;28m-g' | less -KiCRS
 172 }
 173 
 174 # Like A Book groups lines as 2 side-by-side pages, the same way books
 175 # do it; uses my script `book`
 176 lab() {
 177     book "$(($(tput lines) - 1))" "$@" | less -KiCRS
 178 }
 179 
 180 # Nice File Sizes, using my scripts `nn` and `cext`
 181 nfs() {
 182     # turn arg-list into single-item lines
 183     awk 'BEGIN { for (i = 1; i < ARGC; i++) print ARGV[i]; exit }' "$@" |
 184     # calculate file-sizes, and reverse-sort results
 185     xargs -d '\n' wc -c | sort -rn |
 186     # start output with a header-like line, and add a MiB field
 187     awk 'BEGIN { printf "%5s  %9s  %8s  name\n", "n", "bytes", "MiB" }
 188     { printf "%6d  %9d  %8.2f  %s\n", NR - 1, $1, $1 / 1048576, $2 }' |
 189     # make zeros in the MiB field stand out with a special color
 190     awk '{ gsub(/ 0.00 /, "\x1b[38;5;103m 0.00 \x1b[0m"); print }' |
 191     # make numbers nice, alternating styles along 3-digit groups
 192     nn |
 193     # color-code file extensions
 194     cext |
 195     # make table breathe with empty lines, so tall outputs are readable
 196     awk '(NR - 2) % 5 == 1 && NR > 1 { print "" } 1'
 197 }
 198 
 199 # Nice Size, using my scripts `nn` and `cext`
 200 ns() {
 201     wc -c "$@" | nn | cext
 202 }
 203 
 204 # Show Files (and folders)
 205 # sf() {
 206 #     ls -al --color=never --time-style iso "$@" | nn |
 207 #         ecoli '^d' blue '^l' green
 208 # }
 209 sf() {
 210     listfiles "$@" | cext
 211 }
 212 
 213 # Styled LEAK, runs my script `leak`
 214 sleak() {
 215     leak "$@"
 216 }
 217 
 218 # Show Latest Podcasts, using my scripts `podfeed` and `si`
 219 slp() {
 220     podfeed -title "Latest Podcast Episodes as of $(date +'%F %T')" "$@" | si
 221 }
 222 
 223 # show a random command defined in `clam`
 224 surprise() {
 225     wat "$(g '^[a-z]+\(' "$(which clam)" | sed -E 's-\(.*--' | shuf -n 1)"
 226 }
 227 
 228 # run my script `realign` using tab as the only field-separator,
 229 # which means the resulting TSV items can have spaces in them
 230 tsv2ssv() {
 231     realign --tsv "$@"
 232 }
 233 
 234 # Word-Count Plus runs `wc` and enriches its output; uses my scripts `nn`,
 235 # and `cext`
 236 wcp() {
 237     wc "$@" | sort -rn | nn | cext | awk '{ printf "%6d  %s\n", NR - 1, $0 }'
 238 }