Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension state #33

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
79 changes: 43 additions & 36 deletions plugin/tabline/component.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -20,57 +21,63 @@ 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 = 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

Expand All @@ -87,18 +94,18 @@ 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
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)
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()
Expand Down
13 changes: 5 additions & 8 deletions plugin/tabline/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ M.opts = {}
M.sections = {}
M.component_opts = {}
M.colors = {}
M.normal_mode_colors = {}

local default_opts = {
options = {
Expand Down Expand Up @@ -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 },
Expand Down
86 changes: 53 additions & 33 deletions plugin/tabline/extension.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugin/tabline/extensions/presentation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local config = require('tabline.config')

return {
{
'presentation',
events = {
show = 'xarvex.presentation.activate',
hide = 'xarvex.presentation.deactivate',
Expand Down
8 changes: 7 additions & 1 deletion plugin/tabline/extensions/quick_domains.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ 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' },
hide = {
'quick_domain.fuzzy_selector.canceled',
'quick_domain.fuzzy_selector.selected',
'resurrect.fuzzy_load.start',
'smart_workspace_switcher.workspace_switcher.start',
},
},
sections = {
tabline_a = {
Expand Down
8 changes: 7 additions & 1 deletion plugin/tabline/extensions/resurrect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ local config = require('tabline.config')

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 = {
Expand All @@ -22,6 +27,7 @@ return {
},
},
{
'resurrect.periodic_save',
events = {
show = 'resurrect.periodic_save',
delay = 7,
Expand Down
3 changes: 3 additions & 0 deletions plugin/tabline/extensions/smart_workspace_switcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ local config = require('tabline.config')

return {
{
'smart_workspace_switcher',
events = {
show = 'smart_workspace_switcher.workspace_switcher.start',
hide = {
'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 = {
Expand Down
Loading