373 lines
8.6 KiB
Bash
Executable file
373 lines
8.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#!/usr/bin/env -S echo "This file can only be sourced, not run stand-alone."
|
|
# 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 <lackadaisical-root>/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 LD_INTERNAL to 1.
|
|
|
|
# Pass _LD_DEBUG=1 during sourcing to see debug information on a variety of things.
|
|
|
|
if [[ $LD_INTERNAL == 1 ]];
|
|
then
|
|
export LD_BIN=$(basename $0)
|
|
fi
|
|
|
|
function ld_dbg
|
|
{
|
|
if [[ $_LD_DEBUG == 1 ]];
|
|
then
|
|
$@
|
|
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
|
|
{
|
|
if command -v "$1" >/dev/null 2>&1; then
|
|
echo 1
|
|
else
|
|
echo 0
|
|
fi
|
|
}
|
|
|
|
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)
|
|
LD_HAS_dialog=$(_daisy_dependency_check dialog)
|
|
|
|
ld_dbg echo "Presence of utils:"
|
|
ld_dbg echo fzf $LD_HAS_fzf
|
|
ld_dbg echo md5sum $LD_HAS_md5sum
|
|
ld_dbg echo peco $LD_HAS_peco
|
|
ld_dbg echo tree $LD_HAS_tree
|
|
ld_dbg echo dialog $LD_HAS_dialog
|
|
|
|
export LD_FOLDER=$(dirname $(realpath $arg))
|
|
export LD_SOURCE_FILE=$(realpath $arg)
|
|
export LD_AVAILABLE=0
|
|
|
|
# Config folder setup
|
|
export LD_CONFIG_FOLDER="$HOME/.config/lackadaisical"
|
|
export LD_NEW_INSTALL=
|
|
|
|
if [[ ! -d "$LD_CONFIG_FOLDER" ]];
|
|
then
|
|
# Create the folder with its basics
|
|
mkdir -p "$LD_CONFIG_FOLDER"
|
|
new_install=1
|
|
fi
|
|
|
|
# Multiple default source files
|
|
# [LEA.TODO] Turn these into arrays
|
|
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
|
|
|
|
ld_dbg echo "Sourced config contents:"
|
|
ld_dbg cat $LD_ALIASFILE
|
|
ld_dbg cat $LD_EDITORFILE
|
|
ld_dbg cat $LD_ESOURCEFILE
|
|
|
|
# Source everything in the config folder
|
|
function _daisy_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
|
|
{
|
|
cdpath="$@"
|
|
if [[ $cdpath == '' ]]
|
|
then
|
|
\cd
|
|
return
|
|
fi
|
|
|
|
if cdz --check "$cdpath" >/dev/null 2>&1
|
|
then
|
|
cdz "$cdpath"
|
|
return
|
|
fi
|
|
|
|
if [[ -f "$cdpath/.needs_mount" ]]
|
|
then
|
|
squasher mount "$cdpath"
|
|
fi
|
|
\cd "$cdpath"
|
|
}
|
|
|
|
alias cd=multicd
|
|
|
|
# bak and unbak
|
|
function bak
|
|
{
|
|
# Input: <file>
|
|
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 == 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
|
|
}
|
|
|
|
function unbak
|
|
{
|
|
unbak_mode=1 bak $@
|
|
}
|
|
|
|
function lsa
|
|
{
|
|
ls -a -l -h $@
|
|
}
|
|
|
|
function lsn
|
|
{
|
|
ls -a -l -tu -r -h $@
|
|
}
|
|
|
|
function lss
|
|
{
|
|
ls -a -l -S -r -h $@
|
|
}
|
|
|
|
# Simple version of `cdf`
|
|
function cdf
|
|
{
|
|
if [[ $LD_HAS_fzf != 1 ]];
|
|
then
|
|
echo "This function requires the utility 'fzf'. Please install it."
|
|
return 1
|
|
fi
|
|
cd "$(dirname "$(fzf)")"
|
|
}
|
|
|
|
function cdp
|
|
{
|
|
if [[ $LD_HAS_peco != 1 || $LD_HAS_tree != 1 ]];
|
|
then
|
|
echo "This function requires the utilities 'peco' and 'tree'. Please install them."
|
|
echo "Consider using 'cdf' instead."
|
|
return 1
|
|
fi
|
|
cd $(dirname $(tree -fia --noreport . | peco))
|
|
}
|
|
|
|
function editpeco
|
|
{
|
|
if [[ $LD_HAS_peco != 1 || $LD_HAS_tree != 1 ]];
|
|
then
|
|
echo "This function 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 {}"
|
|
}
|
|
|
|
# for convenience purposes
|
|
function editbin
|
|
{
|
|
editx $(which $1)
|
|
}
|
|
|
|
# sets a new editor based on commony available ones, and some visual ones
|
|
function ched
|
|
{
|
|
override=0
|
|
if [[ $1 == "-g" ]];
|
|
then
|
|
override=1
|
|
fi
|
|
|
|
if [[ $LD_HAS_dialog != 1 ]];
|
|
then
|
|
echo "This function requires the utility 'dialog'. Please install it."
|
|
return 1
|
|
fi
|
|
|
|
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)")
|
|
elif [[ $LD_EDITOR == "$editor_real" ]];
|
|
then
|
|
available_editors+=("$editor_real" "$editor (LD choice)")
|
|
else
|
|
available_editors+=("$editor_real" "$editor")
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [[ $override == 0 ]] && [[ ! -z $@ ]];
|
|
then
|
|
text="$@"
|
|
dialog --msgbox "$text" 0 0
|
|
elif [[ $override == 1 ]];
|
|
then
|
|
text="You have passed '-g'. Your choice of dialog will override any other choice or setting of 'EDITOR'."
|
|
dialog --msgbox "$text" 0 0
|
|
fi
|
|
|
|
# Present all choices
|
|
choice=$(dialog --output-fd 1 --clear --title "Select Text Editor" \
|
|
--menu "Choose one of the installed text editors:" 15 100 6 \
|
|
"${available_editors[@]}")
|
|
dialog_ret=$?
|
|
|
|
if [ $dialog_ret -ne 0 ];
|
|
then
|
|
dialog --msgbox "No editor selected. Variables will not be updated." 0 0
|
|
return 0
|
|
fi
|
|
|
|
[[ $override == 0 ]] && echo export EDITOR="${EDITOR:-$choice}" > "$LD_EDITORFILE"
|
|
[[ $override == 1 ]] && echo export EDITOR="$choice" > "$LD_EDITORFILE"
|
|
echo export LD_EDITOR="$choice" >> "$LD_EDITORFILE"
|
|
echo export LD_OLD_EDITOR="$EDITOR" >> "$LD_EDITORFILE"
|
|
|
|
source "$LD_EDITORFILE"
|
|
}
|
|
|
|
function ldrc
|
|
{
|
|
ARG=$1
|
|
SOURCE="$LD_SOURCE_FILE"
|
|
[[ "$ARG" == "-e" ]] && SOURCE="$LD_ESOURCEFILE"
|
|
|
|
daisy editor "$SOURCE"
|
|
LD_INTERNAL=0 source "$SOURCE"
|
|
}
|
|
|
|
# Saves a bit on typing
|
|
function grab
|
|
{
|
|
[[ -z $@ ]] && return;
|
|
awk '{print $'$1'}'
|
|
}
|
|
|
|
function clip
|
|
{
|
|
data=""
|
|
|
|
if [ ! -t 0 ]; then
|
|
data="$(cat)"
|
|
elif [ "$*" != "" ]; then
|
|
data="$*"
|
|
else
|
|
echo $LD_CLIP
|
|
return 0
|
|
fi
|
|
|
|
# Export the variable
|
|
export LD_CLIP="$data"
|
|
echo "Variable set to \"$LD_CLIP\"."
|
|
}
|
|
|
|
# Aliases for front-facing daisy commands
|
|
function _daisy_def_alias
|
|
{
|
|
# 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
|
|
_daisy_def_alias enc
|
|
_daisy_def_alias enc_multi
|
|
_daisy_def_alias enc_folder
|
|
_daisy_def_alias dec
|
|
_daisy_def_alias dec_multi
|
|
_daisy_def_alias alias
|
|
_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
|