Skip to content

Commit

Permalink
feat: improve launch menu
Browse files Browse the repository at this point in the history
* Improved the launch menu that appears when new-tab button is right
  clicked.
  The new menu shows all launch items with a corresponding icon for its
  launch Domain.
  The new menu is now also able to select he `DefaultDomain` when
  launching items with args.
* Adjust tab-title `inset` and add icon when `InputSelector` is active
* Fix broken regex in `BackDrops:choices()`
  • Loading branch information
KevinSilvester committed Oct 10, 2024
1 parent 99da209 commit f34530d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 14 deletions.
5 changes: 4 additions & 1 deletion config/bindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ local keys = {
key = [[/]],
mods = mod.SUPER_REV,
action = act.InputSelector({
title = 'Select Background',
title = 'InputSelector: Select Background',
choices = backdrops:choices(),
fuzzy = true,
fuzzy_description = 'Select Background: ',
action = wezterm.action_callback(function(window, _pane, idx)
if not idx then
return
end
---@diagnostic disable-next-line: param-type-mismatch
backdrops:set_img(window, tonumber(idx))
end),
Expand Down
114 changes: 105 additions & 9 deletions events/new-tab-button.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,123 @@
local wezterm = require('wezterm')
local launch_menu = require('config.launch').launch_menu
local domains = require('config.domains')
local Cells = require('utils.cells')

local nf = wezterm.nerdfonts
local act = wezterm.action
local attr = Cells.attr

local M = {}

-- stylua: ignore
local colors = {
label_text = { bg = 'rgba(0, 0, 0, 0)', fg = '#CDD6F4' },
icon_default = { bg = 'rgba(0, 0, 0, 0)', fg = '#89B4FA' },
icon_wsl = { bg = 'rgba(0, 0, 0, 0)', fg = '#FAB387' },
icon_ssh = { bg = 'rgba(0, 0, 0, 0)', fg = '#F38BA8' },
icon_unix = { bg = 'rgba(0, 0, 0, 0)', fg = '#CBA6F7' },
}

local cells = Cells:new(colors)
:add_segment('icon_default', ' ' .. nf.md_domain .. ' ', 'icon_default')
:add_segment('icon_wsl', ' ' .. nf.cod_terminal_linux .. ' ', 'icon_wsl')
:add_segment('icon_ssh', ' ' .. nf.md_ssh .. ' ', 'icon_ssh')
:add_segment('icon_unix', ' ' .. nf.dev_gnu .. ' ', 'icon_unix')
:add_segment('label_text', '', 'label_text', attr(attr.intensity('Bold')))

local function build_choices()
local choices = {}
local choices_data = {}
local idx = 1

-- Add launch menu items (DefaultDomain)
for _, v in ipairs(launch_menu) do
cells:update_segment_text('label_text', v.label)

table.insert(choices, {
id = tostring(idx),
label = wezterm.format(cells:render({ 'icon_default', 'label_text' })),
})
table.insert(choices_data, {
args = v.args,
domain = 'DefaultDomain',
})
idx = idx + 1
end

-- Add WSL domains
for _, v in ipairs(domains.wsl_domains) do
cells:update_segment_text('label_text', v.name)

table.insert(choices, {
id = tostring(idx),
label = wezterm.format(cells:render({ 'icon_wsl', 'label_text' })),
})
table.insert(choices_data, {
domain = { DomainName = v.name },
})
idx = idx + 1
end

-- Add SSH domains
for _, v in ipairs(domains.ssh_domains) do
cells:update_segment_text('label_text', v.name)
table.insert(choices, {
id = tostring(idx),
label = wezterm.format(cells:render({ 'icon_ssh', 'label_text' })),
})
table.insert(choices_data, {
domain = { DomainName = v.name },
})
idx = idx + 1
end

-- Add Unix domains
for _, v in ipairs(domains.unix_domains) do
cells:update_segment_text('label_text', v.name)
table.insert(choices, {
id = tostring(idx),
label = wezterm.format(cells:render({ 'icon_unix', 'label_text' })),
})
table.insert(choices_data, {
domain = { DomainName = v.name },
})
idx = idx + 1
end

return choices, choices_data
end

local choices, choices_data = build_choices()

M.setup = function()
wezterm.on('new-tab-button-click', function(window, pane, button, default_action)
if default_action and button == 'Left' then
window:perform_action(default_action, pane)
end

if default_action and button == 'Right' then
-- window:perform_action(
-- wezterm.action.ShowLauncherArgs({
-- title = nf.fa_rocket .. ' Select/Search:',
-- flags = 'FUZZY|LAUNCH_MENU_ITEMS|DOMAINS',
-- }),
-- pane
-- )
-- window:perform_action(wezterm.action.SpawnTab({ DomainName = 'WSL:Ubuntu'}), pane)
window:perform_action(wezterm.action.SpawnCommandInNewTab({ args = {'pwsh'}}), pane)
window:perform_action(
act.InputSelector({
title = 'InputSelector: Launch Menu',
choices = choices,
fuzzy = true,
fuzzy_description = nf.md_rocket .. ' Select a lauch item: ',
action = wezterm.action_callback(function(_window, _pane, id, label)
if not id and not label then
return
else
wezterm.log_info('you selected ', id, label)
wezterm.log_info(choices_data[tonumber(id)])
window:perform_action(
act.SpawnCommandInNewTab(choices_data[tonumber(id)]),
pane
)
end
end),
}),
pane
)
end
return false
end)
Expand Down
11 changes: 9 additions & 2 deletions events/tab-title.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ local GLYPH_SCIRCLE_LEFT = nf.ple_left_half_circle_thick --[[  ]]
local GLYPH_SCIRCLE_RIGHT = nf.ple_right_half_circle_thick --[[]]
local GLYPH_CIRCLE = nf.fa_circle --[[]]
local GLYPH_ADMIN = nf.md_shield_half_full --[[ 󰞀 ]]
local GLYPH_UBUNTU = nf.cod_terminal_linux --[[]]
local GLYPH_LINUX = nf.cod_terminal_linux --[[]]
local GLYPH_DEBUG = nf.fa_bug --[[]]
-- local GLYPH_SEARCH = nf.fa_search --[[  ]]
local GLYPH_SEARCH = '🔭'

local TITLE_INSET = {
DEFAULT = 6,
Expand Down Expand Up @@ -75,6 +77,11 @@ local function create_title(process_name, base_title, max_width, inset)
inset = inset - 2
end

if base_title:match('^InputSelector:') ~= nil then
title = base_title:gsub('InputSelector:', GLYPH_SEARCH)
inset = inset - 2
end

if title:len() > max_width - inset then
local diff = title:len() - max_width + inset
title = title:sub(1, title:len() - diff)
Expand Down Expand Up @@ -139,7 +146,7 @@ function Tab:set_cells(is_active, hover)
self.cells
:add_segment('scircle_left', GLYPH_SCIRCLE_LEFT, SEGMENT_COLORS['scircle'][color_variant])
:add_segment('admin', ' ' .. GLYPH_ADMIN, SEGMENT_COLORS['text'][color_variant])
:add_segment('wsl', ' ' .. GLYPH_UBUNTU, SEGMENT_COLORS['text'][color_variant])
:add_segment('wsl', ' ' .. GLYPH_LINUX, SEGMENT_COLORS['text'][color_variant])
:add_segment('title', ' ', SEGMENT_COLORS['text'][color_variant], attr(attr.intensity('Bold')))
:add_segment('unseen_output', ' ' .. GLYPH_CIRCLE, SEGMENT_COLORS['unseen_output'][color_variant])
:add_segment('padding', ' ', SEGMENT_COLORS['text'][color_variant])
Expand Down
3 changes: 1 addition & 2 deletions utils/backdrops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ end
function BackDrops:choices()
local choices = {}
for idx, file in ipairs(self.files) do
local name = file:match('([^' .. PATH_SEP .. ']+)$')
table.insert(choices, {
id = tostring(idx),
label = name,
label = file:match('([^/]+)$'),
})
end
return choices
Expand Down

0 comments on commit f34530d

Please sign in to comment.