1
0
Fork 0
dotfiles/fish/conf.d/functions.fish

184 lines
No EOL
4.8 KiB
Fish

function cleanpycs
find . -name '.git' -o -name __pycache__ -delete
find . -name '.git' -o -name '*.py[co]' -delete
end
function envsource
if not set -q argv[1]
set argv[1] ".env"
end
echo "Using $argv[1] as input file"
for line in (grep -v '^\s*\(#\|$\)' $argv[1] | grep -E '^[A-Za-z_]+=[^#\n]+')
set item (string split -m 1 '=' $line)
set -gx $item[1] $item[2]
echo "Exported key $item[1]"
end
end
function envunset
if not set -q argv[1]
set argv[1] ".env"
end
echo "Using $argv[1] as input file"
for line in (grep -v '^\s*\(#\|$\)' $argv[1] | grep -E '^[A-Za-z_]+=[^#\n]+')
set item (string split -m 1 '=' $line)
set -e $item[1]
echo "key $item[1] unset"
end
end
function gen-secret
if not set -q argv[1]
set argv[1] 64
end
openssl rand -hex $argv[1]
end
function ffmerge
set input_videos
for arg in $argv
if test (string sub -l 4 $arg) = ".mp4"
set input_videos $input_videos -i $arg
else
set output $arg
end
end
ffmpeg $input_videos -filter_complex (printf "[%s] " $input_videos | sed 's/ -i / concat=n=%d:v=1:a=1 [v] [a]/') -map "[v]" -map "[a]" $output
end
function fish_right_prompt
set -l k8s_color (set_color blue)
set -l k8s_context (kubectl config current-context)
echo -e -n -s $k8s_color "($k8s_context)"
end
function gr
if not set -q argv[1]
set argv[1] 1
end
git reset "HEAD~$argv[1]"
end
function gur
if not set -q argv[1]
set argv[1] 1
end
git reset "HEAD@{$argv[1]}"
end
function nix_upgrade
# Upgrade this system based on hostname matching the flake host.
# Works on both NixOS (nixos-rebuild) and non-NixOS (Home Manager).
# Usage: nix_upgrade [host-override]
set -l flake_path /home/catalin/.dotfiles/nix
# Determine host either from arg or from the machine hostname
if set -q argv[1]
set -l host $argv[1]
else
set -l host (hostname -s)
if test $status -ne 0 -o -z "$host"
set host (hostname)
end
end
if not test -f $flake_path/flake.nix
echo "Error: flake not found at $flake_path/flake.nix"
return 1
end
# Detect if we are on NixOS
set -l is_nixos 0
if test -f /etc/NIXOS
set is_nixos 1
else if type -q nixos-version
set is_nixos 1
end
echo "Updating inputs for flake: $flake_path"
# On non-NixOS this usually doesn't require sudo; on NixOS it might.
if test $is_nixos -eq 1
sudo nix flake update --flake $flake_path
else
nix flake update --flake $flake_path
end
if test $is_nixos -eq 1
# NixOS path: verify host exists under nixosConfigurations (heuristic)
if not grep -Eq "^[[:space:]]*$host[[:space:]]*=[[:space:]]*lib\\.nixosSystem" $flake_path/flake.nix
echo "Error: host '$host' not found in nixosConfigurations in $flake_path/flake.nix."
echo " Pass an explicit host: nix_upgrade <host>"
return 1
end
echo "Rebuilding NixOS for host: $host"
sudo nixos-rebuild switch --flake $flake_path#$host --upgrade
else
# non-NixOS path: try Home Manager via flake's homeConfigurations
if not grep -q "homeConfigurations" $flake_path/flake.nix
echo "Error: no homeConfigurations found in flake; cannot upgrade on non-NixOS."
return 1
end
# Best-effort heuristic that the host exists as a home configuration
if not grep -Eq "^[[:space:]]*$host[[:space:]]*=" $flake_path/flake.nix
echo "Warning: host '$host' not explicitly found; attempting Home Manager switch anyway."
end
echo "Rebuilding Home Manager for host: $host"
if type -q home-manager
home-manager switch --flake $flake_path#$host
else
# Fallback: use nix to run HM
nix run github:nix-community/home-manager -- switch --flake $flake_path#$host
end
end
end
function delete_line
if test (count $argv) -lt 2
echo "Usage: delete_line <file> <line_number> [--backup]"
return 1
end
set file $argv[1]
set line_num $argv[2]
set make_backup false
if contains -- --backup $argv
set make_backup true
end
if not test -f $file
echo "Error: File '$file' does not exist"
return 1
end
if not string match -qr '^\d+$' $line_num
echo "Error: Line number must be a positive integer"
return 1
end
set total_lines (wc -l < $file)
if test $line_num -gt $total_lines
echo "Error: Line $line_num does not exist (file has only $total_lines lines)"
return 1
end
if test $make_backup = true
cp $file $file.bak
echo "Backup created: $file.bak"
end
sed -i "$line_num"d $file
echo "Deleted line $line_num from $file"
end