Refactor
All checks were successful
/ check (push) Successful in 50s

This commit is contained in:
Oliver Davies 2025-09-03 08:30:15 +01:00
parent 21d9ca8737
commit fcfdc8a580
2 changed files with 126 additions and 103 deletions

View file

@ -1,42 +1,53 @@
{ withSystem, ... }:
{
perSystem =
psArgs@{ pkgs, ... }:
{
packages.clone = pkgs.writeShellApplication {
name = "clone";
runtimeInputs = with pkgs; [
git
psArgs.config.packages.tmux-sessionizer
];
text = ''
repo_url="$1"
repo_url="''${repo_url%.git}"
if [[ "$repo_url" =~ ^(git@|https://|ssh://forgejo@)?([^:/]+)[:/](.*)/(.*)$ ]]; then
domain="''${BASH_REMATCH[2]}"
if [[ "$domain" == "ssh.oliverdavies.uk" ]]; then
domain="code.oliverdavies.uk"
fi
user="''${BASH_REMATCH[3]}"
name="''${BASH_REMATCH[4]}"
user_path="$XDG_REPOS_DIR/$domain/$user"
repo_path="$user_path/$name"
[[ -d "$repo_path" ]] && tmux-sessionizer "$repo_path" && exit 0
mkdir -pv "$repo_path"
git clone "$repo_url" "$repo_path"
tmux-sessionizer "$repo_path"
else
exit 1
fi
'';
};
};
flake.modules.homeManager.base =
{ pkgs, ... }:
{
home.packages = [
(pkgs.writeShellApplication {
name = "clone";
runtimeInputs = with pkgs; [ git ];
text = ''
repo_url="$1"
repo_url="''${repo_url%.git}"
if [[ "$repo_url" =~ ^(git@|https://|ssh://forgejo@)?([^:/]+)[:/](.*)/(.*)$ ]]; then
domain="''${BASH_REMATCH[2]}"
if [[ "$domain" == "ssh.oliverdavies.uk" ]]; then
domain="code.oliverdavies.uk"
fi
user="''${BASH_REMATCH[3]}"
name="''${BASH_REMATCH[4]}"
user_path="$XDG_REPOS_DIR/$domain/$user"
repo_path="$user_path/$name"
[[ -d "$repo_path" ]] && tmux-sessionizer "$repo_path" && exit 0
mkdir -pv "$repo_path"
git clone "$repo_url" "$repo_path"
tmux-sessionizer "$repo_path"
else
exit 1
fi
'';
})
(withSystem pkgs.system (psArgs: psArgs.config.packages.clone))
];
};
}

View file

@ -1,77 +1,89 @@
{ withSystem, ... }:
{
perSystem =
{ pkgs, ... }:
{
packages.tmux-sessionizer = pkgs.writeShellApplication {
name = "tmux-sessionizer";
runtimeInputs = with pkgs; [
coreutils
fzf
tmux
];
text = ''
set -euo pipefail
# Based on https://github.com/jessarcher/dotfiles/blob/master/scripts/t
# and https://github.com/ThePrimeagen/tmux-sessionizer.
has_session() {
tmux list-sessions | grep -q "^$1:"
}
hydrate() {
if [ -f "$2/.tmux-sessionizer" ]; then
tmux send-keys -t "$1" "source $2/.tmux-sessionizer" c-M
elif [ -f "$HOME/.tmux-sessionizer" ]; then
tmux send-keys -t "$1" "source $HOME/.tmux-sessionizer" c-M
fi
}
is_tmux_running() {
tmux_running=$(pgrep tmux)
if [[ -z ''${TMUX:-} ]] && [[ -z $tmux_running ]]; then
return 1
fi
return 0
}
switch_to() {
tmux attach-session -t "$1" || tmux switch-client -t "$1"
}
if [[ $# -eq 1 ]]; then
selected=$1
else
items=$(
find "$HOME/Code" -maxdepth 3 -mindepth 1 -type d
find "$HOME/Documents" -maxdepth 1 -mindepth 1 -type d
find "$HOME/tmp" -maxdepth 1 -type d
)
selected=$(echo "$items" | sed "s|^$HOME/||" | sort | fzf)
[[ $selected ]] && selected="$HOME/$selected"
fi
if [[ -z $selected ]]; then
exit 0
fi
selected_name=$(basename "$selected" | tr . _)
if ! is_tmux_running; then
tmux new-session -ds "$selected_name" -c "$selected"
hydrate "$selected_name" "$selected"
fi
if ! has_session "$selected_name"; then
tmux new-session -ds "$selected_name" -c "$selected"
hydrate "$selected_name" "$selected"
fi
switch_to "$selected_name"
'';
};
};
flake.modules.homeManager.base =
{ pkgs, ... }:
{
home.packages = [
(pkgs.writeShellApplication {
name = "tmux-sessionizer";
runtimeInputs = with pkgs; [ coreutils fzf tmux ];
text = ''
set -euo pipefail
# Based on https://github.com/jessarcher/dotfiles/blob/master/scripts/t
# and https://github.com/ThePrimeagen/tmux-sessionizer.
has_session() {
tmux list-sessions | grep -q "^$1:"
}
hydrate() {
if [ -f "$2/.tmux-sessionizer" ]; then
tmux send-keys -t "$1" "source $2/.tmux-sessionizer" c-M
elif [ -f "$HOME/.tmux-sessionizer" ]; then
tmux send-keys -t "$1" "source $HOME/.tmux-sessionizer" c-M
fi
}
is_tmux_running() {
tmux_running=$(pgrep tmux)
if [[ -z ''${TMUX:-} ]] && [[ -z $tmux_running ]]; then
return 1
fi
return 0
}
switch_to() {
tmux attach-session -t "$1" || tmux switch-client -t "$1"
}
if [[ $# -eq 1 ]]; then
selected=$1
else
items=$(
find "$HOME/Code" -maxdepth 3 -mindepth 1 -type d
find "$HOME/Documents" -maxdepth 1 -mindepth 1 -type d
find "$HOME/tmp" -maxdepth 1 -type d
)
selected=$(echo "$items" | sed "s|^$HOME/||" | sort | fzf)
[[ $selected ]] && selected="$HOME/$selected"
fi
if [[ -z $selected ]]; then
exit 0
fi
selected_name=$(basename "$selected" | tr . _)
if ! is_tmux_running; then
tmux new-session -ds "$selected_name" -c "$selected"
hydrate "$selected_name" "$selected"
fi
if ! has_session "$selected_name"; then
tmux new-session -ds "$selected_name" -c "$selected"
hydrate "$selected_name" "$selected"
fi
switch_to "$selected_name"
'';
})
(withSystem pkgs.system (psArgs: psArgs.config.packages.tmux-sessionizer))
];
};
}