Minor fixes as well as clean-up surrounding internal functions.
This commit is contained in:
parent
405c1e7244
commit
e3a2efdb43
12 changed files with 218 additions and 134 deletions
243
daisy.source
243
daisy.source
|
|
@ -9,11 +9,11 @@
|
|||
# This file is also sourced in some of the scripts included within
|
||||
# lackadaisical for common functionality. Some of the shared functionality is
|
||||
# only included if sourced from one of the included scripts, though you are
|
||||
# free to bypass this by setting env variable DAISY_INTERNAL to 1.
|
||||
# free to bypass this by setting env variable LD_INTERNAL to 1.
|
||||
|
||||
if [[ $DAISY_INTERNAL -eq 1 ]];
|
||||
if [[ $LD_INTERNAL -eq 1 ]];
|
||||
then
|
||||
export DAISY_BIN=$(basename $0)
|
||||
export LD_BIN=$(basename $0)
|
||||
fi
|
||||
|
||||
# Variables for use in other utilities
|
||||
|
|
@ -32,47 +32,54 @@ function daisy_dependency_check
|
|||
echo $(($res ^ 1))
|
||||
}
|
||||
|
||||
DAISY_HAS_fzf=$(daisy_dependency_check fzf)
|
||||
DAISY_HAS_md5sum=$(daisy_dependency_check md5sum)
|
||||
DAISY_HAS_peco=$(daisy_dependency_check peco)
|
||||
DAISY_HAS_tree=$(daisy_dependency_check tree)
|
||||
LD_HAS_fzf=$(daisy_dependency_check fzf)
|
||||
LD_HAS_md5sum=$(daisy_dependency_check md5sum)
|
||||
LD_HAS_peco=$(daisy_dependency_check peco)
|
||||
LD_HAS_tree=$(daisy_dependency_check tree)
|
||||
|
||||
export DAISY_FOLDER=$(dirname $(realpath $arg))
|
||||
export DAISY_SOURCE_FILE=$(realpath $arg)
|
||||
export DAISY_AVAILABLE=0
|
||||
export LD_FOLDER=$(dirname $(realpath $arg))
|
||||
export LD_SOURCE_FILE=$(realpath $arg)
|
||||
export LD_AVAILABLE=0
|
||||
|
||||
# Config folder setup
|
||||
export DAISY_CONFIG_FOLDER="$HOME/.config/lackadaisical"
|
||||
export LD_CONFIG_FOLDER="$HOME/.config/lackadaisical"
|
||||
new_install=0
|
||||
|
||||
if [[ ! -d "$DAISY_CONFIG_FOLDER" ]];
|
||||
if [[ ! -d "$LD_CONFIG_FOLDER" ]];
|
||||
then
|
||||
# Create the folder with its basics
|
||||
mkdir -p "$DAISY_CONFIG_FOLDER"
|
||||
mkdir -p "$LD_CONFIG_FOLDER"
|
||||
daisy_help
|
||||
new_install=1
|
||||
fi
|
||||
|
||||
# Functions for aliases that are added once, but always available
|
||||
DAISY_ALIASFILE="$DAISY_CONFIG_FOLDER"/.daisy_aliases
|
||||
touch $DAISY_ALIASFILE
|
||||
# Multiple default source files
|
||||
# [LEA.TODO] Turn these into arrays
|
||||
LD_ALIASFILE="$LD_CONFIG_FOLDER/aliases.src"
|
||||
LD_EDITORFILE="$LD_CONFIG_FOLDER/editor.src"
|
||||
touch $LD_ALIASFILE
|
||||
touch $LD_EDITORFILE
|
||||
|
||||
# Source everything in the config folder
|
||||
for f in "$DAISY_CONFIG_FOLDER"; do
|
||||
[ -f "$file" ] && source "$f"
|
||||
done
|
||||
function _daisy_source_configs
|
||||
{
|
||||
for f in `find "$LD_CONFIG_FOLDER" -name "*.src" -type f`;
|
||||
do
|
||||
source "$f"
|
||||
done
|
||||
}
|
||||
|
||||
# Installation into PATH
|
||||
if [[ ! $PATH == *"$DAISY_FOLDER"* ]];
|
||||
if [[ ! $PATH == *"$LD_FOLDER"* ]];
|
||||
then
|
||||
export PATH="$PATH:$DAISY_FOLDER"
|
||||
export PATH="$PATH:$LD_FOLDER"
|
||||
fi
|
||||
|
||||
|
||||
# Set up the basic alias for `shrc`
|
||||
# Do not set these up if DAISY_INTERNAL=1 is set, or infinite recursion could
|
||||
# Do not set these up if LD_INTERNAL=1 is set, or infinite recursion could
|
||||
# occur!
|
||||
if [[ ! -v DAISY_INTERNAL ]];
|
||||
if [[ ! -v LD_INTERNAL ]];
|
||||
then
|
||||
alias shrc=". shrc"
|
||||
fi
|
||||
|
|
@ -114,12 +121,17 @@ alias lss="ls -a -l -S -r -h"
|
|||
# Simple version of `cdf`
|
||||
function cdf
|
||||
{
|
||||
if [[ $LD_HAS_fzf != 1 ]];
|
||||
then
|
||||
echo "This alias requires the utility 'fzf'. Please install it."
|
||||
return 1
|
||||
fi
|
||||
cd $(dirname $(fzf))
|
||||
}
|
||||
|
||||
function cdp
|
||||
{
|
||||
if [[ $DAISY_HAS_peco != 1 || $DAISY_HAS_tree != 1 ]];
|
||||
if [[ $LD_HAS_peco != 1 || $LD_HAS_tree != 1 ]];
|
||||
then
|
||||
echo "This alias requires the utilities 'peco' and 'tree'. Please install them."
|
||||
echo "Consider using 'cdf' instead."
|
||||
|
|
@ -130,12 +142,12 @@ function cdp
|
|||
|
||||
function editpeco
|
||||
{
|
||||
if [[ $DAISY_HAS_peco != 1 ]];
|
||||
if [[ $LD_HAS_peco != 1 || $LD_HAS_tree != 1 ]];
|
||||
then
|
||||
echo "This alias requires the utility 'peco'. Please install it."
|
||||
echo "This alias requires the utilities 'peco' and 'tree'. Please install them."
|
||||
echo "Consider using 'cdf' instead."
|
||||
return 1
|
||||
fi
|
||||
|
||||
tree --noreport -fia . | peco --prompt "Press CTRL+C to quit - query:" --exec "xargs -o -I{} $EDITOR {}"
|
||||
}
|
||||
|
||||
|
|
@ -155,7 +167,7 @@ function ched
|
|||
available_editors=()
|
||||
for editor in "${editors[@]}";
|
||||
do
|
||||
editor_real=$(command -v "$editor")
|
||||
editor_real=$(command -v "$editor")
|
||||
if command -v "$editor_real" >/dev/null 2>&1;
|
||||
then
|
||||
if [[ $(realpath "$EDITOR") == "$editor_real" ]];
|
||||
|
|
@ -174,7 +186,7 @@ function ched
|
|||
fi
|
||||
|
||||
# Present all choices
|
||||
choice=$(dialog --output-fd 1 --clear --title "Select Text Editor (Recommendation: nano)" \
|
||||
choice=$(dialog --output-fd 1 --clear --title "Select Text Editor" \
|
||||
--menu "Choose one of the installed text editors:" 15 50 6 \
|
||||
"${available_editors[@]}")
|
||||
dialog_ret=$?
|
||||
|
|
@ -185,12 +197,11 @@ function ched
|
|||
return
|
||||
fi
|
||||
|
||||
echo export EDITOR=$choice > "$DAISY_CONFIG_FOLDER/editor.src"
|
||||
echo export DAISY_EDITOR=$choice >> "$DAISY_CONFIG_FOLDER/editor.src"
|
||||
echo export DAISY_OLD_EDITOR=$EDITOR >> "$DAISY_CONFIG_FOLDER/editor.src"
|
||||
echo export EDITOR=$choice > "$LD_EDITORFILE"
|
||||
echo export LD_EDITOR=$choice >> "$LD_EDITORFILE"
|
||||
echo export LD_OLD_EDITOR=$EDITOR >>"$LD_EDITORFILE"
|
||||
|
||||
# Seems silly but this is also where we should export these
|
||||
source "$DAISY_CONFIG_FOLDER/editor.src"
|
||||
_daisy_source_configs
|
||||
}
|
||||
|
||||
function wait_for_editor
|
||||
|
|
@ -212,44 +223,60 @@ function wait_for_editor
|
|||
done
|
||||
}
|
||||
|
||||
function daisy_reload
|
||||
{
|
||||
FROM_RC=0 source "$LD_SOURCE_FILE"
|
||||
}
|
||||
|
||||
function ldrc
|
||||
{
|
||||
FROM_RC=0 $EDITOR "$DAISY_SOURCE_FILE"
|
||||
source "$DAISY_SOURCE_FILE"
|
||||
$EDITOR "$LD_SOURCE_FILE"
|
||||
FROM_RC=0 source "$LD_SOURCE_FILE"
|
||||
}
|
||||
|
||||
function daisy_enc
|
||||
{
|
||||
has_file=$([[ ! -z $1 ]] && file $1 1>/dev/null; echo $?)
|
||||
has_file=$([[ has_file -eq 0 ]] && echo 1)
|
||||
file_info="no data"
|
||||
file_name="null"
|
||||
if [[ has_file -eq 1 ]];
|
||||
if [ -d $1 ];
|
||||
then
|
||||
echo -e "daisy_create_folder=$1"
|
||||
else
|
||||
file_info=$(file $1)
|
||||
file_dir=$(dirname $1)
|
||||
file_name=$(basename $1)
|
||||
|
||||
base64_inner=$(cat ${1:-/dev/stdin} | base64 | tr -d '\n')
|
||||
|
||||
# Print out our block
|
||||
echo -e "daisy_folder_$file_name=$file_dir"
|
||||
echo -e "daisy_data_base64_$file_name=\"$base64_inner\""
|
||||
fi
|
||||
|
||||
base64_inner=$(cat ${1:-/dev/stdin} | base64 | tr -d '\n')
|
||||
|
||||
# Print out our block
|
||||
echo -e "# File info: $file_info"
|
||||
echo -e "daisy_data_base64_$file_name=\"$base64_inner\""
|
||||
}
|
||||
|
||||
# Will only take input files, always outputs to stdout
|
||||
function daisy_enc_multi
|
||||
{
|
||||
for file in "$@"; do
|
||||
if [[ -f "$file" ]]; then
|
||||
daisy_enc "$file"
|
||||
echo # separate blocks with a newline
|
||||
else
|
||||
echo "daisy_enc_multi: Skipping non-file: $file"
|
||||
fi
|
||||
daisy_enc "$file"
|
||||
done
|
||||
}
|
||||
|
||||
function daisy_enc_folder
|
||||
{
|
||||
if [[ $LD_HAS_tree != 1 ]];
|
||||
then
|
||||
echo "This function requires the utiliy 'tree'. Please install it."
|
||||
return 1
|
||||
fi
|
||||
|
||||
dir="$1"
|
||||
cd "$dir"
|
||||
tree -fia --noreport . | sed 1d | while read -r item;
|
||||
do
|
||||
daisy_enc "$item"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
function daisy_dec
|
||||
{
|
||||
data=$(cat ${1:-/dev/stdin} | grep -v "#" )
|
||||
|
|
@ -262,16 +289,28 @@ function daisy_dec_multi
|
|||
{
|
||||
[[ ! -f $1 ]] && echo "daisy_dec_multi: No input file specified" && return
|
||||
[[ ! -d $2 ]] && echo "daisy_dec_multi: No output directory specified" && return
|
||||
declare -a vars=( $(cat $1 | grep -v "# File") )
|
||||
for enc in "${vars[@]}";
|
||||
do
|
||||
file=$(echo -e "$enc" | cut -d "_" -f 4- | cut -d "=" -f 1)
|
||||
|
||||
if [[ ! "$file" == '' ]]
|
||||
then
|
||||
daisy_dec <(echo "$enc") > "$2"/"$file"
|
||||
fi
|
||||
done
|
||||
folder=
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" == "daisy_create_folder="* ]];
|
||||
then
|
||||
folder=$(echo $line | cut -d "=" -f 2)
|
||||
mkdir -p "$2/$folder"
|
||||
fi
|
||||
|
||||
if [[ "$line" == "daisy_folder"* ]];
|
||||
then
|
||||
folder=$(echo -e $line | cut -d "=" -f 2)
|
||||
continue
|
||||
fi
|
||||
|
||||
if [[ "$line" == "daisy_data_base64"* ]];
|
||||
then
|
||||
file=$(echo -e $line | cut -d "_" -f 4- | cut -d "=" -f 1)
|
||||
daisy_dec <(echo $line) > "$2/$folder/$file"
|
||||
fi
|
||||
|
||||
done <<< $(cat "$1")
|
||||
}
|
||||
|
||||
# Saves a bit on typing
|
||||
|
|
@ -285,6 +324,12 @@ function daisy_unalias
|
|||
{
|
||||
unalias_param=$@
|
||||
|
||||
if [[ $unalias_param =~ '^[0-9]+$' ]]; then
|
||||
selection=$(head -$unalias_param "$LD_ALIASFILE" | tail -1 | cut -d "=" -f 1 | grab 2)
|
||||
daisy_unalias $selection
|
||||
return
|
||||
fi
|
||||
|
||||
if [[ -z $unalias_param ]]; then
|
||||
return
|
||||
fi
|
||||
|
|
@ -292,8 +337,9 @@ function daisy_unalias
|
|||
unalias $@ 2>/dev/null
|
||||
|
||||
# Remove from aliases list
|
||||
newdata=$(cat $DAISY_ALIASFILE | grep -v "alias $unalias_param")
|
||||
echo -e $newdata > $DAISY_ALIASFILE
|
||||
newdata=$(cat "$LD_ALIASFILE" | grep -v "alias $unalias_param")
|
||||
bak "$LD_ALIASFILE" 1>/dev/null
|
||||
echo -e $newdata > "$LD_ALIASFILE"
|
||||
}
|
||||
|
||||
function daisy_alias
|
||||
|
|
@ -301,31 +347,38 @@ function daisy_alias
|
|||
alias_param="$@"
|
||||
|
||||
if [[ -z $alias_param ]]; then
|
||||
linenum=1
|
||||
echo "Active lackadaisical alias lines:"
|
||||
cat $DAISY_ALIASFILE | sed 's/alias //g'
|
||||
while IFS= read -r line; do
|
||||
line=$(echo "$line" | sed 's/alias / /g')
|
||||
echo "$linenum: $line"
|
||||
linenum=$(($linenum + 1))
|
||||
done < "$LD_ALIASFILE"
|
||||
return
|
||||
fi
|
||||
|
||||
# Plain name and contents
|
||||
alias_name=$(echo -e $alias_param | grep -o ".*=" | tr --delete =)
|
||||
|
||||
if [[ $alias_name =~ '^[0-9]+$' ]]; then
|
||||
echo "An alias cannot start with a number! Exiting."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Make persistent
|
||||
daisy_unalias $alias_name
|
||||
echo alias ${alias_param%=*}"="\"${alias_param#*=}\" >> $DAISY_ALIASFILE
|
||||
|
||||
alias $alias_param
|
||||
echo alias ${alias_param%=*}"="\"${alias_param#*=}\" >> $LD_ALIASFILE
|
||||
source $LD_ALIASFILE
|
||||
}
|
||||
|
||||
source $DAISY_ALIASFILE
|
||||
|
||||
alias daisy_init='source "$DAISY_SOURCE_FILE"'
|
||||
_daisy_source_configs
|
||||
|
||||
###############################################################################
|
||||
# end of FUNCTIONS and ALIASES ################################################
|
||||
###############################################################################
|
||||
|
||||
# End of user section!
|
||||
export DAISY_AVAILABLE=1
|
||||
export LD_AVAILABLE=1
|
||||
|
||||
# Start of internal section
|
||||
function daisy_quit_if_no
|
||||
|
|
@ -343,28 +396,27 @@ function daisy_quit_if_no
|
|||
|
||||
if [[ $has_dep -eq 0 ]];
|
||||
then
|
||||
echo "$DAISY_BIN: The dependency $1 was not found! Please install it" \
|
||||
echo "$LD_BIN: The dependency $1 was not found! Please install it" \
|
||||
"to be able to use this utility!"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
[ -d "$DAISY_FOLDER" ] && export DAISY_AVAILABLE=1
|
||||
[ -d "$LD_FOLDER" ] && export LD_AVAILABLE=1
|
||||
|
||||
# Help function, courtesy of Google Gemini
|
||||
function daisy_help() {
|
||||
local target_tool="$1"
|
||||
local file="$DAISY_FOLDER/README.md"
|
||||
|
||||
# 1. Extract the block between the new headers
|
||||
sed -n '/--- BEGIN OF DAISY HELP ---/,/--- END OF DAISY HELP ---/{//!p;}' "$file" | \
|
||||
local target_tool="$1"
|
||||
local file="$LD_FOLDER/README.md"
|
||||
|
||||
# 1. Extract the block between the new headers
|
||||
sed -n '/--- BEGIN OF DAISY HELP ---/,/--- END OF DAISY HELP ---/{//!p;}' "$file" |
|
||||
if [ -z "$target_tool" ]; then
|
||||
# If no argument, print the whole help text
|
||||
cat
|
||||
# If no argument, print the whole help text
|
||||
cat
|
||||
else
|
||||
# 2. Parse specific tool
|
||||
awk -v query="$target_tool" '
|
||||
# 2. Parse specific tool
|
||||
awk -v query="$target_tool" '
|
||||
BEGIN { found=0; printing=0 }
|
||||
|
||||
# Match lines defining tools (e.g., " - calm:" or " - bak/unbak:")
|
||||
|
|
@ -415,18 +467,31 @@ function daisy_help() {
|
|||
|
||||
# Courtesy of Google Gemini
|
||||
daisy_list() {
|
||||
local file="$DAISY_FOLDER/README.md"
|
||||
echo "Available LACKADAISICAL commands:"
|
||||
local file="$LD_FOLDER/README.md"
|
||||
echo "Available LACKADAISICAL commands:"
|
||||
|
||||
# Extract block -> Find tool lines -> Clean formatting -> Print
|
||||
sed -n '/--- BEGIN OF DAISY HELP ---/,/--- END OF DAISY HELP ---/{//!p;}' "$file" | \
|
||||
# Extract block -> Find tool lines -> Clean formatting -> Print
|
||||
sed -n '/--- BEGIN OF DAISY HELP ---/,/--- END OF DAISY HELP ---/{//!p;}' "$file" |
|
||||
awk '
|
||||
/^[[:space:]]*- / {
|
||||
# Remove indentation and "- "
|
||||
sub(/^[[:space:]]*- /, "");
|
||||
sub(/^[[:space:]]*- /, "");
|
||||
# Remove trailing ":"
|
||||
sub(/:[[:space:]]*$/, "");
|
||||
sub(/:[[:space:]]*$/, "");
|
||||
print " " $0
|
||||
}
|
||||
' | sort
|
||||
}
|
||||
|
||||
# Hide what we don't need
|
||||
if [[ ! -v LD_INTERNAL ]];
|
||||
then
|
||||
unset -f _daisy_source_configs
|
||||
unset -f wait_for_editor
|
||||
unset -f daisy_quit_if_no
|
||||
unset -f daisy_dependency_check
|
||||
unset LD_HAS_fzf
|
||||
unset LD_HAS_peco
|
||||
unset LD_HAS_md5sum
|
||||
unset LD_HAS_tree
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue