From 1a1acba0e78274a69f0797b2e9dfd3d00e75f6a2 Mon Sep 17 00:00:00 2001
From: Daniel Lundin <daniel@arity.se>
Date: Sat, 29 Apr 2023 11:33:39 +0200
Subject: [PATCH] Return of the tmux

---
 .config/starship.toml                         | 14 +++-
 .config/wezterm/wezterm.lua                   | 79 +------------------
 .../applications/wezterm-dln-dev.desktop      |  2 +-
 .../share/applications/wezterm-local.desktop  | 11 +++
 .tmux.conf                                    | 24 +++---
 .zshrc                                        |  2 +-
 bin/tmux-build-helper                         |  4 +
 bin/{wezterm-edit-helper => tmux-edit-helper} | 15 ++--
 8 files changed, 47 insertions(+), 104 deletions(-)
 create mode 100644 .local/share/applications/wezterm-local.desktop
 create mode 100755 bin/tmux-build-helper
 rename bin/{wezterm-edit-helper => tmux-edit-helper} (70%)

diff --git a/.config/starship.toml b/.config/starship.toml
index be93261..ab1eeda 100644
--- a/.config/starship.toml
+++ b/.config/starship.toml
@@ -1,20 +1,26 @@
 add_newline = false
 
-# $git_branch\
-# $git_commit\
-# $git_state\
-# $git_status\
 
 format = """\
+${env_var.TMUX_WINDOW}\
 $hostname\
 $directory\
 ${custom.sl_status}\
+$git_branch\
+$git_commit\
+$git_state\
+$git_status\
 ($character)\
 """
 
 right_format = """\
 """
 
+[env_var.TMUX_WINDOW]
+default = ""
+format = '[ $env_value ]($style)[](fg:#d1002f) '
+style = 'italic bg:#d1002f fg:#ffffff'
+
 [custom.sl_status]
 command = '''~/bin/starship-sl-status'''
 when = false
diff --git a/.config/wezterm/wezterm.lua b/.config/wezterm/wezterm.lua
index 3239f30..25aef7c 100644
--- a/.config/wezterm/wezterm.lua
+++ b/.config/wezterm/wezterm.lua
@@ -10,70 +10,6 @@ local function font_with_fallback(name, params)
 	return wezterm.font_with_fallback(names, params)
 end
 
