Skip to content

Commit

Permalink
feat: improve statusbar flexibility (#19)
Browse files Browse the repository at this point in the history
## Proposed changes

<!-- Describe the big picture of your changes here. If it fixes a bug or
resolves a feature request, be sure to link to that issue. -->

## Types of changes

What types of changes does your code introduce to this config?
_Put an `x` in the boxes that apply_

- [x] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

_Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to
ask._

- [x] I have read the
[CONTRIBUTING](https://github.com/sravioli/wezterm/blob/main/.github/contributing.md)
doc
- [x] I have added the necessary documentation (if appropriate)

## Further comments

If this is a relatively large or complex change, kick off the discussion
by explaining why you chose the solution you did and what alternatives
you considered, etc...
  • Loading branch information
sravioli authored Nov 22, 2024
2 parents 36ec0de + 2beb355 commit b382bb5
Show file tree
Hide file tree
Showing 10 changed files with 770 additions and 370 deletions.
2 changes: 1 addition & 1 deletion config/gpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ else
Config.webgpu_power_preference = "HighPerformance"
end

Config.webgpu_preferred_adapter = require("utils.gpu_adapter"):pick_best()
Config.webgpu_preferred_adapter = require("utils.gpu"):pick_best()

return Config
6 changes: 3 additions & 3 deletions config/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
return require("utils.fn").tbl_merge(
(require "config.gpu"),
return require("utils.fn").tbl.merge(
(require "config.appearance"),
(require "config.font"),
(require "config.tab-bar"),
(require "config.general")
(require "config.general"),
(require "config.gpu")
)
49 changes: 31 additions & 18 deletions events/update-status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
---@license GNU-GPLv3

-- selene: allow(incorrect_standard_library_use)
local tunpack = table.unpack or unpack
local tunpack, tinsert = table.unpack or unpack, table.insert
local tostring, tonumber = tostring, tonumber
local floor = math.floor

Expand All @@ -14,7 +14,7 @@ local timefmt, color_parse = wt.strftime, wt.color.parse

local class, fn = require "utils.class", require "utils.fn"
local icon, sep, sb = class.icon, class.icon.Sep, class.layout:new "StatusBar"
local fs, mt, str = fn.fs, fn.mt, fn.str
local fs, mt, str, tbl = fn.fs, fn.mt, fn.str, fn.tbl

---Update status event
---@param window wt.Window Wezterm's window object
Expand Down Expand Up @@ -176,29 +176,42 @@ wt.on("update-status", function(window, pane)
local fancy_bg = Config.window_frame.active_titlebar_bg
local last_fg = Config.use_fancy_tab_bar and fancy_bg or theme.tab_bar.background

local cells_of_cells = { cwd_cells, hostname_cells, time_cells, battery.cells }
for i = 1, #cells_of_cells do
local cells = cells_of_cells[i]
local cell_bg, cell_fg = palette[i], i == 1 and last_fg or palette[i - 1]
local rsep = sep.sb.right
local sets = { cwd_cells, hostname_cells, time_cells, battery.cells }

rsb:append(cell_fg, cell_bg, rsep)
local function compute_width(combination, sep_width, pad_width)
local total_width = 0
for i = 1, #combination do
total_width = total_width + str.width(combination[i]) + sep_width + pad_width
end
return total_width
end

local function find_best_fit(combinations, max_width, sep_width, pad_width)
local best_fit = nil
local best_fit_width = 0

local cell, is_cell_used = cells[1], false
width.cell = 0
for j = 1, #cells do
width.cell = str.width(cell)
if width.usable > width.cell then
cell, is_cell_used = cells[j], true
break
for i = 1, #combinations do
local total_width = compute_width(combinations[i], sep_width, pad_width)
if total_width <= max_width and total_width > best_fit_width then
best_fit = combinations[i]
best_fit_width = total_width
end
end

cell = not is_cell_used and "" or str.pad(cell)
return best_fit or { "", "", "", "" }
end

rsb:append(palette[i], theme.tab_bar.background, cell, { "Bold" })
local cells = tbl.reverse(
find_best_fit(tbl.cartesian(sets), width.usable, str.width(sep.sb.right), 5)
)

width.usable = width.usable - width.cell - str.width(rsep) - 5
-- Render the best fit, ensuring correct colors
for i = 1, #cells do
local cell_bg, cell_fg = palette[i], i == 1 and last_fg or palette[i - 1]
local rsep = sep.sb.right

rsb:append(cell_fg, cell_bg, rsep)
rsb:append(cell_bg, theme.tab_bar.background, str.pad(cells[i]), { "Bold" })
end
--~ }}}

Expand Down
2 changes: 1 addition & 1 deletion mappings/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
return require("utils.fn").tbl_merge(
return require("utils.fn").tbl.merge(
(require "mappings.default"),
(require "mappings.modes")[1]
)
19 changes: 13 additions & 6 deletions utils/class/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
---@author sravioli
---@license GNU-GPLv3

local log = require("utils.class.logger"):new "Config"
---locals are faster
-- local type, pcall, pairs, setmetatable = type, pcall, pairs, setmetatable

local wt = require "wezterm"

---@class Utils.Class.Config
local M = {}

---@package
---
---Class logger
M.log = require("utils.class.logger"):new "Config"

---Initializes a new Config object.
---Creates a new Wezterm configuration object. If `wezterm.config_builder()` is available,
---it will use the config builder and set the configuration to strict mode.
Expand All @@ -19,9 +26,9 @@ function M:new()
if wt.config_builder then ---@diagnostic disable-line: undefined-field
self.config = wt.config_builder() ---@diagnostic disable-line: undefined-field
self.config:set_strict_mode(true)
log:debug "Wezterm's config builder is available"
M.log:debug "Wezterm's config builder is available"
else
log:warn "Wezterm's config builder is unavailable"
M.log:warn "Wezterm's config builder is unavailable"
end

self = setmetatable(self.config, { __index = M })
Expand All @@ -45,20 +52,20 @@ end
function M:add(spec)
if type(spec) == "string" then
if not (pcall(require, spec)) then
log:error("Unable to require module %s", spec)
M.log:error("Unable to require module %s", spec)
return self
end
spec = require(spec)
end

for key, value in pairs(spec) do
if self.config[key] == spec[key] then
log:warn("found duplicate! old: %s, new: %s", self.config[key], spec[key])
M.log:warn("found duplicate! old: %s, new: %s", self.config[key], spec[key])
end
self.config[key] = value
end

return self
return self.config
end

return M
4 changes: 2 additions & 2 deletions utils/class/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---@class Utils.Class
---@field config Utils.Class.Config
---@field layout Utils.Class.Layout
---@field icon Utils.Class.Icons
---@field picker Utils.Class.Picker
---@field layout Utils.Class.Layout
---@field logger Utils.Class.Logger
---@field picker Utils.Class.Picker
local M = {}

local mod = ...
Expand Down
2 changes: 1 addition & 1 deletion utils/class/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end
---@param message string log message or format string
---@param ... any additional arguments to format into the message
function M:log(level, message, ...)
if not self.enabled then
if not (G.enable_logging and self.enabled) then
return
end

Expand Down
Loading

0 comments on commit b382bb5

Please sign in to comment.