From 76a64b4c56a97d25c3cbe119b384a4440d33b051 Mon Sep 17 00:00:00 2001 From: Oliver Webb Date: Thu, 25 Jul 2024 19:11:13 -0500 Subject: [PATCH] Revise README, XDG_DATA_HOME, compact text-processing code --- README.md | 73 ++++++++++++++++++++++++++++++++++++------------------- cheat | 33 +++++-------------------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 223596a..5734cdc 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,53 @@ -cheat - Print useful info about a command +cheat - show use cases of a command --- -- Install cheat -git clone https://www.github.com/oliverkwebb/cheat && ./cheat/cheat -u +cheat is a POSIX shell script designed to replicate [cheat.sh][2] in a +offline manner. +--- +How to... +- Install cheat + ``` + git clone https://www.github.com/oliverkwebb/cheat && ./cheat/cheat -u + ``` - Update page database for cheat - -cheat -u - -- Show use cases of `rsync`, Using "less -RF", from all sources - -cheat rsync - -- Show guide on learning `awk` - -cheat learn-awk - -- Show page on `cpio` from source tldr - -cheat -d tldr cpio - + ``` + cheat -u + ``` +- Show use cases of `rsync` + ``` + cheat rsync + ``` +- View guide on learning `awk` + ``` + cheat learn-awk + ``` +- Open page on `cpio` from source tldr + ``` + cheat -d tldr cpio + ``` - Print use cases of `cut` to stdout, colorized - -cheat -c cut - -- Print use cases of `tar` uncolorized - -cheat -tc tar - + ``` + cheat -c cut + ``` +- Print use cases of `tar`, uncolorized + ``` + cheat -tc tar + ``` +--- +See Also:\ + [Bash Oneliner][1]\ + [cheat.sh][2] + +Page Sources: (These account for 96.6% of the pages on cheat.sh)\ + [Learn X in Y Minutes][3]\ + [tldr][4]\ + [cheat.sheets][5]\ + [cheat][6] + +[1]: https://onceupon.github.io/Bash-Oneliner/ +[2]: https://github.com/chubin/cheat.sh +[3]: https://learnxinyminutes.com/ +[4]: https://tldr.sh +[5]: https://github.com/chubin/cheat.sheets +[6]: https://github.com/cheat/cheat diff --git a/cheat b/cheat index 63fd7e5..81a5ac5 100755 --- a/cheat +++ b/cheat @@ -6,7 +6,7 @@ ESC_TL="\x1b[48;5;8m" PGER="less -RF" -CHEAT_PAGES_DIR="$HOME/.local/cheat" +CHEAT_PAGES_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/cheat" TMPDIR="$CHEAT_PAGES_DIR/tmpdir" PAGEFILE="$CHEAT_PAGES_DIR/page" @@ -27,19 +27,7 @@ update () { echo > "$PAGEFILE" for tldr_page in $(find "$TMPDIR/tldr-cv" ! -name 'tldr-cv' -printf '%f\n' | sed 's/.md$//'); do printf "[T]%s:\n" "tldr/$tldr_page" >> "$PAGEFILE" - awk '{\ - gsub(/\{\{|\}\}/, "");\ - gsub(/^[->]/, "#");\ - gsub(/^`|`$/, "");\ - \ - if(expcmd==1){\ - if($0 !~ /^\s*$/)\ - print $0;\ - } else print $0;\ - \ - if($1 ~ /#/) expcmd=1;\ - else expcmd=0;\ - }' "$TMPDIR/tldr-cv/$tldr_page.md" >> "$PAGEFILE" + sed 's/{{|}}//g; s/^[->]/#/g; s/^`\|`$//g;' "$TMPDIR/tldr-cv/$tldr_page.md" >> "$PAGEFILE" done #cheat @@ -87,21 +75,12 @@ shift $((OPTIND-1)) : ${1:?Needs Args} awk " - \$1 ~ /^\[T\]/ {\ - if(\$0 ~ /$GETFROM\\/$1:$/) j = 1;\ - else j = 0;\ - }\ - j != 0 {\ - if(\$0 ~ /^\[T\]/) {\ - gsub(/^\[T\]/, \"$ESC_TL\"); - gsub(/$/, \"$ESC_R\"); - }\ - if(\$1 ~ /^#/) {\ + /^\[T\]/ { j = /$GETFROM\\/$1:$/ }\ + j && (\$1 ~ /^#/ || /^\[T\]/) {\ + gsub(/^\[T\]/, \"$ESC_TL\") gsub(/^#/, \"$ESC_CC#\")\ gsub(/$/, \"$ESC_R\")\ - };\ - print \$0\ - }" "$PAGEFILE" | $PGER + } j" "$PAGEFILE" | tr -d '\r' | $PGER [ "$(stat -c '%Y' "$PAGEFILE" )" -lt "$(date -d '1 week ago' '+%s')" ] && \ printf "It has been over 1 week since you updated your pages, \nrun '%s -u' to update them\n" "$(basename "$0")"