Skip to content

Commit

Permalink
Revise README, XDG_DATA_HOME, compact text-processing code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Webb committed Jul 26, 2024
1 parent f1b78f0 commit 76a64b4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
73 changes: 48 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
33 changes: 6 additions & 27 deletions cheat
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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")"

0 comments on commit 76a64b4

Please sign in to comment.