-- wezterm.lua
-- __      __      _
-- \ \    / /__ __| |_ ___ _ _ _ __
--  \ \/\/ / -_)_ /  _/ -_) '_| '  \
--   \_/\_/\___/__|\__\___|_| |_|_|_|
--
-- github.com/theopn/dotfiles/blob/main/wezterm/wezterm.lua
-- hackernoon.com/get-the-most-out-of-your-terminal-a-comprehensive-guide-to-wezterm-configuration

local wezterm = require("wezterm")
local act = wezterm.action
local mux = wezterm.mux

local config = {}
-- use config builder for object if possible
if wezterm.config_builder then
	config = wezterm.config_builder()
end

-- settings
config.default_prog = { "/usr/bin/bash", "-l" }
--config.enable_wayland = true
config.color_scheme = "catppuccin-mocha"
-- config.color_scheme = "Tokyo Night"
config.font = wezterm.font_with_fallback({
	{ family = "MesloLGS Nerd Font", scale = 1.1, weight = "Medium" },
	{ family = "FantasqueSansM Nerd Font", scale = 1.3 },
	{ family = "Noto Color Emoji" },
})
config.window_background_opacity = 0.9
config.window_decorations = "RESIZE"
config.window_close_confirmation = "AlwaysPrompt"
config.scrollback_lines = 3000
config.default_workspace = "main"

-- Dim inactive panes
config.inactive_pane_hsb = {
	saturation = 0.24,
	brightness = 0.5,
}

-- keys
config.leader = { key = "a", mods = "CTRL", timeout_milliseconds = 1000 }
config.keys = {
	-- send C-a when pressing C-a twice
	{ key = "a", mods = "LEADER|CTRL", action = act.SendKey({ key = "a", mods = "CTRL" }) },
	{ key = "c", mods = "LEADER", action = act.ActivateCopyMode },
	{ key = "phys:Space", mods = "LEADER", action = act.ActivateCommandPalette },

	-- pane keybindings
	{ key = "s", mods = "LEADER", action = act.SplitVertical({ domain = "CurrentPaneDomain" }) },
	{ key = "v", mods = "LEADER", action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }) },
	{ key = "h", mods = "LEADER", action = act.ActivatePaneDirection("Left") },
	{ key = "j", mods = "LEADER", action = act.ActivatePaneDirection("Down") },
	{ key = "k", mods = "LEADER", action = act.ActivatePaneDirection("Up") },
	{ key = "l", mods = "LEADER", action = act.ActivatePaneDirection("Right") },
	{ key = "q", mods = "LEADER", action = act.CloseCurrentPane({ confirm = true }) },
	{ key = "z", mods = "LEADER", action = act.TogglePaneZoomState },
	{ key = "o", mods = "LEADER", action = act.RotatePanes("Clockwise") },
	-- we can make keybindings for resizing panes
	-- we are going to use KeyTable
	{ key = "r", mods = "LEADER", action = act.ActivateKeyTable({ name = "resize_pane", one_shot = false }) },

	-- tab keybindins
	{ key = "PageDown", mods = "CTRL", action = act.ActivateTabRelative(1) },
	{ key = "PageUp", mods = "CTRL", action = act.ActivateTabRelative(-1) },
	{ key = "[", mods = "LEADER", action = act.ActivateTabRelative(-1) },
	{ key = "]", mods = "LEADER", action = act.ActivateTabRelative(1) },
	{ key = "h", mods = "LEADER", action = act.ActivateTabRelative(-1) },
	{ key = "l", mods = "LEADER", action = act.ActivateTabRelative(1) },
	{ key = "n", mods = "LEADER", action = act.ShowTabNavigator },
	{ key = "t", mods = "CTRL", action = act.SpawnTab("CurrentPaneDomain") },
	{ key = "t", mods = "LEADER", action = act.SpawnTab("CurrentPaneDomain") },
	{ key = "w", mods = "CTRL", action = act.CloseCurrentTab({ confirm = false }) },
	{
		key = "e",
		mods = "LEADER",
		action = act.PromptInputLine({
			description = wezterm.format({
				{ Attribute = { Intensity = "Bold" } },
				{ Foreground = { AnsiColor = "Fuchsia" } },
				{ Text = "Renaming Tab Title...:" },
			}),
			action = wezterm.action_callback(function(window, pane, line)
				if line then
					window:active_tab():set_title(line)
				end
			end),
		}),
	},
	{ key = "m", mods = "LEADER", action = act.ActivateKeyTable({ name = "move_tab", one_shot = false }) },
	-- Or shortcuts to move tab w/o move_tab table. SHIFT is for when caps lock is on
	{ key = "{", mods = "LEADER|SHIFT", action = act.MoveTabRelative(-1) },
	{ key = "}", mods = "LEADER|SHIFT", action = act.MoveTabRelative(1) },

	{ key = "w", mods = "LEADER", action = act.ShowLauncherArgs({ flags = "FUZZY|WORKSPACES" }) },
}

