local t={}function t.basename(t)return string.gsub(t,"\040\046\042\091\047\092\093\041\040\046\042\041","\037\050")end function t.get_current_working_dir(z)local R=z.active_pane.current_working_dir if R then local z=R.file_path return t.basename(z)end return""end function t.tab_title(z)local R=z.tab_title if#R==0 then R=t.get_current_working_dir(z)end if#R==0 then R=z.active_pane.title end if z.active_pane.is_zoomed then local A=z.active_pane local V=t.basename(A.foreground_process_name)R=R..("\058"..V)end return R end return t
local M = {}

-- Equivalent to POSIX basename(3)
-- Given "/foo/bar" returns "bar"
-- Given "c:\\foo\\bar" returns "bar"
function M.basename(s)
  return string.gsub(s, "(.*[/\\])(.*)", "%2")
end

function M.get_current_working_dir(tab)
  local current_dir = tab.active_pane.current_working_dir

  if current_dir then
    local path = current_dir.file_path
    return M.basename(path)
  end

  return ""
end

-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.
function M.tab_title(tab)
  -- If the tab title is explicitly set, take that
  local title = tab.tab_title

  -- Otherwise, try to use current working directory as the title
  if #title == 0 then
    title = M.get_current_working_dir(tab)
  end

  -- Otherwise, use the title from the active pane
  -- in that tab
  if #title == 0 then
    title = tab.active_pane.title
  end

  if tab.active_pane.is_zoomed then
    local pane = tab.active_pane
    local proc_name = M.basename(pane.foreground_process_name)
    title = title .. ":" .. proc_name
  end

  return title
end

return M
