From 60cc207011dda16ba4000ed23518874cfd76cb4c Mon Sep 17 00:00:00 2001 From: Lea Date: Tue, 9 Jun 2026 13:17:56 +0200 Subject: [PATCH] Added "daisy arg", a hooking tool that allows you to re-use arguments from previous commands. --- README.md | 13 +++++++++ daisy.command.source | 12 ++++++++ ld.source | 69 +++++++++++++++++++++++++++++--------------- 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 450c3e3..f7b1f87 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,19 @@ Edits ld.source and re-sources it, similarly to shrc. Append "-e" to edit Append "-d" to edit "daisy.command.source". ················································································ +< daisy arg > +················································································ +This tool intercepts the arguments of the last command executed in your shell. +You can retrieve specific arguments by index (starting at 1) or all arguments +at once using "@". The command itself (index 0) is excluded from "@". +Arguments containing spaces are correctly preserved. + +Usage: $(daisy arg ) + $(daisy arg @) +Example: ls -l "My Folder" + cd "$(daisy arg 2)" # Changes directory to "My Folder" +················································································ + < daisy reload > ················································································ Re-sources ld.source. Essentially `ldrc` without editing. diff --git a/daisy.command.source b/daisy.command.source index b825177..c4fc493 100755 --- a/daisy.command.source +++ b/daisy.command.source @@ -419,6 +419,18 @@ daisy () return 1 fi ;; + arg) + local index=$2 + if [[ "$index" == "@" ]]; then + echo "${DAISY_LAST_ARGS[@]:1}" + elif [[ "$index" =~ ^[0-9]+$ ]]; then + if [[ -n "$ZSH_VERSION" ]]; then echo "${DAISY_LAST_ARGS[$((index+1))]}"; else echo "${DAISY_LAST_ARGS[$index]}"; fi + else + echo "Usage: daisy arg " + return 1 + fi + ;; + reload) LD_INTERNAL=0 source "$LD_SOURCE_FILE" ;; diff --git a/ld.source b/ld.source index 1882c0c..4ab1981 100755 --- a/ld.source +++ b/ld.source @@ -91,30 +91,6 @@ ld_dbg cat $LD_ESOURCEFILE ld_dbg cat $LD_SHORTFILE # Source everything in the config folder -function _ld.source_configs -{ - while IFS= read -r -d '' f; do - source "$f" - done < <(find "$LD_CONFIG_FOLDER" -name "*.src" -type f -print0) -} - -# Installation into PATH -if [[ ! $PATH == *"$LD_FOLDER"* ]]; -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! -if [[ ! -v LD_INTERNAL ]]; -then - alias shrc=". shrc" -fi - -############################################################################### -# FUNCTIONS and ALIASES ####################################################### -############################################################################### function multicd { @@ -350,6 +326,51 @@ _daisy_def_alias combine _daisy_def_alias help _daisy_def_alias list _daisy_def_alias check +_daisy_def_alias arg + + +# --- Argument Interception Hook --- +_daisy_intercept_args() { + local last_cmd + if [[ -n "$ZSH_VERSION" ]]; then + last_cmd=$(fc -ln -1 | sed "s/^[[:space:]]*//") + else + last_cmd=$(fc -ln -1 2>/dev/null | sed "s/^[[:space:]]*//") + fi + [[ -z "$last_cmd" ]] && return + [[ "$last_cmd" == daisy* ]] && return + [[ "$last_cmd" == ld_* ]] && return + [[ "$last_cmd" == daisy_* ]] && return + local -a tmp_args + if [[ -n "$ZSH_VERSION" ]]; then + tmp_args=(${(z)last_cmd}) + else + eval "tmp_args=($last_cmd)" 2>/dev/null + fi + if [[ ${#tmp_args[@]} -gt 0 ]]; then + export DAISY_LAST_ARGS=("${tmp_args[@]}") + fi +} +if [[ -n "$ZSH_VERSION" ]]; then + if [[ ! "$precmd_functions" == *_daisy_intercept_args* ]]; then + precmd_functions+=(_daisy_intercept_args) + fi +else + if [[ "$PROMPT_COMMAND" != *_daisy_intercept_args* ]]; then + if [[ -z "$PROMPT_COMMAND" ]]; then + PROMPT_COMMAND="_daisy_intercept_args" + else + PROMPT_COMMAND="_daisy_intercept_args; $PROMPT_COMMAND" + fi + fi +fi + +function _ld.source_configs +{ + while IFS= read -r -d "" f; do + source "$f" + done < <(find "$LD_CONFIG_FOLDER" -name "*.src" -type f -print0) +} _ld.source_configs