From 7d5a1665dd5fa832f794ba996632fdbaeb038078 Mon Sep 17 00:00:00 2001 From: Michael Brusegard <56915010+michaelbrusegard@users.noreply.github.com> Date: Tue, 24 Sep 2024 04:59:02 +0200 Subject: [PATCH 1/3] store active extensions in table --- README.md | 1 + plugin/init.lua | 2 +- plugin/tabline/component.lua | 69 +++++++-------- plugin/tabline/config.lua | 13 ++- plugin/tabline/extension.lua | 86 ++++++++++++------- plugin/tabline/extensions/presentation.lua | 1 + plugin/tabline/extensions/quick_domains.lua | 1 + plugin/tabline/extensions/resurrect.lua | 2 + .../extensions/smart_workspace_switcher.lua | 1 + 9 files changed, 98 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index f3de61c..bd22276 100644 --- a/README.md +++ b/README.md @@ -587,6 +587,7 @@ The `colors` overwrite the default colors for the extension to use with its `sec ```lua local my_extension = { + 'my_extension_name', events = { show = 'my_plugin.show', hide = 'my_plugin.hide', diff --git a/plugin/init.lua b/plugin/init.lua index 2f6a39c..13f539d 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -42,7 +42,7 @@ function M.setup(opts) return require('tabline.tabs').set_title(tab, hover) end) - require('tabline.extension').load() + -- require('tabline.extension').load() end function M.apply_to_config(config) diff --git a/plugin/tabline/component.lua b/plugin/tabline/component.lua index 2b5c728..0fe39f3 100644 --- a/plugin/tabline/component.lua +++ b/plugin/tabline/component.lua @@ -1,6 +1,7 @@ local wezterm = require('wezterm') local util = require('tabline.util') local config = require('tabline.config') +local extension = require('tabline.extension') local M = {} @@ -20,57 +21,58 @@ local tabline_a, tabline_b, tabline_c, tabline_x, tabline_y, tabline_z = {}, {}, local function create_attributes(window) local mode = window:active_key_table() or 'normal_mode' + local colors = config.colors[mode] attributes_a = { - { Foreground = { Color = config.colors[mode].a.fg } }, - { Background = { Color = config.colors[mode].a.bg } }, + { Foreground = { Color = colors.a.fg } }, + { Background = { Color = colors.a.bg } }, { Attribute = { Intensity = 'Bold' } }, } attributes_b = { - { Foreground = { Color = config.colors[mode].b.fg } }, - { Background = { Color = config.colors[mode].b.bg } }, + { Foreground = { Color = colors.b.fg } }, + { Background = { Color = colors.b.bg } }, { Attribute = { Intensity = 'Normal' } }, } attributes_c = { - { Foreground = { Color = config.colors[mode].c.fg } }, - { Background = { Color = config.colors[mode].c.bg } }, + { Foreground = { Color = colors.c.fg } }, + { Background = { Color = colors.c.bg } }, } attributes_x = { - { Foreground = { Color = config.colors[mode].x and config.colors[mode].x.fg or config.colors[mode].c.fg } }, - { Background = { Color = config.colors[mode].x and config.colors[mode].x.bg or config.colors[mode].c.bg } }, + { Foreground = { Color = colors.x and colors.x.fg or colors.c.fg } }, + { Background = { Color = colors.x and colors.x.bg or colors.c.bg } }, } attributes_y = { - { Foreground = { Color = config.colors[mode].y and config.colors[mode].y.fg or config.colors[mode].b.fg } }, - { Background = { Color = config.colors[mode].y and config.colors[mode].y.bg or config.colors[mode].b.bg } }, + { Foreground = { Color = colors.y and colors.y.fg or colors.b.fg } }, + { Background = { Color = colors.y and colors.y.bg or colors.b.bg } }, { Attribute = { Intensity = 'Normal' } }, } attributes_z = { - { Foreground = { Color = config.colors[mode].z and config.colors[mode].z.fg or config.colors[mode].a.fg } }, - { Background = { Color = config.colors[mode].z and config.colors[mode].z.bg or config.colors[mode].a.bg } }, + { Foreground = { Color = colors.z and colors.z.fg or colors.a.fg } }, + { Background = { Color = colors.z and colors.z.bg or colors.a.bg } }, { Attribute = { Intensity = 'Bold' } }, } section_seperator_attributes_a = { - { Foreground = { Color = config.colors[mode].a.bg } }, - { Background = { Color = config.colors[mode].b.bg } }, + { Foreground = { Color = colors.a.bg } }, + { Background = { Color = colors.b.bg } }, } section_seperator_attributes_b = { - { Foreground = { Color = config.colors[mode].b.bg } }, - { Background = { Color = config.colors[mode].c.bg } }, + { Foreground = { Color = colors.b.bg } }, + { Background = { Color = colors.c.bg } }, } section_seperator_attributes_c = { - { Foreground = { Color = config.colors[mode].a.bg } }, - { Background = { Color = config.colors[mode].c.bg } }, + { Foreground = { Color = colors.a.bg } }, + { Background = { Color = colors.c.bg } }, } section_seperator_attributes_x = { - { Foreground = { Color = config.colors[mode].z and config.colors[mode].z.bg or config.colors[mode].a.bg } }, - { Background = { Color = config.colors[mode].x and config.colors[mode].x.bg or config.colors[mode].c.bg } }, + { Foreground = { Color = colors.z and colors.z.bg or colors.a.bg } }, + { Background = { Color = colors.x and colors.x.bg or colors.c.bg } }, } section_seperator_attributes_y = { - { Foreground = { Color = config.colors[mode].y and config.colors[mode].y.bg or config.colors[mode].b.bg } }, - { Background = { Color = config.colors[mode].x and config.colors[mode].x.bg or config.colors[mode].c.bg } }, + { Foreground = { Color = colors.y and colors.y.bg or colors.b.bg } }, + { Background = { Color = colors.x and colors.x.bg or colors.c.bg } }, } section_seperator_attributes_z = { - { Foreground = { Color = config.colors[mode].z and config.colors[mode].z.bg or config.colors[mode].a.bg } }, - { Background = { Color = config.colors[mode].y and config.colors[mode].y.bg or config.colors[mode].b.bg } }, + { Foreground = { Color = colors.z and colors.z.bg or colors.a.bg } }, + { Background = { Color = colors.y and colors.y.bg or colors.b.bg } }, } end @@ -87,18 +89,13 @@ local function insert_component_separators(components, is_left) end local function create_sections(window) - tabline_a = - insert_component_separators(util.extract_components(config.sections.tabline_a, attributes_a, window), true) - tabline_b = - insert_component_separators(util.extract_components(config.sections.tabline_b, attributes_b, window), true) - tabline_c = - insert_component_separators(util.extract_components(config.sections.tabline_c, attributes_c, window), true) - tabline_x = - insert_component_separators(util.extract_components(config.sections.tabline_x, attributes_x, window), false) - tabline_y = - insert_component_separators(util.extract_components(config.sections.tabline_y, attributes_y, window), false) - tabline_z = - insert_component_separators(util.extract_components(config.sections.tabline_z, attributes_z, window), false) + local sections = config.sections + tabline_a = insert_component_separators(util.extract_components(sections.tabline_a, attributes_a, window), true) + tabline_b = insert_component_separators(util.extract_components(sections.tabline_b, attributes_b, window), true) + tabline_c = insert_component_separators(util.extract_components(sections.tabline_c, attributes_c, window), true) + tabline_x = insert_component_separators(util.extract_components(sections.tabline_x, attributes_x, window), false) + tabline_y = insert_component_separators(util.extract_components(sections.tabline_y, attributes_y, window), false) + tabline_z = insert_component_separators(util.extract_components(sections.tabline_z, attributes_z, window), false) end local function right_section() diff --git a/plugin/tabline/config.lua b/plugin/tabline/config.lua index ada7fc5..4f6497c 100644 --- a/plugin/tabline/config.lua +++ b/plugin/tabline/config.lua @@ -7,7 +7,6 @@ M.opts = {} M.sections = {} M.component_opts = {} M.colors = {} -M.normal_mode_colors = {} local default_opts = { options = { @@ -82,14 +81,12 @@ local function get_colors(theme) end end - M.normal_mode_colors = { - a = { fg = mantle, bg = blue }, - b = { fg = blue, bg = surface0 }, - c = { fg = text, bg = mantle }, - } - return { - normal_mode = util.deep_copy(M.normal_mode_colors), + normal_mode = { + a = { fg = mantle, bg = blue }, + b = { fg = blue, bg = surface0 }, + c = { fg = text, bg = mantle }, + }, copy_mode = { a = { fg = mantle, bg = yellow }, b = { fg = yellow, bg = surface0 }, diff --git a/plugin/tabline/extension.lua b/plugin/tabline/extension.lua index eac026c..5a649fb 100644 --- a/plugin/tabline/extension.lua +++ b/plugin/tabline/extension.lua @@ -1,9 +1,10 @@ local wezterm = require('wezterm') -local util = require('tabline.util') local config = require('tabline.config') local M = {} +M.extensions = {} + local function correct_window(window) if window then if window.gui_window then @@ -13,61 +14,80 @@ local function correct_window(window) return window end -local function set_attributes(sections, colors, window) - config.sections = sections - config.colors.normal_mode = colors +local function refresh_tabline(window) if window and window.window_id then require('tabline.component').set_status(window) end end -local function on_show_event(event, events, sections, colors) +local function add_extension(extension) + local exists = false + for _, ext in ipairs(M.extensions) do + if ext[1] == extension[1] then + exists = true + break + end + end + if not exists then + table.insert(M.extensions, extension) + end +end + +local function remove_extension(extension) + for i, ext in ipairs(M.extensions) do + if ext[1] == extension[1] then + table.remove(M.extensions, i) + break + end + end +end + +local function on_show_event(event, extension) wezterm.on(event, function(window, ...) window = correct_window(window) - set_attributes(sections, colors, window) - if not events.hide then - wezterm.time.call_after(events.delay or 5, function() - set_attributes(config.opts.sections, config.normal_mode_colors, window) + add_extension(extension) + refresh_tabline(window) + if not extension.events.hide then + wezterm.time.call_after(extension.events.delay or 5, function() + remove_extension(extension) + refresh_tabline(window) end) end - if events.callback then - events.callback(window, ...) + if extension.events.callback then + extension.events.callback(window, ...) end end) end -local function on_hide_event(event, events) +local function on_hide_event(event, extension) wezterm.on(event, function(window) window = correct_window(window) - if events.delay then - wezterm.time.call_after(events.delay, function() - set_attributes(config.opts.sections, config.normal_mode_colors, window) + if extension.events.delay then + wezterm.time.call_after(extension.events.delay, function() + remove_extension(extension) + refresh_tabline(window) end) else - set_attributes(config.opts.sections, config.normal_mode_colors, window) + remove_extension(extension) + refresh_tabline(window) end end) end local function setup_extension(extension) - local sections = util.deep_extend(util.deep_copy(config.opts.sections), extension.sections or {}) - local colors = util.deep_extend(util.deep_copy(config.normal_mode_colors), extension.colors or {}) - local events = extension.events - if sections and events then - if type(events.show) == 'string' then - on_show_event(events.show, events, sections, colors) - else - for _, event in ipairs(events.show) do - on_show_event(event, events, sections, colors) - end + if type(extension.events.show) == 'string' then + on_show_event(extension.events.show, extension) + else + for _, event in ipairs(extension.events.show) do + on_show_event(event, extension) end - if events.hide then - if type(events.hide) == 'string' then - on_hide_event(events.hide, events) - elseif type(events.hide) == 'table' then - for _, event in ipairs(events.hide) do - on_hide_event(event, events) - end + end + if extension.events.hide then + if type(extension.events.hide) == 'string' then + on_hide_event(extension.events.hide, extension) + elseif type(extension.events.hide) == 'table' then + for _, event in ipairs(extension.events.hide) do + on_hide_event(event, extension) end end end diff --git a/plugin/tabline/extensions/presentation.lua b/plugin/tabline/extensions/presentation.lua index 0435f72..e333ae9 100644 --- a/plugin/tabline/extensions/presentation.lua +++ b/plugin/tabline/extensions/presentation.lua @@ -3,6 +3,7 @@ local config = require('tabline.config') return { { + 'presentation', events = { show = 'xarvex.presentation.activate', hide = 'xarvex.presentation.deactivate', diff --git a/plugin/tabline/extensions/quick_domains.lua b/plugin/tabline/extensions/quick_domains.lua index 5f08b11..e6a7759 100644 --- a/plugin/tabline/extensions/quick_domains.lua +++ b/plugin/tabline/extensions/quick_domains.lua @@ -2,6 +2,7 @@ local config = require('tabline.config') return { { + 'quick_domains', events = { show = 'quick_domain.fuzzy_selector.opened', hide = { 'quick_domain.fuzzy_selector.canceled', 'quick_domain.fuzzy_selector.selected' }, diff --git a/plugin/tabline/extensions/resurrect.lua b/plugin/tabline/extensions/resurrect.lua index c2f8590..881c2ad 100644 --- a/plugin/tabline/extensions/resurrect.lua +++ b/plugin/tabline/extensions/resurrect.lua @@ -3,6 +3,7 @@ local config = require('tabline.config') return { { + 'resurrect', events = { show = 'resurrect.fuzzy_load.start', hide = 'resurrect.fuzzy_load.finished', @@ -22,6 +23,7 @@ return { }, }, { + 'resurrect.periodic_save', events = { show = 'resurrect.periodic_save', delay = 7, diff --git a/plugin/tabline/extensions/smart_workspace_switcher.lua b/plugin/tabline/extensions/smart_workspace_switcher.lua index 8048092..ba7e215 100644 --- a/plugin/tabline/extensions/smart_workspace_switcher.lua +++ b/plugin/tabline/extensions/smart_workspace_switcher.lua @@ -2,6 +2,7 @@ local config = require('tabline.config') return { { + 'smart_workspace_switcher', events = { show = 'smart_workspace_switcher.workspace_switcher.start', hide = { From cc6467fb22322a56ce6b6bdf3f852aaddc389dc0 Mon Sep 17 00:00:00 2001 From: Michael Brusegard <56915010+michaelbrusegard@users.noreply.github.com> Date: Tue, 24 Sep 2024 05:17:34 +0200 Subject: [PATCH 2/3] apply the extensions on top of each other --- plugin/init.lua | 2 +- plugin/tabline/component.lua | 10 ++++++++++ plugin/tabline/tabs.lua | 33 +++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/plugin/init.lua b/plugin/init.lua index 13f539d..2f6a39c 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -42,7 +42,7 @@ function M.setup(opts) return require('tabline.tabs').set_title(tab, hover) end) - -- require('tabline.extension').load() + require('tabline.extension').load() end function M.apply_to_config(config) diff --git a/plugin/tabline/component.lua b/plugin/tabline/component.lua index 0fe39f3..9428d5d 100644 --- a/plugin/tabline/component.lua +++ b/plugin/tabline/component.lua @@ -22,6 +22,11 @@ local tabline_a, tabline_b, tabline_c, tabline_x, tabline_y, tabline_z = {}, {}, local function create_attributes(window) local mode = window:active_key_table() or 'normal_mode' local colors = config.colors[mode] + for _, ext in pairs(extension.extensions) do + if ext.colors then + colors = util.deep_extend(util.deep_copy(colors), ext.colors) + end + end attributes_a = { { Foreground = { Color = colors.a.fg } }, { Background = { Color = colors.a.bg } }, @@ -90,6 +95,11 @@ end local function create_sections(window) local sections = config.sections + for _, ext in pairs(extension.extensions) do + if ext.sections then + sections = util.deep_extend(util.deep_copy(sections), ext.sections) + end + end tabline_a = insert_component_separators(util.extract_components(sections.tabline_a, attributes_a, window), true) tabline_b = insert_component_separators(util.extract_components(sections.tabline_b, attributes_b, window), true) tabline_c = insert_component_separators(util.extract_components(sections.tabline_c, attributes_c, window), true) diff --git a/plugin/tabline/tabs.lua b/plugin/tabline/tabs.lua index 12b17bc..a7d8a54 100644 --- a/plugin/tabline/tabs.lua +++ b/plugin/tabline/tabs.lua @@ -1,5 +1,6 @@ local config = require('tabline.config') local util = require('tabline.util') +local extension = require('tabline.extension') local M = {} @@ -10,27 +11,39 @@ local active_attributes, inactive_attributes, active_separator_attributes, inact local tab_active, tab_inactive = {}, {} local function create_attributes(hover) + local colors = config.colors.tab + for _, ext in pairs(extension.extensions) do + if ext.colors and ext.colors.tab then + colors = util.deep_extend(util.deep_copy(colors), ext.colors.tab) + end + end active_attributes = { - { Foreground = { Color = config.colors.tab.active.fg } }, - { Background = { Color = config.colors.tab.active.bg } }, + { Foreground = { Color = colors.active.fg } }, + { Background = { Color = colors.active.bg } }, } inactive_attributes = { - { Foreground = { Color = hover and config.colors.tab.inactive_hover.fg or config.colors.tab.inactive.fg } }, - { Background = { Color = hover and config.colors.tab.inactive_hover.bg or config.colors.tab.inactive.bg } }, + { Foreground = { Color = hover and colors.inactive_hover.fg or colors.inactive.fg } }, + { Background = { Color = hover and colors.inactive_hover.bg or colors.inactive.bg } }, } active_separator_attributes = { - { Foreground = { Color = config.colors.tab.active.bg } }, - { Background = { Color = config.colors.tab.inactive.bg } }, + { Foreground = { Color = colors.active.bg } }, + { Background = { Color = colors.inactive.bg } }, } inactive_separator_attributes = { - { Foreground = { Color = hover and config.colors.tab.inactive_hover.bg or config.colors.tab.inactive.bg } }, - { Background = { Color = config.colors.tab.inactive.bg } }, + { Foreground = { Color = hover and colors.inactive_hover.bg or colors.inactive.bg } }, + { Background = { Color = colors.inactive.bg } }, } end local function create_tab_content(tab) - tab_active = util.extract_components(config.sections.tab_active, active_attributes, tab) - tab_inactive = util.extract_components(config.sections.tab_inactive, inactive_attributes, tab) + local sections = config.sections + for _, ext in pairs(extension.extensions) do + if ext.sections then + sections = util.deep_extend(util.deep_copy(sections), ext.sections) + end + end + tab_active = util.extract_components(sections.tab_active, active_attributes, tab) + tab_inactive = util.extract_components(sections.tab_inactive, inactive_attributes, tab) end local function tabs(tab) From 56d22a7f83ddbbfda7d032a39f466c3992f46152 Mon Sep 17 00:00:00 2001 From: Michael Brusegard <56915010+michaelbrusegard@users.noreply.github.com> Date: Tue, 24 Sep 2024 05:49:46 +0200 Subject: [PATCH 3/3] close fuzzy finder extensions when other fuzzy finder extensions are activated --- plugin/tabline/extensions/quick_domains.lua | 7 ++++++- plugin/tabline/extensions/resurrect.lua | 6 +++++- plugin/tabline/extensions/smart_workspace_switcher.lua | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugin/tabline/extensions/quick_domains.lua b/plugin/tabline/extensions/quick_domains.lua index e6a7759..f211b92 100644 --- a/plugin/tabline/extensions/quick_domains.lua +++ b/plugin/tabline/extensions/quick_domains.lua @@ -5,7 +5,12 @@ return { 'quick_domains', events = { show = 'quick_domain.fuzzy_selector.opened', - hide = { 'quick_domain.fuzzy_selector.canceled', 'quick_domain.fuzzy_selector.selected' }, + hide = { + 'quick_domain.fuzzy_selector.canceled', + 'quick_domain.fuzzy_selector.selected', + 'resurrect.fuzzy_load.start', + 'smart_workspace_switcher.workspace_switcher.start', + }, }, sections = { tabline_a = { diff --git a/plugin/tabline/extensions/resurrect.lua b/plugin/tabline/extensions/resurrect.lua index 881c2ad..26c8f07 100644 --- a/plugin/tabline/extensions/resurrect.lua +++ b/plugin/tabline/extensions/resurrect.lua @@ -6,7 +6,11 @@ return { 'resurrect', events = { show = 'resurrect.fuzzy_load.start', - hide = 'resurrect.fuzzy_load.finished', + hide = { + 'resurrect.fuzzy_load.finished', + 'quick_domain.fuzzy_selector.opened', + 'smart_workspace_switcher.workspace_switcher.start', + }, }, sections = { tabline_a = { diff --git a/plugin/tabline/extensions/smart_workspace_switcher.lua b/plugin/tabline/extensions/smart_workspace_switcher.lua index ba7e215..4fb40ff 100644 --- a/plugin/tabline/extensions/smart_workspace_switcher.lua +++ b/plugin/tabline/extensions/smart_workspace_switcher.lua @@ -9,6 +9,8 @@ return { 'smart_workspace_switcher.workspace_switcher.canceled', 'smart_workspace_switcher.workspace_switcher.chosen', 'smart_workspace_switcher.workspace_switcher.created', + 'resurrect.fuzzy_load.start', + 'quick_domain.fuzzy_selector.opened', }, }, sections = {