Feat: Pure Bash Bible and TLDR in cheat

This commit is contained in:
2023-05-05 23:02:31 +03:00
parent e5fe49834d
commit 8de487250d
7 changed files with 150 additions and 5 deletions

2
.gitignore vendored
View File

@@ -5,3 +5,5 @@ Brewfile.lock.json
.idea
.vscode
config/fzf
config/cheat/cheatsheets/pure-bash-bible/*
config/cheat/cheatsheets/tldr/*

View File

@@ -1,6 +1,6 @@
---
# The editor to use with 'cheat -e <sheet>'. Defaults to $EDITOR or $VISUAL.
editor: $EDITOR
# editor: $EDITOR
# Should 'cheat' always colorize output?
colorize: true
@@ -8,7 +8,7 @@ colorize: true
# Which 'chroma' colorscheme should be applied to the output?
# Options are available here:
# https://github.com/alecthomas/chroma/tree/master/styles
style: catppuccin-mocha
style: "catppuccin"
# Which 'chroma' "formatter" should be applied?
# One of: "terminal", "terminal256", "terminal16m"
@@ -63,14 +63,22 @@ cheatpaths:
# 'readonly': shall user-created ('cheat -e') cheatsheets be saved here?
- name: community
path: ~/.config/cheat/cheatsheets/community
tags: [community]
tags: [ community ]
readonly: true
# If you have personalized cheatsheets, list them last. They will take
# precedence over the more global cheatsheets.
- name: personal
path: ~/.config/cheat/cheatsheets/personal
tags: [personal]
path: ~/.dotfiles/config/cheat/cheatsheets/personal
tags: [ personal ]
readonly: false
- name: pure-bash-bible
path: ~/.dotfiles/config/cheat/cheatsheets/pure-bash-bible
tags: [ pure-bash-bible ]
readonly: true
- name: tldr
path: ~/.dotfiles/config/cheat/cheatsheets/tldr
tags: [ tldr ]
readonly: true
# While it requires no configuration here, it's also worth noting that
# cheat will automatically append directories named '.cheat' within the

View File

@@ -10,6 +10,8 @@
~/.cache/git:
~/.config:
~/.config/cheat/cheatsheets/personal:
~/.config/cheat/cheatsheets/pure-bash-bible:
~/.config/cheat/cheatsheets/tldr:
~/.local:
~/.local/run:
~/.local/share:

View File

@@ -0,0 +1,54 @@
#!/usr/bin/env bash
# shellcheck disable=SC2231,SC2034,SC2181,SC2068
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
PBB_REQUIRED_TOOLS=(basename git mkdir cheat ls grep head awk cp echo rm)
for t in ${PBB_REQUIRED_TOOLS[@]}; do
! have "$t" && echo "(!) $t is missing, can't continue..." && exit 1
done
PBB_GIT="https://github.com/dylanaraps/pure-bash-bible.git"
PBB_SOURCE="source: $PBB_GIT"
PBB_SYNTAX="syntax: bash"
PBB_TAGS="tags: [bash]"
PBB_TEMP_PREFIX=$(basename "$0")
PBB_TEMP_DIR="/tmp/pbb-$(rnd)"
# If there's no .git, clone the folder
if [ ! -d "$PBB_TEMP_DIR/.git" ]; then
git clone "$PBB_GIT" "$PBB_TEMP_DIR"
fi
PBB_CHAPTERS=$(ls -1v "$PBB_TEMP_DIR"/manuscript/chapter*)
CHEAT_DEST="$(cheat -d | grep pure-bash-bible | head -1 | awk '{print $2}')"
if [ ! -d "$CHEAT_DEST" ]; then
mkdir -p "$CHEAT_DEST"
fi
for f in ${PBB_CHAPTERS[@]}; do
# get all headers, take the first one, strip the # and return the first word in lowercase
HEADER=$(grep -e '^[#] ' "$f" | head -1 | awk '{print tolower($2)}')
CHEAT_FILE="$CHEAT_DEST/${HEADER}"
echo "(*) $CHEAT_FILE"
if [ ! -f "$CHEAT_FILE" ]; then
cp "$f" "$CHEAT_FILE"
fi
LC_ALL=C perl -pi.bak -e 's/\<\!-- CHAPTER END --\>//' "$CHEAT_FILE"
rm "$CHEAT_FILE.bak"
# add tags if the file doesn't have them
if [ '---' != "$(head -1 < "$CHEAT_FILE")" ]; then
T="$PBB_SYNTAX\n$PBB_TAGS\n$PBB_SOURCE\n"
echo -e "---\n$T---\n$(cat "$CHEAT_FILE")" > "$CHEAT_FILE"
fi
done
# Cleanup
if [ -d "$PBB_TEMP_DIR" ]; then
rm -rf "$PBB_TEMP_DIR"
fi

73
scripts/install-cheat-tldr.sh Executable file
View File

@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# shellcheck disable=SC2231,SC2034,SC2181,SC2068
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
TLDR_REQUIRED_TOOLS=(basename git mkdir cheat ls grep head awk cp echo rm)
for t in ${TLDR_REQUIRED_TOOLS[@]}; do
! have "$t" && echo "(!) $t is missing, can't continue..." && exit 1
done
TLDR_GIT="https://github.com/tldr-pages/tldr.git"
TLDR_SOURCE="source: $TLDR_GIT"
TLDR_SYNTAX="syntax: markdown"
TLDR_TAGS="tags: [bash]"
TLDR_TEMP_PREFIX=$(basename "$0")
TLDR_TEMP_DIR="/tmp/cheat-tldr-$(rnd)"
# If there's no .git, clone the folder
if [ ! -d "$TLDR_TEMP_DIR/.git" ]; then
git clone "$TLDR_GIT" "$TLDR_TEMP_DIR"
fi
# Fetch the destination directory from cheat defined directories.
TLDR_CHEAT_DEST="$(cheat -d | grep tldr | head -1 | awk '{print $2}')"
[ "$TLDR_CHEAT_DEST" = "" ] && msg_err "cheat doesn't know about the destination" && exit 1
if [ ! -d "$TLDR_CHEAT_DEST" ]; then
mkdir -p "$TLDR_CHEAT_DEST"
fi
for d in "$TLDR_TEMP_DIR"/pages/*; do
DIRNAME=$(basename "$d")
# echo "-> $DIRNAME ($d)"
case "$DIRNAME" in
*common)
SECTION_DIR="${TLDR_CHEAT_DEST}"
TLDR_TAGS="tags: [common]"
;;
*)
SECTION_DIR="${TLDR_CHEAT_DEST}/$DIRNAME"
TLDR_TAGS="tags: [$DIRNAME]"
;;
esac
if [ ! -d "$SECTION_DIR" ]; then
mkdir -p "$SECTION_DIR"
fi
for FILE in $d/*.md; do
BASENAME=$(basename "$FILE" .md)
FILENAME="${BASENAME%%.*}"
# echo "-> $FILE = $FILENAME"
TLDR_FILE="$SECTION_DIR/${FILENAME}"
# echo "-> dest: $TLDR_FILE"
if [ ! -f "$TLDR_FILE" ]; then
cp "$FILE" "$TLDR_FILE"
fi
if [ -f "$TLDR_FILE" ] && [ '---' != "$(head -1 < "$TLDR_FILE")" ]; then
echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$TLDR_FILE")" > "$TLDR_FILE"
fi
done
done
# Cleanup
if [ -d "$TLDR_TEMP_DIR" ]; then
rm -rf "$TLDR_TEMP_DIR"
fi

View File

@@ -224,3 +224,9 @@ function path_prepend
path_remove "$1"
PATH="$1${PATH:+":$PATH"}"
}
# Creates a random string
rnd()
{
echo $RANDOM | md5sum | head -c 20
}