Complete overhaul of how daisy core tools are called, as well as changes to the help system

This commit is contained in:
Sam Hardeman 2026-03-30 01:48:18 +02:00
parent 74ed7582f9
commit 3fa3daacba
9 changed files with 1263 additions and 682 deletions

View file

@ -1,3 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env -S echo "This file can only be sourced, not run stand-alone."
# LACKADAISICAL SOURCE-ABLE FILE
@ -63,21 +64,20 @@ export LD_AVAILABLE=0
# Config folder setup
export LD_CONFIG_FOLDER="$HOME/.config/lackadaisical"
new_install=0
export LD_NEW_INSTALL=
if [[ ! -d "$LD_CONFIG_FOLDER" ]];
then
# Create the folder with its basics
mkdir -p "$LD_CONFIG_FOLDER"
daisy_help
new_install=1
fi
# Multiple default source files
# [LEA.TODO] Turn these into arrays
LD_ALIASFILE="$LD_CONFIG_FOLDER/aliases.src"
LD_EDITORFILE="$LD_CONFIG_FOLDER/editor.src"
LD_ESOURCEFILE="$LD_CONFIG_FOLDER/extra.src"
export LD_ALIASFILE="$LD_CONFIG_FOLDER/aliases.src"
export LD_EDITORFILE="$LD_CONFIG_FOLDER/editor.src"
export LD_ESOURCEFILE="$LD_CONFIG_FOLDER/extra.src"
touch $LD_ALIASFILE
touch $LD_EDITORFILE
touch $LD_ESOURCEFILE
@ -90,8 +90,8 @@ ld_dbg cat $LD_ESOURCEFILE
# Source everything in the config folder
function _daisy_source_configs
{
while IFS= read -r -d '' f; do
source "$f"
while IFS= read -r -d '' f; do
source "$f"
done < <(find "$LD_CONFIG_FOLDER" -name "*.src" -type f -print0)
}
@ -101,7 +101,6 @@ then
export PATH="$PATH:$LD_FOLDER"
fi
# Set up the basic alias for `shrc`
# Do not set these up if LD_INTERNAL=1 is set, or infinite recursion could
# occur!
@ -114,26 +113,6 @@ fi
# FUNCTIONS and ALIASES #######################################################
###############################################################################
# Undocumented but internally used
function daisy_wait_for_editor
{
pname="$1"
fname="$2"
# Give some time for a process to launch
sleep 1
while true;
do
alive=$(pgrep -f "$pname.*$fname")
if [[ $alive == "" ]]
then
break
fi
sleep 1
done
}
function multicd
{
cdpath="$@"
@ -158,16 +137,6 @@ function multicd
alias cd=multicd
# Undocumented but internally used
function daisy_editor
{
editor=${LD_EDITOR:-$EDITOR};
ld_dbg echo Opening $editor to edit file: $1
$editor "$1"
sleep 1
daisy_wait_for_editor $editor "$1"
}
# bak and unbak
function bak
{
@ -319,158 +288,16 @@ function ched
source "$LD_EDITORFILE"
}
function daisy_reload
{
LD_INTERNAL=0 source "$LD_SOURCE_FILE"
}
function ldrc
{
ARG=$1
SOURCE="$LD_SOURCE_FILE"
[[ "$ARG" == "-e" ]] && SOURCE="$LD_ESOURCEFILE"
daisy_editor "$SOURCE"
daisy editor "$SOURCE"
LD_INTERNAL=0 source "$SOURCE"
}
enc_is_folder=0
function daisy_enc
{
if [ -t 0 ] && [ -z "$1" ];
then
echo "# $0: No arguments or stdin specified!"
return 1;
fi
if [ ! -t 0 ] && [ -z "$1" ];
then
echo "# $0: Please provide a filename as argument when using stdin"
return 1;
fi
if [ -n "$1" ] && [ -d "$1" ];
then
echo -e "daisy_create_folder=$1"
else
file_dir=""
file_name=""
perms=755
target=$1
# [TODO, FIX: An unknown bug is causing daisy_enc_folder to misbehave if stdin is accessed, so we disable it here.]
if [[ ! -t 0 ]] && [[ $enc_is_folder == 0 ]];
then
file_dir="."
file_name="$1"
shift
elif [ -f "$1" ];
then
file_dir=$(dirname "$1")
file_name=$(basename "$1")
perms=$(stat -c %a "$1")
else
echo "# $0: An error occured during encoding."
return 1
fi
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\""
echo -e "daisy_perms_$file_name=$perms"
fi
}
# Will only take input files, always outputs to stdout
function daisy_enc_multi
{
for file in "$@"; do
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
enc_is_folder=1
daisy_enc "$item"
enc_is_folder=0
done
}
function daisy_dec
{
if [ -t 0 ] && [ -z "$1" ];
then
echo "$0: No arguments or stdin specified!"
return 1;
fi
data=$(cat ${1:-/dev/stdin} | grep -v "#" )
echo -e "$data" | cut -d "=" -f 2- | cut -b 2- | head -c -2 | base64 -d
}
# Will only take a file and directory, sources it to find all encoded data
# Extracts to the directory
function daisy_dec_multi
{
arg1=$1
arg2=$2
# Handle stdin support
if [ ! -t 0 ];
then
arg2=$1
arg1=/dev/stdin
fi
[[ -t 0 ]] && [[ ! -f $arg1 ]] && echo "daisy_dec_multi: No input file specified" && return
[[ ! -d $arg2 ]] && echo "daisy_dec_multi: No output directory specified" && return
folder=
while IFS= read -r line; do
if [[ "$line" == "daisy_create_folder="* ]];
then
folder=$(echo $line | cut -d "=" -f 2)
echo $folder
mkdir -p "$arg2/$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)
mkdir -p "$arg2/$folder"
daisy_dec <(echo $line) > "$arg2/$folder/$file"
fi
if [[ "$line" == "daisy_perms"* ]];
then
file=$(echo -e $line | cut -d "_" -f 3- | cut -d "=" -f 1)
perms=$(echo -e $line | cut -d "_" -f 3- | cut -d "=" -f 2)
chmod $perms "$arg2/$folder/$file"
fi
done <<< $(cat "$arg1")
}
# Saves a bit on typing
function grab
{
@ -496,95 +323,13 @@ function clip
echo "Variable set to \"$LD_CLIP\"."
}
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
unalias $@ 2>/dev/null
# Remove from aliases list
newdata=$(cat "$LD_ALIASFILE" | grep -v "alias $unalias_param")
[[ NO_BAK -lt 1 ]] && bak "$LD_ALIASFILE" 1>/dev/null
echo -e $newdata > "$LD_ALIASFILE"
}
function daisy_alias
{
alias_param="$@"
if [[ -z $alias_param ]]; then
linenum=1
echo "Active lackadaisical alias lines:"
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#*=}\" >> $LD_ALIASFILE
source $LD_ALIASFILE
}
function daisy_backup
{
for f in `find "$LD_CONFIG_FOLDER" -name "*.src" -type f`;
do
bak "$f"
done
}
function daisy_clear
{
daisy_backup
for f in `find "$LD_CONFIG_FOLDER" -name "*.src" -type f`;
do
echo "Removing config file: $f"
rm -rf "$f"
done
echo "Config cleared. Use 'daisy_restore' if you would like to undo this."
daisy_reload
}
function daisy_restore
{
for f in `find "$LD_CONFIG_FOLDER" -name "*.src" -type f`;
do
unbak "$f"
bak "$f" 1>/dev/null
done
echo "Config restored. Backups have been retained."
daisy_reload
}
# Aliases for front-facing daisy_ functions
# Aliases for front-facing daisy commands
function _daisy_def_alias
{
alias ld_$1=daisy_$1
# Map underscores in name to spaces in command if needed
local cmd=$(echo $1 | tr '_' ' ')
alias ld_$1="daisy $cmd"
alias daisy_$1="daisy $cmd"
}
_daisy_def_alias reload
@ -598,97 +343,31 @@ _daisy_def_alias unalias
_daisy_def_alias backup
_daisy_def_alias clear
_daisy_def_alias restore
_daisy_def_alias combine
_daisy_def_alias help
_daisy_def_alias list
_daisy_source_configs
###############################################################################
# check for dependencies @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
###############################################################################
if [[ $new_install -eq 1 ]];
then
daisy check
fi
###############################################################################
# end of FUNCTIONS and ALIASES ################################################
###############################################################################
source "/etc/lackadaisical/daisy.command.source"
###############################################################################
# Autocomplete for `daisy` command ############################################
###############################################################################
# End of user section!
export LD_AVAILABLE=1
[ -d "$LD_FOLDER" ] && export LD_AVAILABLE=1
# Help function, courtesy of Google Gemini
function daisy_help()
{
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
else
# 2. Parse specific tool
awk -v query="$target_tool" '
BEGIN { found=0; printing=0 }
# Match lines defining tools (e.g., " - calm:" or " - bak/unbak:")
$0 ~ /^[[:space:]]*- / {
printing=0 # Stop printing previous tool
# Clean the line to get the "signature"
# " - bak/unbak:" becomes "bak/unbak"
sig = $0
sub(/^[[:space:]]*- /, "", sig)
sub(/:[[:space:]]*$/, "", sig)
# Check for exact match OR match within a slash-separated list
# This handles "bak", "unbak", and "daisy_alias"
split(sig, names, "/")
is_match = 0
if (sig == query) is_match = 1
else {
for (i in names) {
if (names[i] == query) { is_match = 1; break }
}
}
if (is_match) {
printing=1
found=1
print $0 # Print the header line (e.g., " - bak/unbak:")
next
}
}
# Print description lines if we are in a "found" block
printing {
# Stop if we hit the start of the NEXT tool
if ($0 ~ /^[[:space:]]*- /) { printing=0; next }
print
}
END {
if (found == 0) {
print "Tool '"'"'" query "'"'"' not found in README.md."
}
}
'
fi
}
# Courtesy of Google Gemini
daisy_list()
{
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" |
awk '
/^[[:space:]]*- / {
# Remove indentation and "- "
sub(/^[[:space:]]*- /, "");
# Remove trailing ":"
sub(/:[[:space:]]*$/, "");
print " " $0
}
' | sort
}