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 ai_shell = require 'bugabinga.ai_shell'
local palette = require 'bugabinga.nugu.palette_dark'.palette
local icon_names = require 'bugabinga.icons'

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 short_workspace = function ( name )
  local home = wez.home_dir
  if home and name:sub( 1, #home ) == home then
    name = '~' .. name:sub( #home + 1 )
  end
  if #name > 28 then
    return 'โ€ฆ/' .. (basename( name ) or name)
  end
  return name
end

local classify_workspace_program = function ( program )
  if program == 'pi' then return 'pi' end
  if program == 'nvim' or program == 'vim' then return 'nvim' end
  if program == 'git' or program == 'gh' then return 'git' end
  if program == 'yazi' or program == 'ranger' or program == 'lf' then return 'files' end
  if program == 'aerc' or program == 'neomutt' then return 'mail' end
  if program == 'wthrr' then return 'weather' end
  return nil
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, programs = {} }
        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 program = detect_program( info.pane )
              local class = classify_workspace_program( program )
              if class then
                summary.programs[class] = (summary.programs[class] or 0) + 1
              end
            end
          end
        end
      end
    end
  end

  return summaries
end

local append_count = function ( parts, icon, count )
  if count and count > 0 then
    table.insert( parts, icon .. (count > 1 and tostring( count ) or '') )
  end
end

local workspace_badge = function ( summary, active )
  local parts = { short_workspace( summary.name ) }
  append_count( parts, 'ฯ€', summary.programs.pi )
  append_count( parts, '๎Ÿ…', summary.programs.nvim )
  append_count( parts, '๎œ‚', summary.programs.git )
  append_count( parts, '๏ป', summary.programs.files )
  append_count( parts, '๓ฐ‡ฎ', summary.programs.mail )
  append_count( parts, '๓ฐ–', summary.programs.weather )

  if #parts == 1 and active and summary.tabs > 1 then
    table.insert( parts, tostring( summary.tabs ) .. 't' )
  end

  return table.concat( parts, ' ' )
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
    table.insert( elements, { Foreground = { Color = active and palette.ui.important_local or palette.ui.minor } } )
    if active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end
    table.insert( elements, { Text = (active and ' โ— ' or ' โ—‹ ') .. workspace_badge( summaries[name], active ) } )
    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

  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 )
  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 )
end

wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width )
  local title, icon = tab_title( tab )
  local has_unseen_output = false
  for _, pane_info in ipairs( tab.panes or {} ) do
    if pane_info.has_unseen_output then
      has_unseen_output = true
      break
    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 ) )

  return {
    { Background = { Color = bg } },
    { Foreground = { Color = fg } },
    { Text = text },
  }
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 )
  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 time = wez.time.now():format  '%H:%M'
  local ai_status = ai_shell.get_status( window )
  local status = { time, ai_status, leader, }

  -- Special program indicators
  local prog = detect_program( pane )
  if prog == 'pi' then
    table.insert( status, wez.format {
      { Foreground = { Color = '#d151d1' } },
      { Attribute = { Intensity = 'Bold' } },
      { Text = 'pi' },
    } )
  elseif prog and prog ~= 'nu' then
    table.insert( status, program_icon( prog ) .. ' ' .. prog )
  end

  if key_table then
    table.insert( status, key_table )
  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 right_elements = {
    { Text = table.concat( status, DELIMITER ), },
  }
  if key_table then
    table.insert( right_elements, { Text = DELIMITER } )
    table.insert( right_elements, { Background = { Color = palette.error, }, } )
    table.insert( right_elements, { Foreground = { Color = palette.ui.backdrop, }, } )
    table.insert( right_elements, { Text = ' ' .. key_table .. ' ', } )
  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 = {
    { Text = ' ๎žข  ' },
    { Text = battery_icons },
  }
  for _, element in ipairs( workspace_hud( current_workspace ) ) do
    table.insert( left_elements, element )
  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.tab_bar_at_bottom = true
  cfg.status_update_interval = 1000
end