/ wezterm / events / right-status.lua
right-status.lua
  1  local umath = require('extensions.math')
  2  local wezterm = require('wezterm')
  3  
  4  local nf = wezterm.nerdfonts
  5  local M = {}
  6  
  7  local SEPARATOR_CHAR = nf.oct_dash .. ' '
  8  
  9  local discharging_icons = {
 10    nf.md_battery_10,
 11    nf.md_battery_20,
 12    nf.md_battery_30,
 13    nf.md_battery_40,
 14    nf.md_battery_50,
 15    nf.md_battery_60,
 16    nf.md_battery_70,
 17    nf.md_battery_80,
 18    nf.md_battery_90,
 19    nf.md_battery,
 20  }
 21  local charging_icons = {
 22    nf.md_battery_charging_10,
 23    nf.md_battery_charging_20,
 24    nf.md_battery_charging_30,
 25    nf.md_battery_charging_40,
 26    nf.md_battery_charging_50,
 27    nf.md_battery_charging_60,
 28    nf.md_battery_charging_70,
 29    nf.md_battery_charging_80,
 30    nf.md_battery_charging_90,
 31    nf.md_battery_charging,
 32  }
 33  
 34  local colors = {
 35    date_fg = '#fab387',
 36    date_bg = 'rgba(0, 0, 0, 0.4)',
 37    battery_fg = '#f9e2af',
 38    battery_bg = 'rgba(0, 0, 0, 0.4)',
 39    separator_fg = '#74c7ec',
 40    separator_bg = 'rgba(0, 0, 0, 0.4)',
 41  }
 42  
 43  local __cells__ = {} -- wezterm FormatItems (ref: https://wezfurlong.org/wezterm/config/lua/wezterm/format.html)
 44  
 45  ---@param text string
 46  ---@param icon string
 47  ---@param fg string
 48  ---@param bg string
 49  ---@param separate boolean
 50  local _push = function(text, icon, fg, bg, separate)
 51    table.insert(__cells__, { Foreground = { Color = fg } })
 52    table.insert(__cells__, { Background = { Color = bg } })
 53    table.insert(__cells__, { Attribute = { Intensity = 'Bold' } })
 54    table.insert(__cells__, { Text = icon .. ' ' .. text .. ' ' })
 55  
 56    if separate then
 57      table.insert(__cells__, { Foreground = { Color = colors.separator_fg } })
 58      table.insert(__cells__, { Background = { Color = colors.separator_bg } })
 59      table.insert(__cells__, { Text = SEPARATOR_CHAR })
 60    end
 61  end
 62  
 63  local _set_workspace = function(active_workspace_name)
 64    _push(active_workspace_name, '📡', colors.date_fg, colors.date_bg, false)
 65  end
 66  
 67  local _set_date = function()
 68    local date = wezterm.strftime('%a %D %H:%M')
 69    _push(date, '📆', colors.date_fg, colors.date_bg, true)
 70  end
 71  
 72  local _set_battery = function()
 73    -- ref: https://wezfurlong.org/wezterm/config/lua/wezterm/battery_info.html
 74  
 75    local charge = ''
 76    local icon = ''
 77  
 78    for _, b in ipairs(wezterm.battery_info()) do
 79      local idx = umath.clamp(umath.round(b.state_of_charge * 10), 1, 10)
 80      charge = string.format('%.0f%%', b.state_of_charge * 100)
 81  
 82      if b.state == 'Charging' then
 83        icon = charging_icons[idx]
 84      else
 85        icon = discharging_icons[idx]
 86      end
 87    end
 88  
 89    _push(charge, icon, colors.battery_fg, colors.battery_bg, false)
 90  end
 91  
 92  M.setup = function()
 93    wezterm.on('update-right-status', function(window)
 94      __cells__ = {}
 95      _set_workspace(string.format('wokspace: %s %s', wezterm.mux.get_active_workspace(), SEPARATOR_CHAR))
 96      _set_date()
 97      _set_battery()
 98  
 99      window:set_right_status(wezterm.format(__cells__))
100    end)
101  end
102  
103  return M