From 8de487250dac044f9b03aa9fa9970526ca451098 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 5 May 2023 23:02:31 +0300 Subject: [PATCH] Feat: Pure Bash Bible and TLDR in cheat --- .gitignore | 2 + config/cheat/cheatsheets/personal/.gitkeep | 0 config/cheat/conf.yml | 18 ++++-- install.conf.yaml | 2 + scripts/install-cheat-purebashbible.sh | 54 ++++++++++++++++ scripts/install-cheat-tldr.sh | 73 ++++++++++++++++++++++ scripts/shared.sh | 6 ++ 7 files changed, 150 insertions(+), 5 deletions(-) delete mode 100644 config/cheat/cheatsheets/personal/.gitkeep create mode 100755 scripts/install-cheat-purebashbible.sh create mode 100755 scripts/install-cheat-tldr.sh diff --git a/.gitignore b/.gitignore index 585c491..a8eda40 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ Brewfile.lock.json .idea .vscode config/fzf +config/cheat/cheatsheets/pure-bash-bible/* +config/cheat/cheatsheets/tldr/* diff --git a/config/cheat/cheatsheets/personal/.gitkeep b/config/cheat/cheatsheets/personal/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/config/cheat/conf.yml b/config/cheat/conf.yml index 0113ed6..b47de8e 100644 --- a/config/cheat/conf.yml +++ b/config/cheat/conf.yml @@ -1,6 +1,6 @@ --- # The editor to use with 'cheat -e '. 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 diff --git a/install.conf.yaml b/install.conf.yaml index 3e7d856..a3ebe03 100644 --- a/install.conf.yaml +++ b/install.conf.yaml @@ -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: diff --git a/scripts/install-cheat-purebashbible.sh b/scripts/install-cheat-purebashbible.sh new file mode 100755 index 0000000..3629dfa --- /dev/null +++ b/scripts/install-cheat-purebashbible.sh @@ -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 diff --git a/scripts/install-cheat-tldr.sh b/scripts/install-cheat-tldr.sh new file mode 100755 index 0000000..4463f30 --- /dev/null +++ b/scripts/install-cheat-tldr.sh @@ -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 diff --git a/scripts/shared.sh b/scripts/shared.sh index d200faa..41bc00b 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -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 +}