repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

wezterm/bugabinga/status.lua

Raw
local wez = require 'wezterm'
local bat = require 'bugabinga.battery'

local DELIMITER = ' ยท '
local palette = require 'bugabinga.nugu.palette_dark'.palette
local icon_names = require 'bugabinga.icons'

local animation_tick = 0

local zoomed = function ( pane )
  local tab = pane:tab()
  local any_is_zoomed = false
  if tab then
    for _, info in ipairs( tab:panes_with_info() ) do
      if info.is_zoomed then
        any_is_zoomed = true
      end
    end
  end
  return any_is_zoomed and '๐Ÿ”Ž Zoomed Pane' or nil
end

local basename = function ( value )
  if not value or value == '' then return nil end
  value = value:gsub( '\\', '/' )
  return value:match( '([^/]+)$' ) or value
end

local first_command = function ( commandline )
  if not commandline or commandline == '' then return nil end
  local token = commandline:match( '^%s*([^%s|;&]+)' )
  return basename( token )
end

local normalize_program = function ( program )
  program = basename( program )
  if not program then return nil end
  program = program:lower()
  if program == 'vim.opti' then return 'vim' end
  if program == 'node.opti' then return 'node' end
  if program:find( '^python' ) then return 'python' end
  if program == 'lazygit' then return 'git' end
  return program
end

local safe_user_vars = function ( pane )
  local ok, vars = pcall( pane.get_user_vars, pane )
  if ok and vars then return vars end
  return {}
end

local safe_title = function ( pane )
  local ok, title = pcall( pane.get_title, pane )
  if ok then return title end
  return nil
end

local detect_program_from_title = function ( title )
  if not title then return nil end
  local lower = title:lower()
  if title:find( 'ฯ€' ) or lower:find( '%f[%w]pi%f[%W]' ) then return 'pi' end
  for _, program in ipairs {
    'nvim', 'vim', 'git', 'gh', 'yazi', 'aerc', 'weechat', 'cmus',
    'wthrr', 'btm', 'top', 'nu', 'bash',
  } do
    if lower:find( program, 1, true ) then return program end
  end
  return nil
end

local detect_program = function ( pane )
  local vars = safe_user_vars( pane )
  local from_command = normalize_program( first_command( vars.WEZTERM_PROG ) )
  if from_command then return from_command end
  return detect_program_from_title( safe_title( pane ) )
end

local program_icon = function ( program )
  local icon_name = icon_names[program]
  if icon_name and wez.nerdfonts[icon_name] then
    return wez.nerdfonts[icon_name]
  end
  return '๏’‰'
end

local pulse_color = function ()
  return palette.ui.important_local
end