-- I can use the tab navigator (LDR t), but I also want to quickly navigate tabs with index
for i = 1, 9 do
	table.insert(config.keys, {
		key = tostring(i),
		mods = "LEADER",
		action = act.ActivateTab(i - 1),
	})
end

config.key_tables = {
	resize_pane = {
		{ key = "h", mods = "LEADER", action = act.AdjustPaneSize({ "Left", 1 }) },
		{ key = "j", mods = "LEADER", action = act.AdjustPaneSize({ "Down", 1 }) },
		{ key = "k", mods = "LEADER", action = act.AdjustPaneSize({ "Up", 1 }) },
		{ key = "l", mods = "LEADER", action = act.AdjustPaneSize({ "Right", 1 }) },
		{ key = "Escape", mods = "LEADER", action = "PopKeyTable" },
		{ key = "Enter", mods = "LEADER", action = "PopKeyTable" },
	},
	move_tab = {
		{ key = "h", mods = "LEADER", action = act.MoveTabRelative(-1) },
		{ key = "j", mods = "LEADER", action = act.MoveTabRelative(-1) },
		{ key = "k", mods = "LEADER", action = act.MoveTabRelative(1) },
		{ key = "l", mods = "LEADER", action = act.MoveTabRelative(1) },
		{ key = "Escape", mods = "LEADER", action = "PopKeyTable" },
		{ key = "Enter", mods = "LEADER", action = "PopKeyTable" },
	},
}

-- tab bar
config.use_fancy_tab_bar = false
config.status_update_interval = 1000
config.tab_bar_at_bottom = false

window_frame = {
	font = wezterm.font({ family = "Noto Sans", weight = "Regular" }),
}

wezterm.on("update-status", function(window, pane)
	local stat = window:active_workspace()
	local stat_color = "#f7768e"
	if window:active_key_table() then
		stat = window:active_key_table()
		stat_color = "#7dcfff"
	end
	if window:leader_is_active() then
		stat = "LDR"
		stat_color = "#bb9af7"
	end

	local basename = function(s)
		return string.gsub(s, "(.*[/\\])(.*)", "%2")
	end

	local cwd = pane:get_current_working_dir()
	if cwd then
		if type(cwd) then
			cwd = basename(cwd.file_path)
		else
			cwd = basename(cwd)
		end
	else
		cwd = ""
	end

	local cmd = pane:get_foreground_process_name()
	cmd = cmd and basename(cmd) or ""

	local time = wezterm.strftime("%H:%M")

	-- left status
	window:set_left_status(wezterm.format({
		{ Foreground = { Color = stat_color } },
		{ Text = "  " },
		{ Text = wezterm.nerdfonts.oct_table .. "  " .. stat },
		{ Text = " |" },
	}))

	-- right status
	window:set_right_status(wezterm.format({
		{ Text = wezterm.nerdfonts.md_folder .. "  " .. cwd },
		{ Text = " | " },
		{ Foreground = { Color = "#e0af68" } },
		{ Text = wezterm.nerdfonts.fa_code .. "  " .. cmd },
		"ResetAttributes",
		{ Text = " | " },
		{ Text = wezterm.nerdfonts.md_clock .. "  " .. time },
		{ Text = "  " },
	}))
end)

local SOLID_LEFT_ARROW = wezterm.nerdfonts.pl_right_hard_divider
local SOLID_RIGHT_ARROW = wezterm.nerdfonts.pl_left_hard_divider

local SOLID_LEFT_CIRCLE = wezterm.nerdfonts.ple_left_half_circle_thick
local SOLID_RIGHT_CIRCLE = wezterm.nerdfonts.ple_right_half_circle_thick

function tab_title(tab_info)
	local title = tab_info.tab_title
	if title and #title > 0 then
		return title
	end
	return tab_info.active_pane.title
end

config.tab_max_width = 30
config.show_tab_index_in_tab_bar = true
config.tab_and_split_indices_are_zero_based = false
--
--wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
--	--local edge_background = "#0b0022"
--	local edge_background = "#333333"
--	local background = "#181825"
--	local foreground = "#cdd6f4"
--	--
--	if tab.is_active then
--		background = "#cba6f7"
--		foreground = "#11111b"
--	elseif hover then
--		background = "#45475a"
--		foreground = "#cdd6f4"
--	end
--
--	local edge_foreground = background
--
--	local title = tab_title(tab)
--
--	title = wezterm.truncate_right(title, max_width - 2)
--
--	return {
--		{ Background = { Color = edge_background } },
--		{ Foreground = { Color = edge_foreground } },
--		--		{ Text = " " },
--		{ Text = SOLID_LEFT_CIRCLE },
--		{ Background = { Color = background } },
--		{ Foreground = { Color = foreground } },
--		{ Text = " " },
--		{ Text = title },
--		{ Text = " " },
--		{ Background = { Color = edge_background } },
--		{ Foreground = { Color = edge_foreground } },
--		{ Text = SOLID_RIGHT_CIRCLE },
--		--		{ Text = " " },
--	}
--end)

config.ssh_domains = wezterm.default_ssh_domains()
for _, dom in ipairs(config.ssh_domains) do
	dom.assume_shell = "Posix"
end

return config
