nix!: refactored homeModules

This commit is contained in:
Primrose 2024-10-12 00:55:42 +02:00
parent 1c48edddf4
commit e9f8ccd2b3
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA
50 changed files with 14 additions and 41 deletions

View file

@ -0,0 +1,15 @@
function ,
set ps
set os
for p in $argv
if not string match -q -- "--*" $p
set -a ps "nixpkgs#$p"
else
set -a os "$p"
end
end
set cmd "SHELL=(which fish) IN_NIX_SHELL=\"impure\" nix shell $os $ps"
echo "Executing `$cmd`..."
eval $cmd
end

View file

@ -0,0 +1,12 @@
function __tmux_attach_or_switch \
--description "Attach (not in tmux) or switch (in tmux) to a session"
### Arguments:
set session_name $argv[1]
if [ -z "$TMUX" ]
tmux attach-session -t $session_name
else
tmux switch-client -t $session_name
end
end

View file

@ -0,0 +1,15 @@
function __tmux_maybe_create \
--description "Create a tmux session with sensible defaults if it doesn't exist yet"
### Arguments:
set session_name $argv[1]
set session_dir $argv[2]
if pgrep tmux 2>&1 >/dev/null; or ! tmux has -t=$session_name 2>/dev/null
tmux \
new-session -ds $session_name -c $session_dir \; \
send-keys -t $session_name $EDITOR ENTER \; \
new-window -t $session_name -c $session_dir \; \
select-window -t $session_name:1
end
end

View file

@ -0,0 +1,11 @@
function __tmux_register_session \
--description "Register the session, if it's not set yet"
### Effects:
set last /tmp/TMUX_LAST
set this (tmux display-message -p '#S')
if [ ! -f "$last" ]; or [ (cat "$last") != "$this" ]
echo "$this" >"$last"
end
end

View file

@ -0,0 +1,13 @@
function clone_to_repos
if not count $argv >/dev/null
echo "Git url needed"
end
set name_repo (echo $argv | sed -E 's/.*[:\\/]([^\\/]+)\\/([^\\/]+?)(:?\\.git)$/\1 \2/')
set name (echo $name_repo | cut -d ' ' -f1)
set repo (echo $name_repo | cut -d ' ' -f2)
mkdir -p $REPOS_PATH/$name
git clone $argv $REPOS_PATH/$name/$repo
end

View file

@ -0,0 +1,12 @@
function fish_greeting
set_color --italics
switch $FISH_GREETING
case toh
cat ~/.quotes/toh/* | shuf -n 1
case fleabag
cat ~/.quotes/fleabag/* | shuf -n 1
case "*"
end
end

View file

@ -0,0 +1,29 @@
function fzf_pass
set selected \
(begin
fd -tf . $PASSWORD_STORE_DIR | sed "s|^$PASSWORD_STORE_DIR/\(.*\)\.gpg\$|\1|"
end | fzf)
if [ -z "$selected" ]
echo Nothing selected
echo exiting...
end
set mode \
(begin
echo "password"
echo "otp"
echo "all"
end | fzf)
switch "$mode"
case all
pass "$selected" | nvim +"setlocal buftype=nofile bufhidden=hide noswapfile"
case password
pass "$selected" -c
case otp
pass otp "$selected" -c
end
end

View file

@ -0,0 +1,5 @@
function snakecase
echo $argv | sed -E 's/[^A-Za-z0-9_\n]+/_/g' | sed -E 's/__+/_/g' | tr '[:upper:]' '[:lower:]'
end

View file

@ -0,0 +1,19 @@
function tmux_home \
--description "Take me back to ~"
set session_name home
if pgrep tmux 2>&1 >/dev/null
__tmux_register_session
end
if pgrep tmux 2>&1 >/dev/null; or ! tmux has -t=$session_name 2>/dev/null
tmux \
new-session -ds home \; \
new-window -t home \; \
select-window -t "home":1
end
__tmux_attach_or_switch $session_name
end

View file

@ -0,0 +1,16 @@
function tmux_last \
--description "Toggle the last tmux session"
set tmux_last /tmp/TMUX_LAST
if [ ! -f $tmux_last ]
echo "Last session is not yet set"
return 1
end
set session_name (cat $tmux_last)
__tmux_register_session
__tmux_maybe_create $session_name /tmp
__tmux_attach_or_switch $session_name
end

View file

@ -0,0 +1,48 @@
function tmux_sessionizer \
--description "Manage tmux sessions in specific folders with fzf"
set selected \
(begin
fd . $REPOS_PATH $UNIV_REPOS_PATH --exact-depth 2 --type d
fd . $PLAYGROUND_PATH --exact-depth 1 --type d
# Special mode to create a playground
echo "play"
echo "dotfiles"
end 2> /dev/null | sed -e "s|^$HOME|~|" | fzf)
set selected (echo $selected | sed -e "s|^~|$HOME|")
if [ -z "$selected" ]
commandline --function repaint
return 0
end
# derive session name based on selected path
# dots are meaningful in tmux, remove them
set session_name (echo $selected | tr . _)
# fixup special cases, override derived session_name if necessary
# this should be id for non-special cases
switch $selected
case play
read -P "Give it a name: " name
if [ -z "$name" ]
return 0
end
set name (snakecase "$name")
set selected ~/playground/$name
set session_name (echo $selected | tr . _)
mkdir -p $selected
case dotfiles
set selected ~/.dotfiles
set session_name dotfiles
end
# effects
__tmux_register_session $session_name
__tmux_maybe_create $session_name $selected
__tmux_attach_or_switch $session_name
end

View file

@ -0,0 +1,10 @@
function update_dotfiles
cd ~/.dotfiles/
git reset --hard
git pull --set-upstream origin main
git submodule update --init
prevd
restow
end