Skip to content

Commit

Permalink
refactor: modularize tmux for better readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Dec 11, 2023
1 parent d457ef1 commit 897a924
Show file tree
Hide file tree
Showing 21 changed files with 221 additions and 197 deletions.
5 changes: 2 additions & 3 deletions home/konrad/common/global/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ in
./git.nix
./glow.nix
./k9s.nix
./modules.nix
./neovim.nix
./packages.nix
./readline.nix
Expand All @@ -23,11 +24,9 @@ in
./ssh-keys.nix
./starship.nix
./tealdeer.nix
./tmux.nix
] ++ (builtins.attrValues (import ./../modules))
]
++ (builtins.attrValues customArgs.homeManagerModules);

# Let Home Manager install and manage itself.
programs.home-manager.enable = true;

home = {
Expand Down
6 changes: 6 additions & 0 deletions home/konrad/common/global/modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
imports = builtins.attrValues (import ./../modules);

konrad.programs.tmux.enable = true;
konrad.programs.ssh-egress.enable = true;
}
2 changes: 0 additions & 2 deletions home/konrad/common/global/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
up

rfv
tmux-sessionizer
tmux-windowizer
] ++ lib.optionals pkgs.stdenvNoCC.isLinux [
psmisc
trace-cmd
Expand Down
4 changes: 0 additions & 4 deletions home/konrad/common/global/shells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ in
watch = "viddy";
# For a full list of active aliases, run `alias`.
# to run command that is shadowed by an alias run (for example): \ls or command ls
# prime
txs = "${pkgs.tmux-sessionizer}/bin/tmux-sessionizer";
txw = "${pkgs.tmux-windowizer}/bin/tmux-windowizer";
txr = "${pkgs.tmux-switcher}/bin/tmux-switcher";
# faster navigation
".." = "cd ..";
"..." = "cd ../..";
Expand Down
179 changes: 0 additions & 179 deletions home/konrad/common/global/tmux.nix

This file was deleted.

1 change: 1 addition & 0 deletions home/konrad/common/modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
alacritty = import ./alacritty.nix;
bitwarden = import ./bitwarden.nix;
gpg-agent = import ./gpg-agent.nix;
tmux = import ./tmux;
restic = import ./restic.nix;
ssh-egress = import ./ssh-egress.nix;
syncthing = import ./syncthing.nix;
Expand Down
76 changes: 76 additions & 0 deletions home/konrad/common/modules/tmux/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{ tmux-switcher, tmuxTextProcessor, pkgs, ... }:
''
## KONRAD's SENSIBLE DEFAULTS
# tmux messages are displayed for 4 seconds
set-option -g display-time 4000
# upgrade color and fix italics
set -ga terminal-overrides ",-256color:Tc,alacritty:Tc"
# focus events enabled for terminals that support them
set-option -g focus-events on
# refresh interval
set-option -g status-interval 60
# Let the window to be renamed automatically when launching a process
set-option -g automatic-rename on
# But prevent renaming once you have manually changed it. And you can re-rename it after.
set-option -g allow-rename off
set-option -g set-titles on
# renumber so that we have 1,2,3 always
set-option -g renumber-windows on
# Set window notifications
set-option -g monitor-activity on
set-option -g visual-activity off
# vim splits
bind-key v split-window -h
bind-key g split-window -v
# vim panes
bind-key h select-pane -L
bind-key j select-pane -D
bind-key k select-pane -U
bind-key l select-pane -R
# vim pane resize
bind-key < resize-pane -L 10
bind-key > resize-pane -R 10
bind-key - resize-pane -D 10
bind-key + resize-pane -U 10
# Setup 'v' to begin selection, just like vim
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection
bind-key -T copy-mode-vi 'V' send-keys -X select-line
bind-key -T copy-mode-vi 'r' send-keys -X rectangle-toggle
# Setup 'y' to yank (copy), just like VIM
bind-key -T copy-mode-vi 'y' send-keys -X copy-selection-and-cancel
# Setup P (capital) to paste after the prefix keys, JUST LIKE VIM
bind P paste-buffer
# Use m to toggle mouse mode
unbind m
bind-key m setw mouse
# enable mouse by default, useful for resizing
set-option -g mouse on
# when nested tmux session,
# C-a will send the prefix directly to the remote session
# C-A clashes with C-A in neovim!
#bind-key -n C-a send-prefix
# facebook pathpicker
bind-key F run-shell -b "${tmuxTextProcessor} '${pkgs.fpp}/bin/fpp' '#{pane_id}' '#{pane_current_path}'"
# urlscan
bind-key U run-shell -b "${tmuxTextProcessor} '${pkgs.urlscan}/bin/urlscan' '#{pane_id}' '#{pane_current_path}'"
# tmux session switcher
bind-key r run-shell -b "${tmux-switcher}/bin/tmux-switcher"
# toggle last window
bind-key W last-window
# toggle last session
bind-key S switch-client -l
''
71 changes: 71 additions & 0 deletions home/konrad/common/modules/tmux/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.konrad.programs.tmux;
tmuxTextProcessor = pkgs.callPackage ./text_processor.nix { };
tmux-sessionizer = pkgs.callPackage ./tmux-sessionizer { };
tmux-switcher = pkgs.callPackage ./tmux-switcher { };
tmux-windowizer = pkgs.callPackage ./tmux-windowizer { };
baseConfig = pkgs.callPackage ./config.nix { inherit tmuxTextProcessor tmux-switcher; };
themeConfig = import ./theme.nix { inherit (cfg) colorscheme; };
in
{
options.konrad.programs.tmux = {
enable = mkEnableOption "Enables personalized tmux through home-manager";

colorscheme = lib.mkOption {
type = lib.types.nullOr lib.types.attrs;
default = config.colorscheme;
description = "Colorscheme attrset compatible with nix-colors format.";
example = "config.colorscheme";
};
};

config = mkIf cfg.enable
{
programs.tmux = {
enable = true;
aggressiveResize = true;
sensibleOnTop = true;
# tmux-256color is the proper one to enable italics
# just ensure you have that terminfo, newer ncurses provide it
# Macos does not have it but we fix that by installing ncurses through nix-darwin
# screen-256color works properly everywhere but does not have italics
terminal = "tmux-256color";
keyMode = "vi";
escapeTime = 0;
baseIndex = 1;
historyLimit = 50000;
extraConfig = lib.concatStringsSep "\n" [ baseConfig themeConfig ];
plugins = [ ];
};

programs.git.ignores = [
".tmux.sh"
];

programs.fzf.tmux.enableShellIntegration = true;

programs.zsh = {
initExtra = ''
# tmux baby
# (this cannot be a zsh widget unfortunately, tmux attach can only attach to a terminal,
# but zsh widgets do not allocate/reuse current terminal)
__txs() { ${pkgs.tmux-sessionizer}/bin/tmux-sessionizer }
bindkey -s '^F' '^u__txs^M'
# fix for ssh socket and display env var in tmux
# run after attaching to a remote session for a 2+ time if you need it
tmux-refresh() {
eval "$(tmux show-environment -s SSH_AUTH_SOCK)"
eval "$(tmux show-environment -s DISPLAY)"
}
'';
shellAliases = {
txs = "${tmux-sessionizer}/bin/tmux-sessionizer";
txw = "${tmux-windowizer}/bin/tmux-windowizer";
txr = "${tmux-switcher}/bin/tmux-switcher";
};
};
};
}
14 changes: 14 additions & 0 deletions home/konrad/common/modules/tmux/text_processor.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ tmux, writeShellScript, ... }:
let
tmux' = "${tmux}/bin/tmux";
in
writeShellScript "tmux_text_processor" ''
program="$1"
paneid="$2"
currentpanepath="$3"
capturename="$(basename $program)-$paneid"
showandpipe="${tmux'} show-buffer -b '$capturename' | $program || true; ${tmux'} delete-buffer -b '$capturename'"
${tmux'} capture-pane -J -S - -E - -b "$capturename" -t "$paneid"
${tmux'} split-window -c "$currentpanepath" "$showandpipe"
''
Loading

0 comments on commit 897a924

Please sign in to comment.