-wezterm.on("gui-startup", function(cmd)
-	local args = {}
-	if cmd then
-		args = cmd.args
-	end
-
-	local tab, pane, window = mux.spawn_window({
-		workspace = "local",
-		args = { "nvim", "--listen", os.getenv("XDG_RUNTIME_DIR") .. "/nvim-persistent.sock" },
-	})
-	tab:set_title("nvim")
-
-	for _ = 1, 9 do
-		window:spawn_tab({})
-	end
-	window:gui_window():perform_action(act.ActivateTab(1), pane)
-
-	local tab, pane, window = mux.spawn_window({
-		workspace = dev_host,
-		args = spawn_dev_nvim,
-	})
-	tab:set_title("nvim")
-
-	for _ = 1, 9 do
-		window:spawn_tab({ args = { "ssh", dev_host } })
-	end
-
-	mux.set_active_workspace("local")
-end)
-
-local function activate_nvim(window, pane)
-	for _, t in ipairs(window:mux_window():tabs_with_info()) do
-		for _, p in ipairs(t.tab:panes()) do
-			if p:get_title() == "nvim" or t.tab:get_title() == "nvim" then
-				window:perform_action(
-					act.Multiple({
-						act.ActivateTab(t.index),
-						act.MoveTab(0),
-					}),
-					pane
-				)
-				return
-			end
-		end
-	end
-
-	local nvim = { "nvim", "--listen", os.getenv("XDG_RUNTIME_DIR") .. "/nvim-persistent.sock" }
-	if window:mux_window():get_workspace() == dev_host then
-		nvim = spawn_dev_nvim
-	end
-
-	local tab, pane, _ = window:mux_window():spawn_tab({ args = nvim })
-	window:perform_action(act.MoveTab(0), pane)
-	tab:set_title("nvim")
-end
-
-wezterm.on("user-var-changed", function(window, pane, name, value)
-	if name == "nvim_activate" then
-		activate_nvim(window, pane)
-	end
-end)
-
-wezterm.on("activate-nvim", activate_nvim)
-
 wezterm.add_to_config_reload_watch_list(os.getenv("HOME") .. "/.config/shelman-theme/current/wezterm")
 
 return {
@@ -115,6 +51,7 @@ return {
 	initial_cols = 132,
 	initial_rows = 45,
 	use_resize_increments = true,
+	window_decorations = "RESIZE",
 	window_background_opacity = 1.0,
 	colors = {
 		tab_bar = {
@@ -127,6 +64,7 @@ return {
 	default_cursor_style = "SteadyBlock",
 	cursor_thickness = "3px",
 	cursor_blink_rate = 0,
+	hide_mouse_cursor_when_typing = false,
 	enable_wayland = true,
 	enable_tab_bar = false,
 	tab_bar_at_bottom = true,
@@ -149,19 +87,6 @@ return {
 		{ key = "Enter", mods = "ALT", action = "ToggleFullScreen" },
 		{ key = "r", mods = "ALT", action = act.ReloadConfiguration },
 		{ key = "L", mods = "CTRL", action = wezterm.action.ShowDebugOverlay },
-		-- mux
-		{ key = "1", mods = "ALT", action = act.EmitEvent("activate-nvim") },
-		{ key = "2", mods = "ALT", action = act.ActivateTab(1) },
-		{ key = "3", mods = "ALT", action = act.ActivateTab(2) },
-		{ key = "4", mods = "ALT", action = act.ActivateTab(3) },
-		{ key = "5", mods = "ALT", action = act.ActivateTab(4) },
-		{ key = "6", mods = "ALT", action = act.ActivateTab(5) },
-		{ key = "7", mods = "ALT", action = act.ActivateTab(6) },
-		{ key = "8", mods = "ALT", action = act.ActivateTab(7) },
-		{ key = "9", mods = "ALT", action = act.ActivateTab(8) },
-		{ key = "0", mods = "ALT", action = act.ActivateTab(9) },
-		{ key = "RightArrow", mods = "CTRL", action = act.ActivateTabRelative(1) },
-		{ key = "LeftArrow", mods = "CTRL", action = act.ActivateTabRelative(-1) },
 		{ key = "l", mods = "ALT", action = wezterm.action.ActivateCommandPalette },
 		{ key = "Backspace", mods = "ALT", action = act.SwitchWorkspaceRelative(1) },
 		{ key = "UpArrow", mods = "SHIFT", action = act.ScrollByLine(-1) },
diff --git a/.local/share/applications/wezterm-dln-dev.desktop b/.local/share/applications/wezterm-dln-dev.desktop
index ae83c85..8f8a4e7 100644
--- a/.local/share/applications/wezterm-dln-dev.desktop
+++ b/.local/share/applications/wezterm-dln-dev.desktop
@@ -7,4 +7,4 @@ TryExec=/usr/bin/wezterm
 Icon=org.wezfurlong.wezterm
 Terminal=false
 Categories=System;TerminalEmulator;
-Exec=/usr/bin/wezterm start --always-new-process --class=wezterm-dln-dev
+Exec=/usr/bin/wezterm --config 'default_prog={"ssh", "-q", "dln-dev", "--", "tmux", "-u", "new", "-As0"}' start --class=wezterm-dln-dev
diff --git a/.local/share/applications/wezterm-local.desktop b/.local/share/applications/wezterm-local.desktop
new file mode 100644
index 0000000..c425874
--- /dev/null
+++ b/.local/share/applications/wezterm-local.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Name=wezterm-local
+GenericName=wezterm-local
+StartupWMClass=wezterm-local
+Type=Application
+TryExec=/usr/bin/wezterm
+Icon=org.wezfurlong.wezterm
+Terminal=false
+Categories=System;TerminalEmulator;
+Exec=/usr/bin/wezterm --config 'default_prog={"tmux", "-u", "new", "-As0"}' start
+
diff --git a/.tmux.conf b/.tmux.conf
index 297c1f2..11ef005 100644
--- a/.tmux.conf
+++ b/.tmux.conf
@@ -53,9 +53,9 @@ bind-key -T copy-mode-vi WheelDownPane select-pane \; send-keys -X -N 1 scroll-d
 bind-key / copy-mode \; send-key ?
 bind-key P 'capture-pane' \; capture-pane -S - \; save-buffer /tmp/tmux \; delete-buffer
 bind-key -n C-\\ copy-mode \; send -X search-backward " % "\; send -X search-again
-bind -n M-g display-popup -S 'bg=#F1F8E9,fg=#689F38' -s 'bg=#F1F8E9' -h '90%' -w '90%' -b rounded -d '#{pane_current_path}' -E "gitui"
-bind -n M-e display-popup -S 'bg=#E3F2FD,fg=#0288D1' -s 'bg=#E3F2FD' -h '40%' -w '90%' -y 0 -b rounded -d '#{pane_current_path}' -e FZF=fzf -E "~/bin/tmux-edit-helper"
-bind -n M-m display-popup -S 'bg=#FFF8E1,fg=#FF6F00' -s 'bg=#FFF8E1' -h '90%' -w '90%' -b rounded -d '#{pane_current_path}' -E "~/bin/tmux-build-helper"
+bind -n M-g display-popup -h '90%' -w '90%' -b rounded -d '#{pane_current_path}' -E "gitui"
+bind -n M-e display-popup -h '40%' -w '90%' -y 0 -b rounded -d '#{pane_current_path}' -e FZF=fzf -E "~/bin/tmux-edit-helper"
+bind -n M-m display-popup -h '90%' -w '90%' -b rounded -d '#{pane_current_path}' -E "~/bin/tmux-build-helper"
 
 setw -g window-status-format ""
 setw -g window-status-current-format ""
@@ -71,14 +71,14 @@ set -g set-titles on
 set -g set-titles-string "#H - #T"
 
 new -s0 -n 1  nvim --listen $XDG_RUNTIME_DIR/nvim.sock
-new-window -n 2
-new-window -n 3
-new-window -n 4
-new-window -n 5
-new-window -n 6
-new-window -n 7
-new-window -n 8
-new-window -n 9
-new-window -n 0
+new-window -n 2 -e TMUX_WINDOW=2
+new-window -n 3 -e TMUX_WINDOW=3
+new-window -n 4 -e TMUX_WINDOW=4
+new-window -n 5 -e TMUX_WINDOW=5
+new-window -n 6 -e TMUX_WINDOW=6
+new-window -n 7 -e TMUX_WINDOW=7
+new-window -n 8 -e TMUX_WINDOW=8
+new-window -n 9 -e TMUX_WINDOW=9
+new-window -n 0 -e TMUX_WINDOW=0
 
 # vim:set ft=tmux:
diff --git a/.zshrc b/.zshrc
index a56a3ea..409fd34 100644
--- a/.zshrc
+++ b/.zshrc
@@ -105,7 +105,7 @@ export LESS="--mouse --wheel-lines=1 -nRXF"
 
 ## Aliases
 alias c='cut -c-${COLUMNS}'
-alias e='wezterm-edit-helper'
+alias e='tmux-edit-helper'
 alias dotgit='git --work-tree $HOME --git-dir $HOME/.dot_git'
 alias l=bat
 alias lw='bat --wrap=never'
diff --git a/bin/tmux-build-helper b/bin/tmux-build-helper
new file mode 100755
index 0000000..e27347c
--- /dev/null
+++ b/bin/tmux-build-helper
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+set -ef -o pipefail
+eval "$(direnv export bash)"
+eval "${BUILD_COMMAND:-echo 'BUILD_COMMAND not set'}" | bat --paging=always
diff --git a/bin/wezterm-edit-helper b/bin/tmux-edit-helper
similarity index 70%
rename from bin/wezterm-edit-helper
rename to bin/tmux-edit-helper
index 714b3d2..efe3e07 100755
--- a/bin/wezterm-edit-helper
+++ b/bin/tmux-edit-helper
@@ -16,14 +16,11 @@ fi
 
 _nvim_socket="$XDG_RUNTIME_DIR/nvim-persistent.sock"
 
-function _nvim_setenv() {
-	nvim --server "$_nvim_socket" --remote-expr "execute(\"let \$${1} = \\\"${2}\\\"\")" 2>&1 >/dev/null
-}
-
-_nvim_setenv BUILD_COMMAND "$BUILD_COMMAND"
-_nvim_setenv GOPACKAGESDRIVER "$GOPACKAGESDRIVER"
-_nvim_setenv GOFLAGS "$GOFLAGS"
+tmux select-window -t nvim 2>/dev/null ||
+	tmux new-window -S -n nvim \
+		-e "BUILD_COMMAND=$BUILD_COMMAND" \
+		-e "GOPACKAGESDRIVER=$GOPACKAGESDRIVER" \
+		-e "GOFLAGS=$GOFLAGS" \
+		nvim --listen "$_nvim_socket"
 
 nvim --server "$_nvim_socket" --remote "$_file"
-
-printf "\033]1337;SetUserVar=%s=%s\007" nvim_activate $(date +%s | base64)