local busy_badge = function ()
  -- Braille spinner glyphs sit low in JetBrains Mono/Nerd Font.
  -- Circle glyphs are visually centered in the tab/status row.
  local frames = { 'โ—‹', 'โ—Œ', 'โ—Ž', 'โ—‰', 'โ—', 'โ—‰', 'โ—Ž', 'โ—Œ' }
  local index = (math.floor( animation_tick / 2 ) % #frames) + 1
  return frames[index], pulse_color()
end

local colored_badge = function ( elements, badge, color, fallback )
  if not badge then return end
  table.insert( elements, { Text = ' ' } )
  table.insert( elements, { Foreground = { Color = color or fallback } } )
  table.insert( elements, { Text = badge } )
  table.insert( elements, { Foreground = { Color = fallback } } )
end

local progress_badge = function ( progress )
  if not progress or progress == 'None' then return nil end
  if progress == 'Indeterminate' then return busy_badge() end
  if type( progress ) == 'table' and progress.Error ~= nil then return 'โœ—', palette.error end
  if type( progress ) == 'table' and progress.Percentage ~= nil then return busy_badge() end
  return busy_badge()
end

local pi_state_badge = function ( state )
  if state == 'busy' or state == 'running' or state == 'thinking' then return busy_badge() end
  if state == 'done' or state == 'success' then return 'โœ“', palette.ui.important_local end
  if state == 'error' or state == 'failed' then return 'โœ—', palette.error end
  if state == 'aborted' then return 'ร—', palette.warning end
  if state == 'waiting' or state == 'input' or state == 'needs_input' then return '?', palette.warning end
  return nil
end

local pane_progress = function ( pane )
  local ok, progress = pcall( pane.get_progress, pane )
  if ok then return progress end
  return nil
end

local short_workspace = function ( name )
  local home = wez.home_dir
  if home and name:sub( 1, #home ) == home then
    name = '~' .. name:sub( #home + 1 )
  end

  -- Workspaces created from zoxide entries are often full paths.
  -- In the status bar, basename carries enough identity and avoids crowding tabs.
  if name:find( '/', 1, true ) or name:find( '\\', 1, true ) then
    return basename( name ) or name
  end

  if #name > 16 then
    return wez.truncate_right( name, 16 )
  end
  return name
end

local workspace_summaries = function ()
  local ok, mux_windows = pcall( wez.mux.all_windows )
  if not ok then return {} end

  local summaries = {}
  for _, mux_window in ipairs( mux_windows ) do
    local ok_workspace, workspace = pcall( function () return mux_window:get_workspace() end )
    if ok_workspace and workspace then
      local summary = summaries[workspace]
      if not summary then
        summary = { name = workspace, windows = 0, tabs = 0, panes = 0, badge = nil, badge_color = nil }
        summaries[workspace] = summary
      end
      summary.windows = summary.windows + 1

      local ok_tabs, tabs = pcall( function () return mux_window:tabs() end )
      if ok_tabs then
        for _, tab in ipairs( tabs ) do
          summary.tabs = summary.tabs + 1
          local ok_panes, panes = pcall( function () return tab:panes_with_info() end )
          if ok_panes then
            for _, info in ipairs( panes ) do
              summary.panes = summary.panes + 1
              local vars = safe_user_vars( info.pane )
              local badge, color = pi_state_badge( vars.PI_STATE )
              if not badge then badge, color = progress_badge( pane_progress( info.pane ) ) end
              if badge and (not summary.badge or badge == 'โœ—' or badge == '?') then
                summary.badge = badge
                summary.badge_color = color
              end
            end
          end
        end
      end
    end
  end

  return summaries
end

local workspace_hud = function ( active_workspace )
  local summaries = workspace_summaries()
  local names = {}
  for name in pairs( summaries ) do table.insert( names, name ) end
  table.sort( names )

  local ordered = {}
  if summaries[active_workspace] then table.insert( ordered, active_workspace ) end
  for _, name in ipairs( names ) do
    if name ~= active_workspace then table.insert( ordered, name ) end
  end

  local elements = {}
  local shown = 0
  for _, name in ipairs( ordered ) do
    shown = shown + 1
    if shown > 5 then break end

    local active = name == active_workspace
    local fg = active and palette.ui.important_local or palette.ui.minor
    local summary = summaries[name]
    table.insert( elements, { Foreground = { Color = fg } } )
    if active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end
    table.insert( elements, { Text = (active and '  โ— ' or '  โ—‹ ') .. short_workspace( summary.name ) } )
    if not active then
      colored_badge( elements, summary.badge, summary.badge_color, fg )
    end
    if active and summary.tabs > 1 then
      table.insert( elements, { Text = ' ' .. tostring( summary.tabs ) .. 't' } )
    end
    table.insert( elements, { Text = ' ' } )
    table.insert( elements, { Attribute = { Intensity = 'Normal' } } )
  end

  if #ordered > 5 then
    table.insert( elements, { Foreground = { Color = palette.ui.minor } } )
    table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) .. ' ' } )
  end

  table.insert( elements, { Text = '  ' } )

  return elements
end

local tab_title = function ( tab )
  if tab.tab_title and #tab.tab_title > 0 then
    local explicit_program = detect_program_from_title( tab.tab_title )
    return tab.tab_title, program_icon( explicit_program ), explicit_program
  end
  local vars = tab.active_pane.user_vars or {}
  local program = normalize_program( first_command( vars.WEZTERM_PROG ) )
    or detect_program_from_title( tab.active_pane.title )
    or normalize_program( tab.active_pane.foreground_process_name )
    or 'nu'
  return program, program_icon( program ), program
end

local noisy_unseen_programs = {
  pi = true,
  nvim = true,
  vim = true,
  yazi = true,
  aerc = true,
  weechat = true,
  cmus = true,
}

local mux_tab_label = function ( mux_tab )
  local explicit_title = nil
  local ok_title, title = pcall( function () return mux_tab:get_title() end )
  if ok_title and title and title ~= '' then explicit_title = title end

  local ok_pane, active_pane = pcall( function () return mux_tab:active_pane() end )
  local program = nil
  local state_badge = nil
  local state_badge_color = nil
  local has_unseen_output = false

  if ok_pane and active_pane then
    program = detect_program( active_pane )
    local vars = safe_user_vars( active_pane )
    local progress = pane_progress( active_pane )
    local pi_badge, pi_color = pi_state_badge( vars.PI_STATE )
    local progress_state_badge, progress_color = progress_badge( progress )
    state_badge = pi_badge or progress_state_badge
    state_badge_color = pi_color or progress_color
  end

  if not noisy_unseen_programs[program] then
    local ok_panes, panes = pcall( function () return mux_tab:panes_with_info() end )
    if ok_panes then
      for _, info in ipairs( panes ) do
        local ok_unseen, unseen = pcall( function () return info.pane:has_unseen_output() end )
        if ok_unseen and unseen then
          has_unseen_output = true
          break
        end
      end
    end
  end

  local label = explicit_title or program or 'nu'
  return wez.truncate_right( label, 12 ), program_icon( program or detect_program_from_title( label ) ), state_badge, state_badge_color, has_unseen_output
