From 83e6afe0aea01e4cfe0e76b09ff93e178aed123b Mon Sep 17 00:00:00 2001 From: winston Date: Sun, 5 Mar 2023 05:40:35 +0100 Subject: [PATCH] feat(wezterm): plugin & config_builder --- home/apps/wezterm/bar.lua | 124 ++++++++++++++++++++-------------- home/apps/wezterm/fonts.lua | 6 ++ home/apps/wezterm/keys.lua | 15 ++-- home/apps/wezterm/theme.lua | 8 +++ home/apps/wezterm/wezterm.lua | 108 ++++++++--------------------- 5 files changed, 125 insertions(+), 136 deletions(-) diff --git a/home/apps/wezterm/bar.lua b/home/apps/wezterm/bar.lua index 3a05938..577fb45 100644 --- a/home/apps/wezterm/bar.lua +++ b/home/apps/wezterm/bar.lua @@ -2,7 +2,10 @@ local wezterm = require("wezterm") local M = {} -M.config = { +-- default configuration +local config = { + position = "bottom", + max_width = 32, dividers = "slant_right", indicator = { leader = { @@ -33,6 +36,9 @@ M.config = { }, } +-- parsed config +local C = {} + local function tableMerge(t1, t2) for k, v in pairs(t2) do if type(v) == "table" then @@ -48,66 +54,77 @@ local function tableMerge(t1, t2) return t1 end -local C = {} +local dividers = { + slant_right = { + left = utf8.char(0xe0be), + right = utf8.char(0xe0bc), + }, + slant_left = { + left = utf8.char(0xe0ba), + right = utf8.char(0xe0b8), + }, + arrows = { + left = utf8.char(0xe0b2), + right = utf8.char(0xe0b0), + }, + rounded = { + left = utf8.char(0xe0b6), + right = utf8.char(0xe0b4), + }, +} -M.setup = function(config) - M.config = tableMerge(M.config, config) - local dividers = { - slant_right = { - left = utf8.char(0xe0be), - right = utf8.char(0xe0bc), - }, - slant_left = { - left = utf8.char(0xe0ba), - right = utf8.char(0xe0b8), - }, - arrows = { - left = utf8.char(0xe0b2), - right = utf8.char(0xe0b0), - }, - rounded = { - left = utf8.char(0xe0b6), - right = utf8.char(0xe0b4), - }, - } +-- conforming to https://github.com/wez/wezterm/commit/e4ae8a844d8feaa43e1de34c5cc8b4f07ce525dd +-- exporting an apply_to_config function, even though we don't change the users config +M.apply_to_config = function(c, opts) + -- make the opts arg optional + if not opts then + opts = {} + end + -- combine user config with defaults + config = tableMerge(config, opts) C.div = { l = "", r = "", } - if M.config.dividers then - C.div.l = dividers[M.config.dividers].left - C.div.r = dividers[M.config.dividers].right + + if config.dividers then + C.div.l = dividers[config.dividers].left + C.div.r = dividers[config.dividers].right end C.leader = { - enabled = M.config.indicator.leader.enabled or true, - off = M.config.indicator.leader.off, - on = M.config.indicator.leader.on, + enabled = config.indicator.leader.enabled or true, + off = config.indicator.leader.off, + on = config.indicator.leader.on, } C.mode = { - enabled = M.config.indicator.mode.enabled, - names = M.config.indicator.mode.names, + enabled = config.indicator.mode.enabled, + names = config.indicator.mode.names, } C.tabs = { - numerals = M.config.tabs.numerals, - pane_count_style = M.config.tabs.pane_count, + numerals = config.tabs.numerals, + pane_count_style = config.tabs.pane_count, brackets = { - active = M.config.tabs.brackets.active, - inactive = M.config.tabs.brackets.inactive, + active = config.tabs.brackets.active, + inactive = config.tabs.brackets.inactive, }, } C.clock = { - enabled = M.config.clock.enabled, - format = M.config.clock.format, + enabled = config.clock.enabled, + format = config.clock.format, } - C.p = (M.config.dividers == "rounded") and "" or " " + -- set the right-hand padding to 0 spaces, if the rounded style is active + C.p = (config.dividers == "rounded") and "" or " " - wezterm.log_info(C) + -- set wezterm config options according to the parsed config + c.use_fancy_tab_bar = false + c.tab_bar_at_bottom = config.position == "bottom" + c.tab_max_width = config.max_width end -- superscript/subscript @@ -171,8 +188,8 @@ local roman_numerals = { -- custom tab bar wezterm.on( "format-tab-title", - function(tab, tabs, panes, config, hover, max_width) - local colours = config.resolved_palette.tab_bar + function(tab, tabs, _panes, conf, _hover, _max_width) + local colours = conf.resolved_palette.tab_bar local active_tab_index = 0 for _, t in ipairs(tabs) do @@ -181,13 +198,14 @@ wezterm.on( end end + -- TODO: make colors configurable local rainbow = { - config.resolved_palette.ansi[2], - config.resolved_palette.indexed[16], - config.resolved_palette.ansi[4], - config.resolved_palette.ansi[3], - config.resolved_palette.ansi[5], - config.resolved_palette.ansi[6], + conf.resolved_palette.ansi[2], + conf.resolved_palette.indexed[16], + conf.resolved_palette.ansi[4], + conf.resolved_palette.ansi[3], + conf.resolved_palette.ansi[5], + conf.resolved_palette.ansi[6], } local i = tab.tab_index % 6 @@ -244,6 +262,7 @@ wezterm.on( index_i = tab.tab_index + 1 end + local index if tab.is_active then index = string.format( "%s%s%s ", @@ -264,8 +283,8 @@ wezterm.on( local fillerwidth = 2 + string.len(index) + string.len(pane_count) + 2 local tabtitle = tab.active_pane.title - local width = config.tab_max_width - fillerwidth - 1 - if (#tabtitle + fillerwidth) > config.tab_max_width then + local width = conf.tab_max_width - fillerwidth - 1 + if (#tabtitle + fillerwidth) > conf.tab_max_width then tabtitle = wezterm.truncate_right(tabtitle, width) .. "…" end @@ -282,8 +301,7 @@ wezterm.on( end ) --- custom status -wezterm.on("update-status", function(window, pane) +wezterm.on("update-status", function(window, _pane) local active_kt = window:active_key_table() ~= nil local show = C.leader.enabled or (active_kt and C.mode.enabled) if not show then @@ -291,7 +309,11 @@ wezterm.on("update-status", function(window, pane) return end - local palette = window:effective_config().resolved_palette + local present, conf = pcall(window.effective_config, window) + if not present then + return + end + local palette = conf.resolved_palette local leader = "" if C.leader.enabled then diff --git a/home/apps/wezterm/fonts.lua b/home/apps/wezterm/fonts.lua index ffb5388..9dd3a2c 100644 --- a/home/apps/wezterm/fonts.lua +++ b/home/apps/wezterm/fonts.lua @@ -46,4 +46,10 @@ wezterm.on("switch-font", function(window, _) }) end) +wezterm.GLOBAL = { font = "berkeley" } +M.apply = function(c) + c.font = M.get_font(wezterm.GLOBAL.font).font + c.font_size = M.get_font(wezterm.GLOBAL.font).size +end + return M diff --git a/home/apps/wezterm/keys.lua b/home/apps/wezterm/keys.lua index dfbbbf6..f9851b0 100644 --- a/home/apps/wezterm/keys.lua +++ b/home/apps/wezterm/keys.lua @@ -13,6 +13,7 @@ local map = function(key, mods, action) end end +wezterm.GLOBAL.enable_tab_bar = true local toggleTabBar = wezterm.action_callback(function(window) wezterm.GLOBAL.enable_tab_bar = not wezterm.GLOBAL.enable_tab_bar window:set_config_overrides({ @@ -113,9 +114,11 @@ for k, _ in pairs(key_tables) do ) end -return { - leader = { key = "s", mods = "CTRL", timeout_milliseconds = 5000 }, - keys = shortcuts, - disable_default_key_bindings = true, - key_tables = key_tables, -} +local M = {} +M.apply = function(c) + c.leader = { key = "s", mods = "CTRL", timeout_milliseconds = 5000 } + c.keys = shortcuts + c.disable_default_key_bindings = true + c.key_tables = key_tables +end +return M diff --git a/home/apps/wezterm/theme.lua b/home/apps/wezterm/theme.lua index 474f760..b6c1d06 100644 --- a/home/apps/wezterm/theme.lua +++ b/home/apps/wezterm/theme.lua @@ -25,4 +25,12 @@ M.get_custom_colorschemes = function() } end +M.apply = function(c) + c.color_schemes = M.get_custom_colorschemes() + c.color_scheme = M.scheme_for_appearance(wezterm.gui.get_appearance(), { + dark = "Catppuccin Americano", + light = "Catppuccin Latte", + }) +end + return M diff --git a/home/apps/wezterm/wezterm.lua b/home/apps/wezterm/wezterm.lua index 7257611..d2fd606 100644 --- a/home/apps/wezterm/wezterm.lua +++ b/home/apps/wezterm/wezterm.lua @@ -1,85 +1,35 @@ local wezterm = require("wezterm") -local theme = require("theme") -require("bar").setup({ - dividers = "slant_right", -- or "slant_left", "arrows", "rounded", false - indicator = { - leader = { - enabled = true, - off = " ", - on = " ", - }, - mode = { - enabled = true, - names = { - resize_mode = "RESIZE", - copy_mode = "VISUAL", - search_mode = "SEARCH", - }, - }, - }, - tabs = { - numerals = "arabic", -- or "roman" - pane_count = "superscript", -- or "subscript", false - brackets = { - active = { "", ":" }, - inactive = { "", ":" }, - }, - }, - clock = { -- note that this overrides the whole set_right_status - enabled = true, - format = "%H:%M", -- use https://wezfurlong.org/wezterm/config/lua/wezterm.time/Time/format.html - }, -}) -wezterm.GLOBAL = { - font = "berkeley", - enable_tab_bar = true, +local c = wezterm.config_builder() +c:set_strict_mode(true) + +require("fonts").apply(c) +require("keys").apply(c) +require("theme").apply(c) + +-- set up terminfo on nix +c.set_environment_variables = { + TERMINFO_DIRS = wezterm.home_dir .. "/.nix-profile/share/terminfo", } -local font = require("fonts").get_font(wezterm.GLOBAL.font) - -local options = { - set_environment_variables = { - TERMINFO_DIRS = wezterm.home_dir .. "/.nix-profile/share/terminfo", - }, - -- font - font = font.font, - font_size = font.size, - -- window - window_decorations = "RESIZE", - window_padding = { - left = 0, - right = 0, - top = 0, - bottom = 0, - }, - inactive_pane_hsb = { - saturation = 1.0, - brightness = 0.6, - }, - -- theme - color_schemes = theme.get_custom_colorschemes(), - color_scheme = theme.scheme_for_appearance(wezterm.gui.get_appearance(), { - dark = "Catppuccin Americano", - light = "Catppuccin Latte", - }), - -- tab bar - tab_bar_at_bottom = true, - tab_max_width = 32, - use_fancy_tab_bar = false, - window_background_opacity = 1.00, - hide_tab_bar_if_only_one_tab = false, - enable_tab_bar = wezterm.GLOBAL.tab_bar_hidden, - -- etc. - adjust_window_size_when_changing_font_size = false, - use_resize_increments = false, - audible_bell = "Disabled", - clean_exit_codes = { 130 }, - default_cursor_style = "BlinkingBar", - enable_scroll_bar = false, +-- window +c.window_decorations = "RESIZE" +c.window_padding = { + left = 0, + right = 0, + top = 0, + bottom = 0, } +-- dim unfocused panes +c.inactive_pane_hsb = { + saturation = 1.0, + brightness = 0.6, +} +-- etc. +c.adjust_window_size_when_changing_font_size = false +c.audible_bell = "Disabled" +c.clean_exit_codes = { 130 } +c.default_cursor_style = "BlinkingBar" -for k, v in pairs(require("keys")) do - options[k] = v -end +require("bar").apply_to_config(c) -return options +return c