1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-08-05 21:58:05 +02:00

Added Template, restructured Scripts

This commit is contained in:
Ryan Greenup
2020-07-14 23:12:56 +10:00
parent 47c20ef11e
commit c3519e16d6
4 changed files with 40 additions and 154 deletions

View File

@@ -1,122 +0,0 @@
#! /usr/bin/env bash
#
# Author: Ryan Greenup <ryan.greenup@protonmail.com>
# * Shell Settings
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# * Main Function
main() {
setVars
arguments "${@}"
SkimAndGrep
}
# ** Helper Functions
# *** Skim and Grep, the important stuff
SkimAndGrep () {
command -v xclip >/dev/null 2>&1 || { echo >&2 "I require xclip but it's not installed. Install it with something like pacman -S xclip Aborting."; exit 1; }
command -v rg >/dev/null 2>&1 || { echo >&2 "I require ripgrep, with the binary rg in the path, but it's not installed, install it with ~cargo install rg~. Aborting."; exit 1; }
command -v mdcat >/dev/null 2>&1 || { echo >&2 "I require mdcat but it's not installed. Install it with ~cargo install mdcat~ Aborting."; exit 1; }
command -v sk >/dev/null 2>&1 || { echo >&2 "I require skim, with the binary sk in the path, but it's not installed. Install it with ~cargo install skim~ Aborting."; exit 1; }
## If using fish, cleverness can be utilised to highlight matches.
## fish only, not zsh or bash
##
if [[ "$(basename $SHELL)" == "fish" ]]; then
ramtmp="$(mktemp -p /dev/shm/)"
sk -c "echo {} > "${ramtmp}" ; rg -t markdown -l --ignore-case (cat "${ramtmp}")" \
--preview "mdcat {} 2> /dev/null | \
rg -t markdown --colors 'match:bg:yellow' \
--no-line-number --ignore-case --pretty --context 20 (cat "${ramtmp}")" \
--bind 'ctrl-f:interactive,pgup:preview-page-up,pgdn:preview-page-down' \
--bind 'ctrl-w:execute-silent(echo {} | xargs realpath | xclip -selection clipboard),alt-w:execute-silent(echo {} | xclip -selection clipboard)' \
--bind 'alt-v:execute-silent(code {}),alt-e:execute-silent(emacs {}),ctrl-o:execute-silent(xdg-open {})' \
--bind 'alt-y:execute-silent(cat {} | xclip -selection clipboard)' \
--bind 'alt-o:execute-silent(cat {} | pandoc -f markdown -t html --mathml | xclip -selection clipboard)' \
--bind 'alt-f:execute-silent(echo {} | xargs dirname | xargs cd; cat {} | pandoc -f markdown -t dokuwiki --mathml | xclip -selection clipboard)' \
## TODO This should be emacsclient
## TODO This should be emacsclient
## Add -i to make it interactive from the start
## C-q toggles interactive
## C-y Copies Full path to clipboard
exit 0
else
sk --ansi -c 'rg -l -t markdown --ignore-case "{}"' --preview "mdcat {}" \
--bind 'ctrl-f:interactive,pgup:preview-page-up,pgdn:preview-page-down,ctrl-w:execute-silent(echo {} | xargs realpath | xclip -selection clipboard),alt-w:execute-silent(echo {} | xclip -selection clipboard)'
--bind 'ctrl-f:interactive,pgup:preview-page-up,pgdn:preview-page-down' \
--bind 'ctrl-w:execute-silent(echo {} | xargs realpath | xclip -selection clipboard),alt-w:execute-silent(echo {} | xclip -selection clipboard)' \
--bind 'alt-v:execute-silent(code {}),alt-e:execute-silent(emacs {}),ctrl-o:execute-silent(xdg-open {})' \
--bind 'alt-p:execute-silent(marktext {} 2> /dev/null)'
fi
}
#
#
# *** Set variables almost globally
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)
}
## *** Interpret arguments
arguments () {
while test $# -gt 0
do
case "$1" in
--help) Help
;;
-h) Help
;;
--*) echo "bad option $1 in "${script_name}""
;;
*) echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
## *** 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[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;93m Ctrl - w \e[0m \e[1;34m ┊┊┊ \e[0m Copy the Full Path to the Clipboard"
echo -e " \e[1;93m Alt - w \e[0m \e[1;34m ┊┊┊ \e[0m Copy the Relative Path to the Clipboard"
echo -e " \e[1;32m Alt - e \e[0m \e[1;34m ┊┊┊ \e[0m Open in Emacs"
echo -e " \e[1;32m Alt - v \e[0m \e[1;34m ┊┊┊ \e[0m Open in VSCode"
echo -e " \e[1;32m Ctrl - o \e[0m \e[1;34m ┊┊┊ \e[0m Open in Default Program"
echo -e " \e[1;33m Ctrl - q \e[0m \e[1;34m ┊┊┊ \e[0m Toggle Searching with ripgrep"
echo
echo -e " \e[3m\e[1m• Compatability \e[0m "
echo
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
exit 0
}
## * Call Main Function
main "${@}"

