1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-08-12 09:04:39 +02:00

Restructured Subfunctions to be simpler

This commit is contained in:
ryangreenup
2020-07-14 21:23:47 +10:00
parent f6e8e1c190
commit 85db26cfff
3 changed files with 82 additions and 113 deletions

View File

@@ -11,16 +11,12 @@ this is an attempt to wrap them into a single script and then have aliases to ma
## Philosophy ## Philosophy
- cadmus acts as a menu for scripts to acheive things - ****<span style="color:rgb(90,210,90);font-family:Courier New,Courier, monospace,serif;">cadmus</span>**** acts as a menu for scripts to acheive things
- the script name will always be printed to the terminal so the individual - the script name will always be printed to the terminal so the individual
script can be used for whatever purpose. script can be used for whatever purpose.
- SubFunctions will take only two arguments: - SubFunctions will take *only one* argument or `STDIN`
- `-h` or `--help` as a help function - If the first argument is either `-h` or `--help` help will be printed and then `exit 0`
- If this is seen anywhere the help will be printed and the script exit will. - This might lead to some limitations but the simplicity is for sanity, modularity and extensibility.
- Some type of input:
- `-d` or `--dir` as a directory location
- `-p` or `--path` as a file location
- `STDIN` which is piped into the subfunction.

View File

