Skip to content

Instantly share code, notes, and snippets.

@roktas
Created October 12, 2023 21:02
Show Gist options
  • Save roktas/a92c002a6b919358a1ad6bcc472967b1 to your computer and use it in GitHub Desktop.
Save roktas/a92c002a6b919358a1ad6bcc472967b1 to your computer and use it in GitHub Desktop.
local wezterm = require("wezterm")
------------------------------------------------------------------------------------------------------------------------
-- Constants and helpers
------------------------------------------------------------------------------------------------------------------------
local FONT_FAMILY = wezterm.font("Spleen 32x64")
local FONT_SIZE = 16
local CHAR_SOLID_LEFT = wezterm.nerdfonts.pl_right_hard_divider
local COLOR_BACKGROUND = "#2e3440"
local icons = {
["bash"] = wezterm.nerdfonts.cod_terminal_bash,
["btm"] = wezterm.nerdfonts.md_chart_donut_variant,
["bundle"] = wezterm.nerdfonts.cod_ruby,
["bun"] = wezterm.nerdfonts.md_hexagon,
["cargo"] = wezterm.nerdfonts.dev_rust,
["curl"] = wezterm.nerdfonts.md_flattr,
["docker"] = wezterm.nerdfonts.linux_docker,
["fish"] = wezterm.nerdfonts.dev_terminal,
["gem"] = wezterm.nerdfonts.cod_ruby,
["gh"] = wezterm.nerdfonts.dev_github_badge,
["git"] = wezterm.nerdfonts.dev_git,
["go"] = wezterm.nerdfonts.seti_go,
["htop"] = wezterm.nerdfonts.md_chart_donut_variant,
["idle"] = wezterm.nerdfonts.dev_python,
["irb"] = wezterm.nerdfonts.cod_ruby,
["lazydocker"] = wezterm.nerdfonts.linux_docker,
["lua"] = wezterm.nerdfonts.seti_lua,
["lxc"] = wezterm.nerdfonts.linux_docker,
["lxd"] = wezterm.nerdfonts.linux_docker,
["make"] = wezterm.nerdfonts.seti_makefile,
["mc"] = wezterm.nerdfonts.md_weather_night,
["node"] = wezterm.nerdfonts.md_hexagon,
["nvim"] = wezterm.nerdfonts.custom_vim,
["pip"] = wezterm.nerdfonts.dev_python,
["python"] = wezterm.nerdfonts.dev_python,
["ruby"] = wezterm.nerdfonts.cod_ruby,
["sudo"] = wezterm.nerdfonts.fa_hashtag,
["unknown"] = wezterm.nerdfonts.oct_gear,
["vagrant"] = wezterm.nerdfonts.linux_docker,
["vim"] = wezterm.nerdfonts.dev_vim,
["wget"] = wezterm.nerdfonts.md_arrow_down_box,
["zsh"] = wezterm.nerdfonts.dev_terminal,
}
local function basename(path) return string.gsub(path, "(.*[/\\])(.*)", "%2") end
local function process_name(pane)
local info = pane:get_foreground_process_info()
if info then
return basename(info.executable)
else
return ""
end
end
local function icon(executable)
if executable then
local name = basename(executable)
local candidates = {
name,
string.gsub(name, "^(.-)(%d+%.?%d*)", "%1"),
string.gsub(name, "^(.-)([.-].*)", "%1"),
}
for _, candidate in ipairs(candidates) do
print(candidate)
if candidate then
if icons[candidate] then return icons[candidate] end
end
end
end
return icons["unknown"]
end
------------------------------------------------------------------------------------------------------------------------
-- General settings
------------------------------------------------------------------------------------------------------------------------
local config = {
color_scheme = "nordfox",
font = FONT_FAMILY,
font_size = FONT_SIZE,
window_padding = { bottom = 0 },
use_fancy_tab_bar = false,
scrollback_lines = 10000,
}
------------------------------------------------------------------------------------------------------------------------
-- Colors
------------------------------------------------------------------------------------------------------------------------
config.colors = {
tab_bar = {
background = COLOR_BACKGROUND,
active_tab = { intensity = "Bold", fg_color = "white", bg_color = "#5e81ac" },
},
}
------------------------------------------------------------------------------------------------------------------------
-- Keys
------------------------------------------------------------------------------------------------------------------------
config.keys = {
{
key = "a",
mods = "ALT",
action = wezterm.action.SpawnTab("CurrentPaneDomain")
},
{
key = "b",
mods = "ALT",
action = wezterm.action.SpawnCommandInNewTab { args = { "/bin/bash" } }
},
{
key = "LeftArrow",
mods = "ALT",
action = wezterm.action.ActivateTabRelative(-1)
},
{
key = "LeftArrow",
mods = "ALT|SHIFT",
action = wezterm.action.MoveTabRelative(-1)
},
{
key = "q",
mods = "ALT",
action = wezterm.action.SpawnCommandInNewTab {
set_environment_variables = { SHELL = "/bin/bash" }, args = { "/usr/bin/mc" }
}
},
{
key = "RightArrow",
mods = "ALT",
action = wezterm.action.ActivateTabRelative(1)
},
{
key = "RightArrow",
mods = "ALT|SHIFT",
action = wezterm.action.MoveTabRelative(1)
},
{
key = "s",
mods = "ALT",
action = wezterm.action.ActivateLastTab
},
{
key = "w",
mods = "ALT",
action = wezterm.action.SpawnWindow
},
}
-- Mark/unmark and (later) activate a tab to focus
local focused_tab = nil
table.insert(config.keys, {
key = "F12",
action = wezterm.action_callback(function(_, pane)
local tab = pane:tab()
if focused_tab then
if focused_tab:tab_id() == tab:tab_id() then
focused_tab = nil
else
focused_tab:activate()
end
else
focused_tab = tab
end
end),
})
-- ALT + number to activate that tab
for i = 1, 9 do
table.insert(config.keys, { key = tostring(i), mods = "ALT", action = wezterm.action.ActivateTab(i - 1) })
end
------------------------------------------------------------------------------------------------------------------------
-- Events
------------------------------------------------------------------------------------------------------------------------
wezterm.on("update-right-status", function(window, pane)
local cells = {}
-- Add hostname
local uri = pane:get_current_working_dir()
if uri then
local hostname = uri.host or wezterm.hostname()
local dot = hostname:find("[.]")
if dot then hostname = hostname:sub(1, dot - 1) end
if hostname == "" then hostname = wezterm.hostname() end
table.insert(cells, hostname)
end
-- Add date/time
local date = wezterm.strftime("%Y-%m-%d %H:%M:%S")
table.insert(cells, date)
-- Add process icon
table.insert(cells, icon(process_name(pane)))
-- Color palette for the backgrounds of each cell
local colors = {
"#434c5e",
"#4c566a",
"#bf616a", -- Aurora red in Nord theme
"#434c5e",
}
local text_fg = "white"
-- The elements to be formatted
local elements = {}
-- How many cells have been formatted
local num_cells = 0
-- Translate a cell into elements
-- TODO: bunu renk argümanı ve cell alan başka bir fonksiyon yapıp while döngüsünü explicit manuel yap
local function push(text)
local cell_no = num_cells + 1
table.insert(elements, { Foreground = { Color = colors[cell_no] } })
table.insert(elements, { Text = CHAR_SOLID_LEFT })
table.insert(elements, { Foreground = { Color = text_fg } })
table.insert(elements, { Background = { Color = colors[cell_no] } })
table.insert(elements, { Text = " " .. text .. " " })
table.insert(elements, { Foreground = { Color = colors[cell_no + 1] } })
num_cells = num_cells + 1
end
-- Start with background color
table.insert(elements, { Background = { Color = COLOR_BACKGROUND } })
while #cells > 0 do
local cell = table.remove(cells, 1)
push(cell)
end
window:set_right_status(wezterm.format(elements))
end)
local function tab_title(tab)
local title = tab.tab_title
-- If the tab title is explicitly set, take that
if title and #title > 0 then return title end
title = string.format("%-15s", string.format(" %d: %s ", tab.tab_index + 1, icon(tab.active_pane.foreground_process_name)))
-- Format focused tab specially
if focused_tab and focused_tab:tab_id() == tab.tab_id then
return {
{ Background = { Color = "#bf616a" } },
{ Foreground = { Color = "#e0e0e0" } },
{ Text = title },
}
else
return title
end
end
wezterm.on("format-tab-title", function(tab, _, _, _, _, _) return tab_title(tab) end)
config.default_prog = { '/usr/bin/fish', '-l' }
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment