feat: tmux sessionizer

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-06-05 00:39:00 -03:00
parent c636db448e
commit d565fd25e4
3 changed files with 64 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
{...}: {
imports = [
./gfonts.nix
./gterminal.nix
./gterminal
./vault
];
options = {};

View File

@@ -5,10 +5,24 @@
...
}: let
cfg = config.profiles.gterminal;
PATHS =
lib.strings.concatMapStringsSep " "
(p: (builtins.replaceStrings ["~/"] ["${config.home.homeDirectory}/"] p))
cfg.sessionizer.paths;
sessionizer = pkgs.writeShellScriptBin "sessionizer" ''
function tmux() { ${pkgs.tmux}/bin/tmux "$@"; }
function fzf() { ${pkgs.fzf}/bin/fzf "$@"; }
PATHS="${PATHS}"
${builtins.readFile ./sessionizer.sh}
'';
in {
imports = [
../programs/wezterm.nix
../programs/neovim.nix
../../programs/wezterm.nix
../../programs/neovim.nix
];
options.profiles.gterminal = with lib;
with lib.types; {
@@ -27,6 +41,16 @@ in {
default = "${cfg.emulator.pkg}/bin/alacritty";
};
};
sessionizer = {
enable = mkOption {
type = bool;
default = true;
};
paths = mkOption {
type = listOf str;
default = [];
};
};
shell = {
pkg = mkOption {
type = package;
@@ -113,6 +137,12 @@ in {
bind -T prefix / split-window -v -c "#''''{pane_current_path}"
bind -T prefix \\ split-window -h -c "#''''{pane_current_path}"
${
if cfg.sessionizer.enable
then "bind -T prefix g run-shell \"tmux neww ${sessionizer}/bin/sessionizer\""
else ""
}
'';
tmux.keyMode = "vi";
tmux.newSession = true;
@@ -210,6 +240,8 @@ in {
alias tmux="tmux -f ${config.xdg.configHome}/tmux/tmux.conf";
alias lg="${pkgs.lazygit}/bin/lazygit";
alias goto="${sessionizer}/bin/sessionizer"
alias gt="${sessionizer}/bin/sessionizer"
'';
};
};

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# "Chat is MIT"
# Thank you ThePrimeagen ;)
# https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer
if [[ $# -eq 1 ]]; then
selected="$1"
else
selected=$(find "$PATHS" -mindepth 1 -maxdepth 2 -type d | fzf)
fi
if [[ -z "$selected" ]]; then
exit 0
fi
selected_name="$(basename "$selected" | tr . _)"
tmux_running="$(pgrep tmux)"
if [[ -z "$TMUX" ]] && [[ -z "$tmux_running" ]]; then
tmux new-session -s "$selected_name" -c "$selected"
exit 0
fi
if ! tmux has-session -t="$selected_name" 2> /dev/null; then
tmux new-session -ds "$selected_name" -c "$selected"
fi
tmux switch-client -t "$selected_name"