@@ -10,24 +10,90 @@ set -o pipefail # don't hide errors within pipes
# * Main Function # * Main Function
main() { main() {
setVars
check_for_help "${@}" # If help is detected this script will exit
check_for_dependencies check_for_dependencies
arguments "${@}" setVars
SkimAndGrep readFirstArgument "${@}"
SkimNotes "${@}"
} }
# ** Helper Functions # ** Helper Functions
# *** Check for Dependencies
check_for_dependencies () {
for i in ${DependArray[@]}; do
command -v "$i" >/dev/null 2>&1 || { echo >&2 "I require $i but it's not installed. Aborting."; exit 1; }
done
}
# **** List of Dependencies
declare -a DependArray=(
"rg"
"sk"
"mdcat"
"xclip"
)
# *** Set variables below main
setVars () {
readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
}
# **** Print Help
Help () {
echo
echo -e " \e[3m\e[1m NoteFind.sh \e[0m; Helpful Shell Scripts for Markdown Notes"
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[3m\e[1m• Usage \e[0m "
echo
echo -e " "${script_name}" [<path/to/notes>]"
echo -e " "${script_name}" [-h]"
echo -e " "${script_name}" [--help]"
echo
echo -e " \e[3m By Design: No Options; No other Arguments\e[0m"
echo
echo
echo -e " \e[1;91m \e[1m Binding \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;95m Ctrl - q \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Search \e[0m with \e[0m\e[3mripgrep\e[0m"
echo -e " \e[1;93m Ctrl - w \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Copy \e[0m the Full Path to the Clipboard"
echo -e " \e[1;93m Alt - w \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Copy \e[0m the Relative Path to the Clipboard"
echo -e " \e[1;94m Alt - e \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in Emacs"
echo -e " \e[1;94m Alt - v \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in VSCode"
echo -e " \e[1;94m Ctrl - o \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in Default Program"
echo
echo -e " \e[3m\e[1m• Compatability \e[0m "
echo
echo -e " Match highlighting occurs automatically if \e[1m\$SHELL\e[0m is \e[1m **/fish\e[0m"
echo -e " This uses \e[1mtmpfs\e[0m at \e[1m /dev/shm\e[0m, this should work on \e[3mArch\e[0m, \e[3mFedora\e[0m and \e[3mUbuntu\e[0m, I don't know about \e[3mMacOS\e[0m "
echo
}
# *** Read First Argument
readFirstArgument () {
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "" ]]; then
Help && exit 0
fi
}
# *** Skim and Grep, the important stuff # *** Skim and Grep, the important stuff
SkimAndGrep () { SkimNotes () {
## Change directory if One was specified, exit if no directory exists ## Change directory if One was specified, exit if no directory exists
if [ "${1:-}" != "" ]; then cd "${1}"
cd "${1}" || exit 4
fi
## If using fish, cleverness can be utilised to highlight matches. ## If using fish, cleverness can be utilised to highlight matches.
## fish only, not zsh or bash ## fish only, not zsh or bash
@@ -74,13 +140,7 @@ SkimGrepHighlightFish () {
## C-q toggles interactive ## C-q toggles interactive
## C-y Copies Full path to clipboard ## C-y Copies Full path to clipboard
} }
#
# *** Set variables below main
setVars () {
readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
}
# **** Skim with Grep # **** Skim with Grep
SkimGrep () { SkimGrep () {
@@ -95,93 +155,6 @@ SkimGrep () {
} }
# *** Interpret arguments
## TODO I should change this, the -d and -p options only complicate things
arguments () {
while test $# -gt 0
do
case "$1" in
-d) SkimAndGrep "${2}" && exit 0
;;
--d) SkimAndGrep $2 && exit 0
;;
--*) echo "bad option $1 in "${script_name}""
;;
-?) echo "(5) Unknown option $1 in "${script_name}""; Help; exit 1
;;
## ?*) echo -e "argument \e[1;35m${1}\e[0m has no definition laksdjfaklsdfj."
## ;;
esac
shift
done
}
# *** Check for Help # * Call Main Function
# **** Print Help
#
check_for_help () {
while test $# -gt 0
do
case "$1" in
--help) Help && exit 0
;;
-h) Help && exit 0
;;
esac
shift
done
}
Help () {
echo
echo -e " \e[3m\e[1m NoteFind.sh \e[0m; Helpful Shell Scripts for Markdown Notes"
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[3m\e[1m• Usage \e[0m "
echo
echo -e " "${script_name}" [-d <path/to/notes] [-h]"
echo -e " "${script_name}" [--dir <path/to/notes/] [--help]"
echo
echo
echo -e " \e[1;91m \e[1m Binding \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;95m Ctrl - q \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Search \e[0m with \e[0m\e[3mripgrep\e[0m"
echo -e " \e[1;93m Ctrl - w \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Copy \e[0m the Full Path to the Clipboard"
echo -e " \e[1;93m Alt - w \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Copy \e[0m the Relative Path to the Clipboard"
echo -e " \e[1;94m Alt - e \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in Emacs"
echo -e " \e[1;94m Alt - v \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in VSCode"
echo -e " \e[1;94m Ctrl - o \e[0m \e[1;34m ┊┊┊ \e[0m \e[1m Open \e[0m in Default Program"
echo
echo -e " \e[3m\e[1m• Compatability \e[0m "
echo
echo -e " Match Highligting occurs automatically if \e[1m\$SHELL\e[0m is \e[1m **/fish\e[0m"
echo -e " This uses \e[1mtmpfs\e[0m at \e[1m /dev/shm\e[0m, this should work on \e[3mArch\e[0m, \e[3mFedora\e[0m and \e[3mUbuntu\e[0m, I don't know about \e[3mMacOS\e[0m "
echo
}
# *** Check for Dependencies
check_for_dependencies () {
for i in ${DependArray[@]}; do
command -v "$i" >/dev/null 2>&1 || { echo >&2 "I require $i but it's not installed. Aborting."; exit 1; }
done
}
# **** List of Dependencies
declare -a DependArray=(
"rg"
"sk"
"mdcat"
"xclip"
)
## * Call Main Function
main "${@}" main "${@}"

View File

@@ -72,7 +72,7 @@ arguments () {
;; ;;
-h) mainHelp && exit 0 -h) mainHelp && exit 0
;; ;;
find) shift; NoteFind -d "${NOTES_DIR}" "${@:-}" ## Don't steal function name find) shift; NoteFind "${NOTES_DIR}" "${@:-}" ## Don't steal function name
;; ;;
search) echo "begin note search" search) echo "begin note search"
;; ;;