end

local text_width = function ( text )
  return wez.column_width( text )
end

local render_fake_tab = function ( elements, tab, compact )
  local bg = tab.active and palette.ui.focus or palette.ui.backdrop
  local fg = tab.active and palette.ui.backdrop or palette.ui.minor

  if tab.has_unseen_output and not tab.active then
    bg = palette.warning
    fg = palette.content.backdrop
  end

  local suffix = tab.has_unseen_output and ' !' or ''
  local text
  if compact and not tab.active then
    text = string.format( ' %d %s%s', tab.index + 1, tab.icon, suffix )
  else
    text = string.format( ' %d %s %s%s', tab.index + 1, tab.icon, tab.label, suffix )
  end

  table.insert( elements, { Background = { Color = bg } } )
  table.insert( elements, { Foreground = { Color = fg } } )
  if tab.active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end
  table.insert( elements, { Text = text } )
  colored_badge( elements, tab.state_badge, tab.state_badge_color, fg )
  table.insert( elements, { Text = ' ' } )
  table.insert( elements, { Attribute = { Intensity = 'Normal' } } )
  table.insert( elements, { Background = { Color = palette.content.backdrop } } )
  table.insert( elements, { Text = ' ' } )

  return text_width( text ) + (tab.state_badge and (text_width( tab.state_badge ) + 1) or 0) + 2
end

local fake_tabs = function ( window, max_width )
  local ok_window, mux_window = pcall( function () return window:mux_window() end )
  if not ok_window or not mux_window then return {} end

  local ok_tabs, tabs = pcall( function () return mux_window:tabs_with_info() end )
  if not ok_tabs then return {} end

  local tab_models = {}
  local full_width = 0
  for _, info in ipairs( tabs ) do
    local label, icon, state_badge, state_badge_color, has_unseen_output = mux_tab_label( info.tab )
    local model = {
      index = info.index,
      active = info.is_active,
      label = label,
      icon = icon,
      state_badge = state_badge,
      state_badge_color = state_badge_color,
      has_unseen_output = has_unseen_output,
    }
    local suffix = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '')
    full_width = full_width + text_width( string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, suffix ) ) + 1
    table.insert( tab_models, model )
  end

  local compact = full_width > max_width
  local active_index = 1
  for i, model in ipairs( tab_models ) do
    if model.active then
      active_index = i
      break
    end
  end

  local selected = { [active_index] = true }
  local suffix = tab_models[active_index].state_badge and (' ' .. tab_models[active_index].state_badge)
    or (tab_models[active_index].has_unseen_output and ' !' or '')
  local active_text = string.format( ' %d %s %s%s ', tab_models[active_index].index + 1, tab_models[active_index].icon, tab_models[active_index].label, suffix )
  local used = text_width( active_text ) + 1
  local overflow_reserve = 4
  local budget = math.max( used, max_width - overflow_reserve )

  local left = active_index - 1
  local right = active_index + 1
  while left >= 1 or right <= #tab_models do
    local progressed = false

    for _, i in ipairs { right, left } do
      local model = tab_models[i]
      if model then
        local s = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '')
        local text = compact
            and string.format( ' %d %s%s ', model.index + 1, model.icon, s )
          or string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, s )
        local width = text_width( text ) + 1
        if used + width <= budget then
          selected[i] = true
          used = used + width
          progressed = true
        end
      end
    end

    right = right + 1
    left = left - 1
    if not progressed and used >= budget then break end
  end

  local rendered = {}
  local hidden = 0
  for i, model in ipairs( tab_models ) do
    if selected[i] then
      table.insert( rendered, model )
    else
      hidden = hidden + 1
    end
  end

  local elements = {}
  if hidden > 0 then
    table.insert( elements, { Foreground = { Color = palette.ui.minor } } )
    table.insert( elements, { Text = '+' .. tostring( hidden ) .. ' ' } )
  end

  for _, model in ipairs( rendered ) do
    render_fake_tab( elements, model, compact )
  end

  return elements
end

wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width )
  local title, icon, program = tab_title( tab )
  local pi_badge, pi_badge_color = pi_state_badge( (tab.active_pane.user_vars or {}).PI_STATE )
  local active_progress_badge, active_progress_color = progress_badge( tab.active_pane.progress )
  local state_badge = pi_badge or active_progress_badge
  local state_badge_color = pi_badge_color or active_progress_color
  local has_unseen_output = false
  if not noisy_unseen_programs[program] then
    for _, pane_info in ipairs( tab.panes or {} ) do
      if pane_info.has_unseen_output then
        has_unseen_output = true
        break
      end
    end
  end

  local bg = palette.ui.backdrop
  local fg = palette.ui.normal
  if tab.is_active then
    bg = palette.ui.focus
    fg = palette.ui.backdrop
  elseif has_unseen_output then
    bg = palette.warning
    fg = palette.content.backdrop
  elseif hover then
    bg = palette.ui.accent
    fg = palette.ui.backdrop
  elseif tab.is_last_active then
    fg = palette.ui.important_local
  else
    fg = palette.ui.minor
  end

  local suffix = has_unseen_output and ' !' or ''
  local text = string.format( '  %d  %s  %s%s', tab.tab_index + 1, icon, title, suffix )
  text = wez.truncate_right( text, math.max( 1, max_width ) )

  local elements = {
    { Background = { Color = bg } },
    { Foreground = { Color = fg } },
    { Text = text },
  }
  colored_badge( elements, state_badge, state_badge_color, fg )
  table.insert( elements, { Text = '  ' } )
  return elements
end )

wez.on( 'format-window-title', function ( tab, pane, tabs, panes, config )
  local ok, workspace = pcall( wez.mux.get_active_workspace )
  workspace = ok and workspace or 'default'
  local title = pane and pane.title or tab.active_pane.title
  return '[' .. workspace .. '] ' .. title
end )

-- Show which key table is active in the status area
wez.on( 'update-status', function ( window, pane )
  animation_tick = animation_tick + 1
  local key_table = window:active_key_table()
  local current_workspace = window:active_workspace()
  local zoomed_state_ok, zoomed_state = pcall( zoomed, pane )
  zoomed_state = zoomed_state_ok and zoomed_state or nil
  local leader = nil
  if window:leader_is_active() then
    leader = '๓ฑ '
  end
  local pane_domain_ok, pane_domain = pcall( pane.get_domain_name, pane )
  if not pane_domain_ok then return end

  local status = {}

  local prog = detect_program( pane )
  if prog and prog ~= 'nu' and prog ~= 'pi' then
    table.insert( status, program_icon( prog ) .. ' ' .. prog )
  end

  if zoomed_state then
    table.insert( status, zoomed_state )
  end

  if pane_domain and pane_domain ~= 'local' and pane_domain ~= 'Local' then
    table.insert( status, '@' .. pane_domain )
  end

  local status_parts = {}
  for _, value in ipairs( status ) do
    if value and value ~= '' then table.insert( status_parts, value ) end
  end

  local right_elements = {}
  local status_text = table.concat( status_parts, DELIMITER )
  if status_text ~= '' then
    table.insert( right_elements, { Foreground = { Color = palette.ui.minor } } )
    table.insert( right_elements, { Text = status_text .. '  ' } )
  end
  local pane_dims = pane:get_dimensions()
  local tab_budget = math.max( 24, math.floor( pane_dims.cols * 0.45 ) )
  if status_text ~= '' then tab_budget = tab_budget - text_width( status_text ) - 2 end
  for _, element in ipairs( fake_tabs( window, tab_budget ) ) do
    table.insert( right_elements, element )
  end

  window:set_right_status( wez.format( right_elements ) )
  local battery_icons = bat.get_battery_icons()
  if battery_icons ~= '' then
    battery_icons = battery_icons .. ' '
  end

  local left_elements = {}
  if key_table or leader then
    local mode_text = key_table or 'LEADER'
    left_elements = {
      { Background = { Color = palette.error } },
      { Foreground = { Color = palette.ui.backdrop } },
      { Attribute = { Intensity = 'Bold' } },
      { Text = ' ๓ฑ  ' .. mode_text .. '  ' },
      'ResetAttributes',
      { Text = '  ' },
    }
  else
    left_elements = {
      { Text = battery_icons },
    }
    for _, element in ipairs( workspace_hud( current_workspace ) ) do
      table.insert( left_elements, element )
    end
  end

  window:set_left_status( wez.format( left_elements ) )
end )

return function ( cfg )
  cfg.enable_tab_bar = true
  -- if this is hidden, we cannot see the right status are
  cfg.hide_tab_bar_if_only_one_tab = false
  cfg.use_fancy_tab_bar = false
  cfg.tab_max_width = 32
  cfg.show_tabs_in_tab_bar = false
  cfg.show_new_tab_button_in_tab_bar = false
  cfg.tab_bar_at_bottom = true
  cfg.status_update_interval = 100
end