Tobias Reisinger
9344f7ec3b
Fix call to $EDITOR (and switch sh to bash) in edit Fix waybar mode (remove to fix layer) Add hyprland media keys
88 lines
1.6 KiB
Bash
Executable file
88 lines
1.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
edit_dir_base="$XDG_RUNTIME_DIR/edit"
|
|
|
|
_pre_checks() {
|
|
if [ -z "$EDITOR" ]; then
|
|
echo "EDITOR is not set"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
_pre_checks_file() {
|
|
if [ -z "$1" ]; then
|
|
echo "No file specified"
|
|
exit 1
|
|
fi
|
|
if sudo test -d "$1"; then
|
|
echo "Cannot edit directory"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
_readlink() {
|
|
sudo readlink -fn "$1"
|
|
}
|
|
|
|
_get_edit_directory() {
|
|
target_dir=$(dirname "$1")
|
|
edit_dir_sub=$(_readlink "$target_dir" | md5sum | awk '{ print $1 }')
|
|
|
|
edit_directory="$edit_dir_base/$edit_dir_sub"
|
|
echo "$edit_directory"
|
|
}
|
|
|
|
_get_edit_file() {
|
|
target=$(_readlink "$1")
|
|
target_name=$(basename "$target")
|
|
edit_directory=$(_get_edit_directory "$target")
|
|
|
|
edit_file="$edit_directory/$target_name"
|
|
echo "$edit_file"
|
|
}
|
|
|
|
_prepare_for_edit() {
|
|
target=$(_readlink "$1")
|
|
|
|
_pre_checks_file "$target"
|
|
|
|
edit_directory=$(_get_edit_directory "$target")
|
|
edit_file=$(_get_edit_file "$target")
|
|
|
|
mkdir -p "$edit_directory"
|
|
|
|
user=$(id -un)
|
|
group=$(id -gn)
|
|
|
|
if sudo test -e "$target"; then
|
|
sudo cp -i "$target" "$edit_file"
|
|
sudo chown "$user":"$group" "$edit_file"
|
|
else
|
|
touch "$edit_file"
|
|
fi
|
|
|
|
echo "$edit_file"
|
|
}
|
|
|
|
_pre_checks
|
|
|
|
# Prepare all files for editing and collect the names of the edit files
|
|
all_edit_files=()
|
|
for arg in "$@"; do
|
|
edit_file=$(_prepare_for_edit "$arg")
|
|
all_edit_files+=("$edit_file")
|
|
done
|
|
|
|
$EDITOR "${all_edit_files[@]}"
|
|
|
|
# Copy all edit files back
|
|
for arg in "$@"; do
|
|
edit_file=$(_get_edit_file "$arg")
|
|
sudo cp "$edit_file" "$arg" && rm "$edit_file"
|
|
|
|
edit_directory=$(_get_edit_directory "$target")
|
|
# Try to remove the directory, but don't fail if it's not empty
|
|
rm -d "$edit_directory" 2>/dev/null || true
|
|
done
|