Skip to content

Commit

Permalink
🔧 (fish): fish is fun
Browse files Browse the repository at this point in the history
  • Loading branch information
elythh committed Oct 19, 2024
1 parent 4bce4e7 commit 8882a78
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 3 deletions.
3 changes: 2 additions & 1 deletion modules/home/core/home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
delta
docker-compose
eza
fd
feh
fx
fzf
Expand Down Expand Up @@ -133,7 +134,7 @@
yarn
zed-editor
zellij
zoxide
# zoxide
];
};
}
19 changes: 18 additions & 1 deletion modules/home/opt/shell/fish/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{ pkgs, ... }:
{ lib, pkgs, ... }:
{

xdg.configFile."fish/functions" = {
source = lib.cleanSourceWith { src = lib.cleanSource ./functions/.; };
recursive = true;
};

programs.fish = {
enable = true;
functions = {
Expand Down Expand Up @@ -28,6 +33,18 @@
inherit (pkgs.fishPlugins.sponge) src;
name = "sponge";
}
{
inherit (pkgs.fishPlugins.z) src;
name = "z";
}
# {
# inherit (pkgs.fishPlugins.fzf) src;
# name = "fzf";
# }
{
inherit (pkgs.fishPlugins.fzf-fish) src;
name = "fzf-fish";
}
];
};
}
24 changes: 24 additions & 0 deletions modules/home/opt/shell/fish/functions/bak.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function bak
if [ (count $argv) -ne 1 ]
echo "1 path must be supplied"
return 1
end
set file (basename $argv[1] '.bak')
set other "$file.bak"
if test -e $argv[1]
if test -e $other
mv $argv[1] $other.tmp
mv $other $argv[1]
mv $other.tmp $other
else
mv $argv[1] $other
end
else
if test -e $other
mv $other $argv[1]
else
echo "Neither $argv[1] nor $other exist"
return 1
end
end
end
32 changes: 32 additions & 0 deletions modules/home/opt/shell/fish/functions/cd.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function cd --description 'change directory and run onefetch if a git directory'
set -l parent

# navigate to root of git repo
if [ $argv = ":/" ]
set -l top (command git rev-parse --show-cdup)
builtin cd $top
# check if cd to file and route to parent
else if [ -f $argv ] && [ -e $argv ]
set parent (dirname $argv)
if [ "$parent" != . ] && [ -d "$parent" ]
builtin cd "$parent"
end
# if navigating to a directory, cd as usual
else if [ -d $argv ]
if test -n "$argv" &&
builtin cd "$argv"
else
builtin cd
end
end

# check for git information
git rev-parse 2>/dev/null

if test $status -eq 0
if test -z "$LAST_REPO" -o "$LAST_REPO" != $(basename $(git rev-parse --show-toplevel))
onefetch
set -g LAST_REPO $(basename $(git rev-parse --show-toplevel))
end
end
end
38 changes: 38 additions & 0 deletions modules/home/opt/shell/fish/functions/ex.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function ex
if test -f $argv[1]
set filename (basename $argv[1])
set foldername (echo $filename | sed 's/\.[^.]*$//')
mkdir -p $foldername
switch $argv[1]
case '*.tar.bz2'
tar xjf $argv[1] -C $foldername
case '*.tar.gz'
tar xzf $argv[1] -C $foldername
case '*.bz2'
bunzip2 -k $argv[1]
mv (basename $argv[1] .bz2) ./$foldername/
case '*.rar'
unrar x $argv[1] ./$foldername/
case '*.gz'
gunzip -k $argv[1]
mv (basename $argv[1] .gz) ./$foldername/
case '*.tar'
tar xf $argv[1] -C ./$foldername/
case '*.tbz2'
tar xjf $argv[1] -C ./$foldername/
case '*.tgz'
tar xzf $argv[1] -C ./$foldername/
case '*.zip'
unzip -d ./$foldername/ "$filename"
case '*.Z'
uncompress "$filename"
mv (basename "$filename" ".Z") "./$foldername/"
case '*.7z'
7z x "$filename" "-o./$foldername/"
otherwise
echo "'$filename' cannot be extracted via ex()"
end
else
echo "'$filename' is not a valid file"
end
end
49 changes: 49 additions & 0 deletions modules/home/opt/shell/fish/functions/git.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Add custom git extensions
function git
set -l code

# Truncate long lines in git grep
if test "$argv[1]" = grep

if ! command -v perl &>/dev/null
command git $argv
return
end

command git -c color.ui=always $argv | perl -pe 'my $truncate = 500; (my $blank = $_) =~ s/\e\[[^m]*m//g; if (length $blank > $truncate) {
s/^((?:(?:\e\[[^m]*m)+(?:.|$)|.(?:\e\[[^m]*m)*|$(*SKIP)(*FAIL)){$truncate})(?=(?:(?:\e\[[^m]*m)+(?:.|$)|.(?:\e\[[^m]*m)*|$(*SKIP)(*FAIL)){15}).*/$1\e\[m...(truncated)/
}'

return
end

# Prevent accidental git commit -a
if test "$argv[1]" = commit && contains -- "$argv[2]" -a
if ! command git diff-index --cached --quiet HEAD -- && ! command git diff-files --quiet

echo >&2 '\e[0;31mERROR!\e[0m Changes are already staged. Preventing git commit -a'
echo >&2 '\e[0;31mERROR!\e[0m Run git commit without -a or run git reset HEAD first'

return 1
end
end

# forward original git command
if test -n "$argv"
command git $argv
else
command git
end

# record command exit code
set code $status

# output commit length if successful
if test "$argv[1]" = commit && ! code
printf 'Commit subject length: '
command git log -1 --format="%s" | tr -d '\n' | wc -m | awk '{print $1}'
end

# return original exit code
return $code
end
9 changes: 9 additions & 0 deletions modules/home/opt/shell/fish/functions/load_ssh.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function load_ssh
if status is-login
and status is-interactive
# To add a key, set -Ua SSH_KEYS_TO_AUTOLOAD keypath
# To remove a key, set -U --erase SSH_KEYS_TO_AUTOLOAD[index_of_key]
set -Ua SSH_KEYS_TO_AUTOLOAD ~/.ssh/id_rsa
keychain -q -Q --eval $SSH_KEYS_TO_AUTOLOAD | source
end
end
4 changes: 4 additions & 0 deletions modules/home/opt/shell/fish/functions/mkcd.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function mkcd
mkdir -p $argv
cd $argv
end
7 changes: 7 additions & 0 deletions modules/home/opt/shell/fish/functions/mvcd.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function mvcd -d "Moves files and directories and changes the current directory"
if test (count $argv) -gt 1
mkdir -p $argv[2]
mv $argv[1] $argv[2]
cd $argv[2]
end
end
16 changes: 16 additions & 0 deletions modules/home/opt/shell/fish/functions/nixify.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function nixify
if not test -e ./.envrc
echo "use nix" >.envrc
direnv allow
end
if not test -e shell.nix -a ! -e default.nix
set code 'with import <nixpkgs> {};
mkShell {
nativeBuildInputs = [
bashInteractive
];
}'
echo $code | sed "s/'/'\\\\''/g" | xargs echo >default.nix
eval (env $EDITOR "default.nix")
end
end
2 changes: 1 addition & 1 deletion modules/home/opt/shell/zsh/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'';
};
programs.atuin = {
enable = true;
enable = false;
enableZshIntegration = true;
settings = {
style = "compact";
Expand Down

0 comments on commit 8882a78

Please sign in to comment.