View File

@@ -10,6 +10,7 @@ set -o pipefail # don't hide errors within pipes
# * Main Function # * Main Function
main() { main() {
setVars setVars
check_for_dependencies
arguments "${@}" arguments "${@}"
SkimAndGrep SkimAndGrep
@@ -66,14 +67,14 @@ SkimAndGrep () {
# #
# #
# *** Set variables almost globally # *** Set variables below main
setVars () { setVars () {
readonly script_name=$(basename "${0}") readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces) IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
} }
## *** Interpret arguments # *** Interpret arguments
arguments () { arguments () {
while test $# -gt 0 while test $# -gt 0
do do
@@ -91,7 +92,7 @@ arguments () {
done done
} }
## *** Print Help # *** Print Help
Help () { Help () {
@@ -118,5 +119,26 @@ Help () {
exit 0 exit 0
} }
# *** 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 ## * Call Main Function
main "${@}" main "${@}"

View File

@@ -98,7 +98,7 @@ arguments () {
function NoteFind() { function NoteFind() {
bash "NoteFind.sh "${@:-}"" NoteFind.sh "${@:-}"
} }
mytest() { mytest() {

42
cadmus Executable file → Normal file
View File

@@ -1,19 +1,15 @@
#! /usr/bin/env bash #! /usr/bin/env bash
# #
# Author: Bert Van Vreckem <bert.vanvreckem@gmail.com> # Author: Ryan Greenup <ryan.greenup@protonmail.com>
#
#{{{ Bash settings
# abort on nonzero exitstatus # abort on nonzero exitstatus
set -o errexit set -o errexit
# abort on unbound variable # abort on unbound variable
set -o nounset set -o nounset
# don't hide errors within pipes # don't hide errors within pipes
set -o pipefail set -o pipefail
#}}}
#{{{ Variables
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
#}}}
## * Main Functions
main() { main() {
@@ -24,6 +20,7 @@ main() {
function setvars() { function setvars() {
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
readonly script_name=$(basename "${0}") readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
readonly TERMINAL="kitty" readonly TERMINAL="kitty"
@@ -33,10 +30,12 @@ function setvars() {
} }
# * Sub-functions
# ** Help
function mainHelp() { function mainHelp() {
## echo -e " \u001b[45;1m \e[1;31m -------------------------\e[0m \u001b[0m \e[1;31m find \e[0m \e[1;34m"
##
echo echo
echo -e " \e[3m\e[1m Cadmus\e[0m; Helpful Shell Scripts for Markdown Notes" echo -e " \e[3m\e[1m Cadmus\e[0m; Helpful Shell Scripts for Markdown Notes"
echo -e " \e[1;31m -------------------------\e[0m " echo -e " \e[1;31m -------------------------\e[0m "
@@ -57,8 +56,8 @@ function mainHelp() {
echo echo
} }
#{{{ Helper functions # ** Main Arguments - Level 0
#
arguments () { arguments () {
while test $# -gt 0 while test $# -gt 0
@@ -95,12 +94,11 @@ arguments () {
done done
} }
function NoteFind() { # *** Find
## sk --ansi -i -c 'rg -l -t markdown "{}"' --preview "mdcat {}" \
## --bind pgup:preview-page-up,pgdn:preview-page-down
bash ""$script_dir"/NoteFind.sh" "${@:-}" ## TODO (2.) is related; do we call it this way? function NoteFind() {
## TODO 4. Currently this only works if called from the current directory
bash "NoteFind.sh "${@:-}""
} }
mytest() { mytest() {
@@ -121,18 +119,6 @@ Help () {
exit 0 exit 0
} }
#}}}
main "${@}" main "${@}"
# cursor: 33 del
## TODO 1. Should interactive elements of scripts be preserved and use the clipboard
## As opposed to having no feedback and just operating on STDIN and STDOUT.
## Could a similar effect be acheived using /tmp or /dev/shm?
## TODO 2. Should subscripts be exptected to be relative or absolute?
## TODO 3. Should the Notes directory be a global variable or an argument
## TODO Should we set the Notes directory in a TOML?
## https://github.com/freshautomations/stoml
## TODO Why do I keep getting Argument has no definition?
## TODO
## TODO