nix-conf/user/config/bash/func

81 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

2024-12-11 05:17:37 -05:00
#!/usr/bin/env bash
function show() {
if [[ "$#" -ne 1 ]]; then
echo "usage: show <prog>" >&2
return 1
fi
case "$(type -t "$1")" in
alias)
alias "$1" | bat -pl sh ;;
keyword|builtin)
help "$1" ;;
function)
declare -f "$1" | bat -l sh ;;
file)
local path="$(which "$1")"
local real="$(realpath "$path")"
[[ "$path" != "$real" ]] && echo "$path"
bat "$real" ;;
"")
echo "$1 doesn't exist" ;;
esac
}
function goto() {
if [[ "$#" -ne 1 ]]; then
echo "usage: goto <prog>" >&2
return 1
fi
local path="$(which "$1")"
[[ -z "$path" ]] && return 2
local real="$(realpath "$path")"
cd "${real%/*}"
}
2024-12-17 19:09:40 -05:00
function rl() {
local l="$1"
local i=50
while [[ -L "$l" && $((i--)) > 0 ]]; do
local n="$(readlink "$l")"
echo "$l -> $n"
l="$n"
done
[[ -e "$l" ]] && echo "$l" || echo "~nonexistent~"
}
2024-12-25 14:31:37 -05:00
function tmpedit() {
if [[ "$#" -ne 1 ]]; then
echo "usage: tmpedit <file>" >&2
return 1
fi
local real="$(realpath "$1")"
if [[ ! -f "$real" ]]; then
echo "not found"
return 2
fi
if [[ ! "$real" =~ /nix/store/* ]]; then
echo "not nix"
return 3
fi
cp --remove-destination "$real" "$1"
chmod +w "$1"
$EDITOR "$1"
}
2025-03-16 20:06:59 -04:00
function venv() {
local dir="$(pwd)"
while [[ "$dir" != "/" ]]; do
if [[ -d "$dir/venv" && -f "$dir/venv/bin/activate" ]]; then
if [[ -v VIRTUAL_ENV ]]; then
if [[ "$VIRTUAL_ENV" != "$dir/venv" ]]; then
deactivate
else
echo "already active"
return
fi
fi
. "$dir/venv/bin/activate"
echo "activated $(realpath --relative-to . "$dir/venv")"
return
fi
dir="$(dirname "$dir")"
done
echo "not found"
}