#!/bin/echo "This file can only be sourced, not run stand-alone." -- #!/bin/bash # LACKADAISICAL SOURCE-ABLE FILE # Source this in your RC file or manually to receive some of the simpler # utilities, as well as aliases for `shrc` and `cdf`. Set env variable # FROM_RC to 1 when sourcing this file to get RC-related functionality: # FROM_RC=1 source /daisy.source # 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. if [[ $DAISY_INTERNAL -eq 1 ]]; then export DAISY_BIN=$(basename $0) fi # Variables for use in other utilities # Find the right argument for our folder ARG=$0 if [[ ! $ARG == *daisy.source* ]]; then ARG="${BASH_SOURCE[0]}" fi # Check for dependencies function daisy_dependency_check { command -v $1 1>/dev/null 2>/dev/null; res=$? 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) export DAISY_FOLDER=$(dirname $(realpath $ARG)) export DAISY_SOURCE_FILE=$(realpath $ARG) export DAISY_AVAILABLE=0 # Config folder setup export DAISY_CONFIG_FOLDER="$HOME/.config/lackadaisical" NEW_INSTALL=0 if [[ ! -d "$DAISY_CONFIG_FOLDER" ]]; then # Create the folder with its basics mkdir -p "$DAISY_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 # Source everything in the config folder for f in "$DAISY_CONFIG_FOLDER"; do [ -f "$file" ] && source "$f" done # Installation into PATH if [[ ! $PATH == *"$DAISY_FOLDER"* ]]; then export PATH="$PATH:$DAISY_FOLDER" fi # Set up the basic alias for `shrc` # Do not set these up if DAISY_INTERNAL=1 is set, or infinite recursion could # occur! if [[ ! -v DAISY_INTERNAL ]]; then alias shrc=". shrc" fi ############################################################################### # FUNCTIONS and ALIASES ####################################################### ############################################################################### # bak and unbak function bak { # Input: target=$1 # Check if file exists if ! test -f "$target"; then echo "Path not found: \"$target\"" return 2 fi # Handle both cases if [[ UNBAK_MODE -eq 1 ]]; then cp -R "$target.bak" "$target" rm -rf "$target.bak" echo "Restored backup: $target <-- $target.bak" else cp -R "$target" "$target.bak" echo "Backup made: $target --> $target.bak" fi } alias unbak="UNBAK_MODE=1 bak" alias lsa="ls -a -l -h" alias lsn="ls -a -l -tu -r -h" alias lss="ls -a -l -S -r -h" # Simple version of `cdf` function cdf { cd $(dirname $(fzf)) } function cdp { if [[ $DAISY_HAS_peco != 1 || $DAISY_HAS_tree != 1 ]]; then echo "This alias requires the utility 'peco' 'tree'. Please install them." echo "Consider using 'cdf' instead." return 1 fi cd $(dirname $(tree -fia --noreport . | peco)) } function editpeco { if [[ $DAISY_HAS_peco != 1 ]]; then echo "This alias requires the utility 'peco'. Please install it." return 1 fi tree --noreport -fia . | peco --prompt "Press CTRL+C to quit - query:" --exec "xargs -o -I{} $EDITOR {}" } # for convenience purposes function editbin { editx $(which $1) } # sets a new editor based on commony available ones, and some visual ones function ched { editors=("nano" "vim" "nvim" "vi" "emacs" "gedit" "kate" "mousepad" "micro" \ "code" "subl" "joe" "kwrite" "gnome-text-editor") # Find which editors are installed available_editors=() for editor in "${editors[@]}"; do EDITOR_REAL=$(command -v "$editor") if command -v "$EDITOR_REAL" >/dev/null 2>&1; then if [[ $(realpath "$EDITOR") == "$EDITOR_REAL" ]]; then available_editors+=("$EDITOR_REAL" "$editor (current choice)") else available_editors+=("$EDITOR_REAL" "$editor") fi fi done if [[ ! -z $@ ]]; then TEXT="$@" dialog --msgbox "$TEXT" 0 0 fi # Present all choices CHOICE=$(dialog --output-fd 1 --clear --title "Select Text Editor (Recommendation: nano)" \ --menu "Choose one of the installed text editors:" 15 50 6 \ "${available_editors[@]}") DIALOG_RET=$? if [ $DIALOG_RET -ne 0 ]; then echo "No editor selected." 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" # Seems silly but this is also where we should export these source "$DAISY_CONFIG_FOLDER/editor.src" } function wait_for_editor { pname="$1" fname="$2" # Give some time for a process to launch sleep 1 while true; do ALIVE=$(ps aux | grep $fname | grep $pname) if [[ $ALIVE == "" ]] then break fi sleep 1 done } function ldrc { FROM_RC=0 $EDITOR "$DAISY_SOURCE_FILE" source "$DAISY_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 ]]; then file_info=$(file $1) file_name=$(basename $1) 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 done } function daisy_dec { 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 { [[ ! -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 } # Saves a bit on typing function grab { [[ -z $@ ]] && return; awk '{print $'$1'}' } function daisy_unalias { unalias_param=$@ if [[ -z $unalias_param ]]; then return fi unalias $@ 2>/dev/null # Remove from aliases list newdata=$(cat $daisy_aliasfile | grep -v "alias $unalias_param") echo -e $newdata > $daisy_aliasfile } function daisy_alias { alias_param="$@" if [[ -z $alias_param ]]; then echo "Active lackadaisical alias lines:" cat $daisy_aliasfile | sed 's/alias //g' return fi # Plain name and contents alias_name=$(echo -e $alias_param | grep -o ".*=" | tr --delete =) # Make persistent daisy_unalias $alias_name echo alias ${alias_param%=*}"="\"${alias_param#*=}\" >> $daisy_aliasfile alias $alias_param } source $daisy_aliasfile alias daisy_init='source "$DAISY_SOURCE_FILE"' ############################################################################### # end of FUNCTIONS and ALIASES ################################################ ############################################################################### # End of user section! export DAISY_AVAILABLE=1 # Start of internal section function daisy_quit_if_no { has_dep=$DAISY_HAS_$1 # Check first if we have checked for this dependency, if not, print a fixme! # TODO: Remove upon release, or convert into self-modifying code. if [[ ! -v DAISY_HAS_$1 ]]; then echo "FIXME: Dependency `$1` should have an env variable, checking ad-hoc" has_dep=$(daisy_dependency_check $1) fi if [[ $has_dep -eq 0 ]]; then echo "$DAISY_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 # 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" | \ 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="$DAISY_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 }