Skip to content

Instantly share code, notes, and snippets.

@doole
Last active January 9, 2025 10:31
Show Gist options
  • Save doole/5619a29cd0a5c21417379f07968bdc6c to your computer and use it in GitHub Desktop.
Save doole/5619a29cd0a5c21417379f07968bdc6c to your computer and use it in GitHub Desktop.
-- vim: tabstop=2 shiftwidth=2 expandtab
local wezterm = require 'wezterm'
local module = {}
-- Returns a bool based on whether the host operating system's
-- appearance is light or dark.
function module.is_dark()
-- wezterm.gui is not always available, depending on what
-- environment wezterm is operating in. Just return true
-- if it's not defined.
if wezterm.gui then
-- Some systems report appearance like "Dark High Contrast"
-- so let's just look for the string "Dark" and if we find
-- it assume appearance is dark.
return wezterm.gui.get_appearance():find("Dark")
end
return true
end
return module
-- vim: tabstop=2 shiftwidth=2 expandtab
local wezterm = require 'wezterm'
local mux = wezterm.mux
local module = {}
local function project_dirs()
local projects = {}
local project_dir = wezterm.home_dir .. "/src/codeberg"
local work_dir = wezterm.home_dir .. "/Projects"
table.insert(projects, work_dir)
-- WezTerm comes with a glob function! Let's use it to get a lua table
-- containing all subdirectories of your project folder.
for _, dir in ipairs(wezterm.glob(project_dir .. '/*')) do
-- ... and add them to the projects table.
table.insert(projects, dir)
end
return projects
end
local function has_value(tab, val)
for _index, value in ipairs(tab) do
if value == val then return true end
end
return false
end
-- TODO: improve
function module.load_work()
return wezterm.action_callback(function(_win, _pane)
local workspace_name = 'PROJECT'
local work_dir = wezterm.home_dir .. '/Projects/PROJECT'
if has_value(mux.get_workspace_names(), workspace_name) then
mux.set_active_workspace(workspace_name)
return
end
-- open your windows, panes, etc here, note `workspace` key
-- which causes new workspace to be created if it doesn't exist yet
local tab, build_pane, window = mux.spawn_window {
workspace = workspace_name,
cwd = work_dir .. '/src',
}
tab:set_title('app')
build_pane:send_text('git fetch\n')
local tab2, pane2, _window2 = window:spawn_tab({})
tab2:set_title('server')
local pane2bottom = pane2:split {
direction = 'Top',
size = 0.7,
}
pane2bottom:send_text('docker attach $(docker compose ps -q api)')
pane2:send_text('docker compose logs -f api')
local tab3, pane3, _window3 = window:spawn_tab({})
tab3:set_title('console')
pane3:send_text('docker compose exec api bundle exec rails c --sandbox')
local tab4, pane4, _window4 = window:spawn_tab({})
tab4:set_title('tests')
pane4:send_text('docker compose exec api_test bundle exec rspec spec/')
local tab5, pane5, _window5 = window:spawn_tab({})
tab5:set_title('db')
pane5:send_text('docker compose exec -e PGPASSWORD=rails api psql -h postgres -U rails')
local tab6 = window:spawn_tab({ cwd = wezterm.home_dir })
tab6:set_title('shell')
tab:activate()
mux.set_active_workspace(workspace_name)
end)
end
function module.choose_project()
local choices = {}
for _, value in ipairs(project_dirs()) do
table.insert(choices, { label = value })
end
-- The InputSelector action presents a modal UI for choosing between a set of options
-- within WezTerm.
return wezterm.action.InputSelector {
title = 'Projects',
choices = choices,
fuzzy = true,
-- The action we want to perform. Note that this doesn't have to be a
-- static definition as we've done before, but can be a callback that
-- evaluates any arbitrary code.
action = wezterm.action_callback(function(child_window, child_pane, id, label)
-- "label" may be empty if nothing was selected. Don't bother doing anything
-- when that happens.
if not label then return end
-- The SwitchToWorkspace action will switch us to a workspace if it already exists,
-- otherwise it will create it for us.
child_window:perform_action(wezterm.action.SwitchToWorkspace {
-- We'll give our new workspace a nice name, like the last path segment
-- of the directory we're opening up.
name = label:match("([^/]+)$"),
-- Here's the meat. We'll spawn a new terminal with the current working
-- directory set to the directory that was picked.
spawn = { cwd = label },
}, child_pane)
end),
}
end
return module
-- vim: tabstop=2 shiftwidth=2 expandtab
-- taken from https://alexplescan.com/posts/2024/08/10/wezterm/
local wezterm = require 'wezterm'
local config = wezterm.config_builder()
local mux = wezterm.mux
local act = wezterm.action
local appearance = require 'appearance'
local projects = require 'projects'
config.set_environment_variables = {
PATH = '/opt/homebrew/bin:' .. os.getenv('PATH')
}
if appearance.is_dark() then
config.color_scheme = 'Catppuccin Mocha'
else
config.color_scheme = 'Catppuccin Mocha'
end
-- config.font = wezterm.font({ family = 'JetBrains Mono' })
config.font_size = 13
config.initial_cols = 170
config.initial_rows = 55
config.scrollback_lines = 10000
config.window_padding = { left = 0, right = 0, bottom = 0, top = 0, }
config.window_background_opacity = 0.97
config.macos_window_background_blur = 30
-- Removes the title bar, leaving only the tab bar. Keeps
-- the ability to resize by dragging the window's edges.
-- On macOS, 'RESIZE|INTEGRATED_BUTTONS' also looks nice if
-- you want to keep the window controls visible and integrate
-- them into the tab bar.
-- config.window_decorations = 'RESIZE'
config.window_decorations = 'RESIZE|INTEGRATED_BUTTONS'
-- Sets the font for the window frame (tab bar)
config.window_frame = {
font = wezterm.font({ family = 'JetBrains Mono', weight = 'Bold' }),
font_size = 11,
}
-- config.hide_tab_bar_if_only_one_tab = true
-- config.use_fancy_tab_bar = true
wezterm.on('gui-startup', function(cmd)
local tab, pane, window = mux.spawn_window(cmd or {})
window:gui_window():maximize()
end)
local function segments_for_right_status(window)
return {
window:active_workspace(),
wezterm.strftime('%a, %-d %b %H:%M'),
wezterm.hostname(),
}
end
wezterm.on('update-status', function(window, _)
local SOLID_LEFT_ARROW = wezterm.nerdfonts.pl_right_hard_divider
local segments = segments_for_right_status(window)
local color_scheme = window:effective_config().resolved_palette
-- Note the use of wezterm.color.parse here, this returns
-- a Color object, which comes with functionality for lightening
-- or darkening the colour (amongst other things).
local bg = wezterm.color.parse(color_scheme.background)
local fg = color_scheme.foreground
-- Each powerline segment is going to be coloured progressively
-- darker/lighter depending on whether we're on a dark/light colour
-- scheme. Let's establish the "from" and "to" bounds of our gradient.
local gradient_to, gradient_from = bg, bg
if appearance.is_dark() then
gradient_from = gradient_to:lighten(0.2)
else
gradient_from = gradient_to:darken(0.2)
end
-- Yes, WezTerm supports creating gradients, because why not?! Although
-- they'd usually be used for setting high fidelity gradients on your terminal's
-- background, we'll use them here to give us a sample of the powerline segment
-- colours we need.
local gradient = wezterm.color.gradient(
{
orientation = 'Horizontal',
colors = { gradient_from, gradient_to },
},
#segments -- only gives us as many colours as we have segments.
)
-- We'll build up the elements to send to wezterm.format in this table.
local elements = {}
for i, seg in ipairs(segments) do
local is_first = i == 1
if is_first then
table.insert(elements, { Background = { Color = 'none' } })
end
table.insert(elements, { Foreground = { Color = gradient[i] } })
table.insert(elements, { Text = SOLID_LEFT_ARROW })
table.insert(elements, { Foreground = { Color = fg } })
table.insert(elements, { Background = { Color = gradient[i] } })
table.insert(elements, { Text = ' ' .. seg .. ' ' })
end
window:set_right_status(wezterm.format(elements))
end)
local function move_pane(key, direction)
return {
key = key,
mods = 'LEADER',
action = act.ActivatePaneDirection(direction),
}
end
local function resize_pane(key, direction)
return {
key = key,
action = act.AdjustPaneSize { direction, 3 }
}
end
-- If you're using emacs you probably wanna choose a different leader here,
-- since we're gonna be making it a bit harder to CTRL + A for jumping to
-- the start of a line
config.leader = { key = '`', mods = '', timeout_milliseconds = 1000 }
-- Table mapping keypresses to actions
config.keys = {
-- Sends ESC + b and ESC + f sequence, which is used
-- for telling your shell to jump back/forward.
{
-- When the left arrow is pressed
key = 'LeftArrow',
-- With the "Option" key modifier held down
mods = 'OPT',
-- Perform this action, in this case - sending ESC + B
-- to the terminal
action = act.SendString '\x1bb',
},
{
key = 'RightArrow',
mods = 'OPT',
action = act.SendString '\x1bf',
},
{
key = ',',
mods = 'SUPER',
action = act.SpawnCommandInNewTab {
cwd = wezterm.home_dir,
args = { 'nvim', wezterm.config_file },
},
},
{
key = '|',
-- Note that instead of a key modifier mapped to a key on your keyboard
-- like CTRL or ALT, we can use the LEADER modifier instead.
-- This means that this binding will be invoked when you press the leader
-- (CTRL + A), quickly followed by quotes (").
mods = 'LEADER',
action = act.SplitHorizontal { domain = 'CurrentPaneDomain' },
},
{
key = '-',
mods = 'LEADER',
action = act.SplitVertical { domain = 'CurrentPaneDomain' },
},
{
key = '`',
mods = 'LEADER',
action = act.SendKey { key = '`', mods = '' },
},
move_pane('j', 'Down'),
move_pane('k', 'Up'),
move_pane('h', 'Left'),
move_pane('l', 'Right'),
{
-- When we push LEADER + R...
key = 'r',
mods = 'LEADER',
-- Activate the `resize_panes` keytable
action = act.ActivateKeyTable {
name = 'resize_panes',
-- Ensures the keytable stays active after it handles its
-- first keypress.
one_shot = false,
-- Deactivate the keytable after a timeout.
timeout_milliseconds = 1000,
}
},
{
key = 'w',
mods = 'LEADER',
action = projects.load_work(),
},
{
key = 'p',
mods = 'LEADER',
-- Present in to our project picker
action = projects.choose_project(),
},
{
key = 'f',
mods = 'LEADER',
-- Present a list of existing workspaces
action = act.ShowLauncherArgs { flags = 'FUZZY|WORKSPACES' },
},
{
key = ',',
mods = 'LEADER',
action = act.PromptInputLine {
description = 'Enter new name for tab',
action = wezterm.action_callback(function(window, _, line)
-- line will be `nil` if they hit escape without entering anything
-- An empty string if they just hit enter
-- Or the actual line of text they wrote
if line then
window:active_tab():set_title(line)
end
end),
},
},
}
config.key_tables = {
resize_panes = {
resize_pane('j', 'Down'),
resize_pane('k', 'Up'),
resize_pane('h', 'Left'),
resize_pane('l', 'Right'),
},
}
-- Returns our config to be evaluated. We must always do this at the bottom of this file
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment