local t=require("\119\101\122\116\101\114\109")local z=require("\117\116\105\108\115")local R=(require("\099\111\108\111\114\115\046\111\110\101\100\097\114\107")).colors local A={}if t.config_builder then A=t.config_builder()end A.color_scheme="\079\110\101\068\097\114\107 \040\098\097\115\101\049\054\041"A.font=t.font("\070\105\114\097\077\111\110\111 \078\101\114\100 \070\111\110\116 \077\111\110\111")A.font_size=16 A.tab_max_width=20 A.window_decorations="\082\069\083\073\090\069"A.leader={key="\111",mods="\067\084\082\076";timeout_milliseconds=2000}A.keys={{mods="\076\069\065\068\069\082";key="\099",action=t.action.SpawnTab("\067\117\114\114\101\110\116\080\097\110\101\068\111\109\097\105\110")};{mods="\076\069\065\068\069\082";key="\120",action=t.action.CloseCurrentPane({confirm=true})};{mods="\076\069\065\068\069\082",key="\124",action=t.action.SplitHorizontal({domain="\067\117\114\114\101\110\116\080\097\110\101\068\111\109\097\105\110"})},{mods="\076\069\065\068\069\082",key="-";action=t.action.SplitVertical({domain="\067\117\114\114\101\110\116\080\097\110\101\068\111\109\097\105\110"})};{mods="\076\069\065\068\069\082";key="\115",action=t.action.PaneSelect({mode="\083\119\097\112\087\105\116\104\065\099\116\105\118\101"})},{key="\058";mods="\076\069\065\068\069\082",action=t.action.ActivateCommandPalette},{mods="\076\069\065\068\069\082";key="\112";action=t.action.ActivateTabRelative(-1)};{mods="\076\069\065\068\069\082";key="\110",action=t.action.ActivateTabRelative(1)};{mods="\076\069\065\068\069\082",key="\104";action=t.action.ActivatePaneDirection("\076\101\102\116")},{mods="\076\069\065\068\069\082",key="\106";action=t.action.ActivatePaneDirection("\068\111\119\110")},{mods="\076\069\065\068\069\082",key="\107";action=t.action.ActivatePaneDirection("\085\112")},{mods="\076\069\065\068\069\082";key="\108";action=t.action.ActivatePaneDirection("\082\105\103\104\116")};{key="\093";mods="\067\077\068",action=t.action_callback(function(z,R)local A=(R:tab()):panes_with_info()local V=false for t,z in ipairs(A)do if z.is_zoomed then V=true break end end t.log_info(string.format("\105\115\095\122\111\111\109\101\100 \037\115",tostring(V)))if#A==1 then return end if V then z:perform_action(t.action.Multiple({t.action.SetPaneZoomState(false),t.action.ActivatePaneDirection("\078\101\120\116");t.action.SetPaneZoomState(true)}),R)else z:perform_action(t.action.ActivatePaneDirection("\078\101\120\116"),R)end end)};{mods="\076\069\065\068\069\082";key="\076\101\102\116\065\114\114\111\119",action=t.action.AdjustPaneSize({"\076\101\102\116",5})};{mods="\076\069\065\068\069\082";key="\082\105\103\104\116\065\114\114\111\119";action=t.action.AdjustPaneSize({"\082\105\103\104\116";5})};{mods="\076\069\065\068\069\082";key="\068\111\119\110\065\114\114\111\119";action=t.action.AdjustPaneSize({"\068\111\119\110",5})};{mods="\076\069\065\068\069\082";key="\085\112\065\114\114\111\119";action=t.action.AdjustPaneSize({"\085\112",5})};{mods="\076\069\065\068\069\082";key="\122";action=t.action.TogglePaneZoomState},{key="\096";mods="\067\084\082\076";action=t.action_callback(function(t,z)local R=z:tab()local A=R:panes_with_info()if#A==1 then z:split({direction="\066\111\116\116\111\109",size=.3})elseif not A[1].is_zoomed then A[1].pane:activate()R:set_zoomed(true)elseif A[1].is_zoomed then R:set_zoomed(false)A[2].pane:activate()end end)},{key="\114",mods="\067\077\068";action=t.action.PromptInputLine({description="\069\110\116\101\114 \110\101\119 \110\097\109\101 \102\111\114 \116\097\098",action=t.action_callback(function(t,z,R)if R then(t:active_tab()):set_title(R)end end)})},{key="\091",mods="\076\069\065\068\069\082",action=t.action.ActivateCopyMode};{key="\107";mods="\067\077\068",action=t.action.ActivatePaneDirection("\082\105\103\104\116")}}for z=0,9,1 do table.insert(A.keys,{key=tostring(z);mods="\076\069\065\068\069\082";action=t.action.ActivateTab(z)})end A.hide_tab_bar_if_only_one_tab=false A.tab_bar_at_bottom=true A.use_fancy_tab_bar=false A.tab_and_split_indices_are_zero_based=true t.on("\117\112\100\097\116\101-\114\105\103\104\116-\115\116\097\116\117\115",function(z,R)local A=""local V={Foreground={Color="\035\048\048\048"}}local Z=""if z:leader_is_active()then Z=" \240\159\140\138"A=utf8.char(57522)end if(z:active_tab()):tab_id()~=0 then V={Foreground={Color="\035\051\051\051\051\051\051"}}end z:set_left_status(t.format({{Background={Color="\035\098\055\098\100\102\056"}};{Text=Z};V,{Text=A}}))end)t.on("\102\111\114\109\097\116-\116\097\098-\116\105\116\108\101",function(A)return t.format({{Attribute={Intensity="\066\111\108\100"}};{Text=string.format(" \037\115\046 ",A.tab_index+1)},"\082\101\115\101\116\065\116\116\114\105\098\117\116\101\115";{Text=z.tab_title(A)},{Foreground={Color=R.base}};{Text=" \226\150\149"}})end)return A
-- Copied originally from https://github.com/dragonlobster/wezterm-config
-- https://github.com/hadynz/dotfiles/blob/main/wezterm/wezterm-multiplex.lua

-- Pull in the wezterm API
local wezterm = require("wezterm")
local utils = require("utils")
local colors = require("colors.onedark").colors

-- This table will hold the configuration.
local config = {}

-- In newer versions of wezterm, use the config_builder which will
-- help provide clearer error messages
if wezterm.config_builder then
  config = wezterm.config_builder()
end

-- For example, changing the color scheme:
-- config.color_scheme = "Catppuccin Macchiato"
-- config.font = wezterm.font("JetBrains Mono NL")
config.color_scheme = "OneDark (base16)"
config.font = wezterm.font("FiraMono Nerd Font Mono")
config.font_size = 16
config.tab_max_width = 20

config.window_decorations = "RESIZE"

-- tmux
config.leader = { key = "o", mods = "CTRL", timeout_milliseconds = 2000 }
config.keys = {
  {
    mods = "LEADER",
    key = "c",
    action = wezterm.action.SpawnTab("CurrentPaneDomain"),
  },
  {
    mods = "LEADER",
    key = "x",
    action = wezterm.action.CloseCurrentPane({ confirm = true }),
  },

  -- Splitting
  {
    mods = "LEADER",
    key = "|",
    action = wezterm.action.SplitHorizontal({ domain = "CurrentPaneDomain" }),
  },
  {
    mods = "LEADER",
    key = "-",
    action = wezterm.action.SplitVertical({ domain = "CurrentPaneDomain" }),
  },

  -- Swap panes
  {
    mods = "LEADER",
    key = "s",
    action = wezterm.action.PaneSelect({
      mode = "SwapWithActive",
    }),
  },

  -- Palette
  {
    key = ":",
    mods = "LEADER",
    action = wezterm.action.ActivateCommandPalette,
  },

  -- Tab Navigation
  {
    mods = "LEADER",
    key = "p",
    action = wezterm.action.ActivateTabRelative(-1),
  },
  {
    mods = "LEADER",
    key = "n",
    action = wezterm.action.ActivateTabRelative(1),
  },

  -- Tab Navigation
  {
    mods = "LEADER",
    key = "h",
    action = wezterm.action.ActivatePaneDirection("Left"),
  },
  {
    mods = "LEADER",
    key = "j",
    action = wezterm.action.ActivatePaneDirection("Down"),
  },
  {
    mods = "LEADER",
    key = "k",
    action = wezterm.action.ActivatePaneDirection("Up"),
  },
  {
    mods = "LEADER",
    key = "l",
    action = wezterm.action.ActivatePaneDirection("Right"),
  },

  -- Cycle Panes Forward/Backward
  -- Keep zoomed state when active.
  {
    key = "]",
    mods = "CMD",
    action = wezterm.action_callback(function(win, pane)
      local panes = pane:tab():panes_with_info()
      local is_zoomed = false
      for _, p in ipairs(panes) do
        if p.is_zoomed then
          is_zoomed = true
          break
        end
      end

      wezterm.log_info(string.format("is_zoomed %s", tostring(is_zoomed)))

      if #panes == 1 then
        return
      end

      if is_zoomed then
        win:perform_action(
          wezterm.action.Multiple({
            wezterm.action.SetPaneZoomState(false),
            wezterm.action.ActivatePaneDirection("Next"),
            wezterm.action.SetPaneZoomState(true),
          }),
          pane
        )
      else
        win:perform_action(wezterm.action.ActivatePaneDirection("Next"), pane)
      end
    end),
  },

  -- Resizing
  {
    mods = "LEADER",
    key = "LeftArrow",
    action = wezterm.action.AdjustPaneSize({ "Left", 5 }),
  },
  {
    mods = "LEADER",
    key = "RightArrow",
    action = wezterm.action.AdjustPaneSize({ "Right", 5 }),
  },
  {
    mods = "LEADER",
    key = "DownArrow",
    action = wezterm.action.AdjustPaneSize({ "Down", 5 }),
  },
  {
    mods = "LEADER",
    key = "UpArrow",
    action = wezterm.action.AdjustPaneSize({ "Up", 5 }),
  },

  -- Zoom/Zen
  {
    mods = "LEADER",

    key = "z",
    action = wezterm.action.TogglePaneZoomState,
  },

  -- Toggle terminal panes
  {
    key = "`",
    mods = "CTRL",
    action = wezterm.action_callback(function(_, pane)
      local tab = pane:tab()
      local panes = tab:panes_with_info()
      if #panes == 1 then
        pane:split({
          direction = "Bottom",
          size = 0.3,
        })
      elseif not panes[1].is_zoomed then
        panes[1].pane:activate()
        tab:set_zoomed(true)
      elseif panes[1].is_zoomed then
        tab:set_zoomed(false)
        panes[2].pane:activate()
      end
    end),
  },
  -- Tab rename
  {
    key = "r",
    mods = "CMD",
    action = wezterm.action.PromptInputLine({
      description = "Enter new name for tab",
      action = wezterm.action_callback(function(window, _, line)
        if line then
          window:active_tab():set_title(line)
        end
      end),
    }),
  },

  -- Copy Mode
  {
    key = "[",
    mods = "LEADER",
    action = wezterm.action.ActivateCopyMode,
  },

  -- Clear Terminal
  {
    key = "k",
    mods = "CMD",
    -- action = wezterm.action.ClearScrollback('ScrollbackAndViewport'),
    action = wezterm.action.ActivatePaneDirection("Right"),
  },
}

for i = 0, 9 do
  -- leader + number to activate that tab
  table.insert(config.keys, {
    key = tostring(i),
    mods = "LEADER",
    action = wezterm.action.ActivateTab(i),
  })
end

-- tab bar
config.hide_tab_bar_if_only_one_tab = false
config.tab_bar_at_bottom = true
config.use_fancy_tab_bar = false
config.tab_and_split_indices_are_zero_based = true

-- tmux status
wezterm.on("update-right-status", function(window, _)
  local SOLID_LEFT_ARROW = ""
  local ARROW_FOREGROUND = { Foreground = { Color = "#000" } }
  local prefix = ""

  if window:leader_is_active() then
    prefix = " 🌊"
    SOLID_LEFT_ARROW = utf8.char(0xe0b2)
  end

  if window:active_tab():tab_id() ~= 0 then
    -- arrow color based on if tab is first pane
    ARROW_FOREGROUND = { Foreground = { Color = "#333333" } }
  end

  window:set_left_status(wezterm.format({
    { Background = { Color = "#b7bdf8" } }, -- bg when active
    { Text = prefix },
    ARROW_FOREGROUND,
    { Text = SOLID_LEFT_ARROW },
  }))
end)

-- Tab title
wezterm.on("format-tab-title", function(tab)
  return wezterm.format({
    { Attribute = { Intensity = "Bold" } },
    { Text = string.format(" %s. ", tab.tab_index + 1) },
    "ResetAttributes",
    { Text = utils.tab_title(tab) },
    { Foreground = { Color = colors.base } },
    { Text = " ▕" },
  })
end)

-- and finally, return the configuration to wezterm
return config
