feat(wezterm): improve tab bar to be like tmux

This commit is contained in:
Price Hiller 2023-05-04 01:31:59 -05:00
parent db8b6d2ab2
commit 568454493e
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8

View File

@ -42,9 +42,7 @@ local color_names = {
},
}
local get_colors = function(color_name)
local colors = {
local colors = {
tokyonight_night = {
foreground = "#c0caf5",
background = "#0f111d",
@ -94,56 +92,50 @@ local get_colors = function(color_name)
indexed = { [16] = color_names.kanagawa.surimiOrange, [17] = color_names.kanagawa.peachRed },
tab_bar = {
background = color_names.kanagawa.sumiInk0,
active_tab = {
bg_color = color_names.kanagawa.sumiInk2,
fg_color = color_names.kanagawa.oniViolet,
},
inactive_tab_edge = color_names.kanagawa.sumiInk1,
inactive_tab = {
bg_color = color_names.kanagawa.sumiInk1,
fg_color = color_names.kanagawa.sumiInk4
bg_color = color_names.kanagawa.sumiInk0,
fg_color = color_names.kanagawa.sumiInk4,
},
inactive_tab_hover = {
bg_color = color_names.kanagawa.sumiInk3,
fg_color = color_names.kanagawa.sakuraPink
bg_color = color_names.kanagawa.sumiInk1,
fg_color = color_names.kanagawa.sakuraPink,
},
new_tab = {
bg_color = color_names.kanagawa.sumiInk0,
fg_color = color_names.kanagawa.oniViolet
fg_color = color_names.kanagawa.oniViolet,
},
new_tab_hover = {
bg_color = color_names.kanagawa.sumiInk3,
fg_color = color_names.kanagawa.sakuraPink,
},
bg_color = color_names.kanagawa.sumiInk0,
fg_color = color_names.kanagawa.peachRed,
},
},
}
},
}
M.theme = colors.kanagawa
local chosen_colors = colors[color_name]
if not chosen_colors then
local fallback_color = "kanagawa"
chosen_colors = colors[fallback_color]
wezterm.log_warn("No color '" .. color_name .. "' exists! Falling back to '" .. fallback_color .. "'!")
color_name = fallback_color
end
return {
palette = chosen_colors,
palette_name = color_name
}
end
-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.
local palette = get_colors("kanagawa")
M.theme = palette.palette
local edges = {
solid = {
left = "",
right = "",
},
empty = {
left = "",
right = "",
},
}
if palette.palette_name == "kanagawa" then
wezterm.log_info("Setting tab format for kanagawa")
-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.
---@diagnostic disable-next-line: unused-local
wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
local function tab_title(tab_info)
local title = tab_info.tab_title
-- if the tab title is explicitly set, take that
@ -155,17 +147,132 @@ if palette.palette_name == "kanagawa" then
return tab_info.active_pane.title
end
wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
local title = tab_title(tab)
local bg = color_names.kanagawa.sumiInk0
local fg = color_names.kanagawa.sumiInk4
-- ensure that the titles fit in the available space,
-- and that we have room for the edges.
title = " " .. wezterm.truncate_right(title,max_width) .. " "
local edge_bg = color_names.kanagawa.sumiInk0
local edge_fg = color_names.kanagawa.oniViolet
local title = " " .. wezterm.truncate_right(tab_title(tab), max_width - 2) .. " "
if tab.is_active then
bg = color_names.kanagawa.sumiInk0
fg = color_names.kanagawa.oniViolet
elseif hover then
bg = color_names.kanagawa.sumiInk2
fg = color_names.kanagawa.peachRed
end
return {
{ Background = { Color = edge_bg } },
{ Foreground = { Color = edge_fg } },
{ Background = { Color = bg } },
{ Foreground = { Color = fg } },
{ Text = title },
{ Background = { Color = edge_bg } },
{ Foreground = { Color = fg } },
{ Text = edges.empty.left },
{ Background = { Color = bg } },
{ Foreground = { Color = fg } },
}
end)
end
end)
wezterm.on("update-status", function(window, pane)
-- Each element holds the text for a cell in a "powerline" style << fade
-- Figure out the cwd and host of the current pane.
-- This will pick up the hostname for the remote host if your
-- shell is using OSC 7 on the remote host.
local cwd_uri = pane:get_current_working_dir()
local hostname = ""
local cwd = ""
if cwd_uri then
cwd_uri = cwd_uri:sub(8)
local slash = cwd_uri:find("/")
if slash then
hostname = cwd_uri:sub(1, slash - 1)
-- Remove the domain name portion of the hostname
local dot = hostname:find("[.]")
if dot then
hostname = hostname:sub(1, dot - 1)
end
hostname = "@" .. hostname
-- and extract the cwd from the uri
cwd = cwd_uri:sub(slash)
end
end
local date = wezterm.strftime("%a, %b %-d, %I:%M %p")
-- An entry for each battery (typically 0 or 1 battery)
local battery = ""
for _, b in ipairs(wezterm.battery_info()) do
local charge_percent = b.state_of_charge * 100
local battery_icon = ""
if charge_percent < 100 then
battery_icon = ""
elseif charge_percent < 70 then
battery_icon = ""
elseif charge_percent < 60 then
battery_icon = ""
elseif charge_percent < 50 then
battery_icon = ""
elseif charge_percent < 40 then
battery_icon = ""
elseif charge_percent < 30 then
battery_icon = ""
elseif charge_percent < 20 then
battery_icon = ""
elseif charge_percent < 10 then
battery_icon = ""
end
battery = battery_icon .. " " .. string.format("%.0f%%", charge_percent)
end
-- Color palette for the backgrounds of each cell
local fade_colors = {
{ bg = color_names.kanagawa.sumiInk1, fg = color_names.kanagawa.springGreen },
{ bg = color_names.kanagawa.sumiInk2, fg = color_names.kanagawa.crystalBlue },
{ bg = color_names.kanagawa.sumiInk3, fg = color_names.kanagawa.surimiOrange },
{ bg = color_names.kanagawa.sumiInk4, fg = color_names.kanagawa.fujiWhite },
}
-- The elements to be formatted
local elements = {}
-- Ensure the items have a "cap" on them
table.insert(elements, { Foreground = { Color = fade_colors[1].bg } })
table.insert(elements, { Text = edges.solid.right })
-- How many cells have been formatted
local num_cells = 0
-- Translate a cell into elements
local function push(text, is_last)
local cell_no = num_cells + 1
table.insert(elements, { Foreground = { Color = fade_colors[cell_no].fg } })
table.insert(elements, { Background = { Color = fade_colors[cell_no].bg } })
table.insert(elements, { Text = " " .. text .. " " })
if not is_last then
table.insert(elements, { Foreground = { Color = fade_colors[cell_no + 1].bg } })
table.insert(elements, { Text = edges.solid.right })
end
num_cells = num_cells + 1
end
local cells = {
cwd,
battery,
date,
hostname,
}
while #cells > 0 do
local cell = table.remove(cells, 1)
push(cell, #cells == 0)
end
window:set_right_status(wezterm.format(elements))
end)
return M