-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwezterm.lua
More file actions
483 lines (421 loc) · 14.4 KB
/
wezterm.lua
File metadata and controls
483 lines (421 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
local wezterm = require 'wezterm'
local config = wezterm.config_builder()
--- 8< -- 8< ---
-- Fonts
-- local font = 'CaskaydiaMono NF'
local font = 'JetBrainsMono NF'
local font_iosevka = wezterm.font {
family = 'JetBrainsMono Nerd Font Propo',
weight = 'Bold',
}
local font_jetbrains = wezterm.font {
family = 'JetBrainsMono Nerd Font',
weight = 'DemiBold',
}
-- Function to check if Neovim is the active process
local function is_nvim(pane)
return pane:get_foreground_process_name():find('n?vim') ~= nil
end
-- Function to update font based on active process
local function update_font(window, pane)
local overrides = window:get_config_overrides() or {}
if is_nvim(pane) then
if overrides.font ~= font_jetbrains or overrides.font_size ~= 14.0 then
wezterm.log_info('Changing font for Neovim')
window:set_config_overrides({
font = font_jetbrains,
font_size = 14.0,
})
end
else
if overrides.font ~= font_iosevka or overrides.font_size ~= 12.0 then
wezterm.log_info('Changing font for terminal')
window:set_config_overrides({
font = font_iosevka,
font_size = 14.0,
})
end
end
end
-- Event handler for window configuration reloaded
wezterm.on('window-config-reloaded', function(window)
local pane = window:active_pane()
update_font(window, pane)
end)
-- Event handler for updating right status
wezterm.on('update-right-status', function(window, pane)
-- Update font if needed
update_font(window, pane)
local date = wezterm.strftime '%a %b %-d %H:%M '
local bat = ''
for _, b in ipairs(wezterm.battery_info()) do
bat = '🔋 ' .. string.format('%.0f%%', b.state_of_charge * 100)
end
local overrides = window:get_config_overrides() or {}
local font_name = overrides.font and overrides.font.family or 'Unknown'
local font_size = overrides.font_size or 'Unknown'
window:set_right_status(wezterm.format {
{ Text = bat .. ' ' .. date .. ' Font: ' .. font_name .. ', Size: ' .. font_size },
})
end)
-- Listen for updates to the configuration
config.font = font_iosevka
config.font_size = 16
config.debug_key_events = true
-- config.font =
-- wezterm.font_with_fallback({
-- { family = font, weight = 'DemiBold', italic = false },
-- -- { family = 'Symbols Nerd Font Mono', scale = 1 },
-- })
config.window_background_opacity = 0.9
config.macos_window_background_blur = 30
config.default_prog = { '/opt/homebrew/bin/nu'}
-- config.font_rules = {
-- {
-- intensity = 'Bold',
-- font = wezterm.font_with_fallback({
-- { family = font, weight = 'Regular', italic = false, weight = 'Bold' },
-- { family = 'Symbols Nerd Font Mono', scale = 1 },
-- }),
-- }
-- }
-- Disable font ligatures
config.harfbuzz_features = { 'calt=1', 'clig=0', 'liga=0', 'zero', 'ss01' }
-- Colors
config.color_scheme = 'Catppuccin Mocha (Gogh)'
-- config.color_scheme = 'Dracula (Gogh)'
config.window_frame = {
font = wezterm.font { family = font, weight = 'Bold' },
font_size = 12,
-- Fancy tab bar
active_titlebar_bg = '#313244',
inactive_titlebar_bg = '#7f849c',
}
-- Command Palette
config.command_palette_rows = 7
config.command_palette_font_size = 13
config.command_palette_bg_color = "#585b70"
config.command_palette_fg_color = "#cba6f7"
-- Hot keys
config.keys = {
-- Make Page up/down work
{ key = 'PageUp', action = wezterm.action.ScrollByPage(-1) },
{ key = 'PageDown', action = wezterm.action.ScrollByPage(1) },
-- Pane splitting
{
key = 'g',
mods = 'CMD',
action = wezterm.action.SplitHorizontal({ domain = 'CurrentPaneDomain' }),
},
{
key = 'd',
mods = 'CMD',
action = wezterm.action.CloseCurrentPane({ confirm = false }),
},
-- Switch between tabs
{
key = 'LeftArrow',
mods = 'CMD',
action = wezterm.action.ActivateTabRelative(-1),
},
{
key = 'RightArrow',
mods = 'CMD',
action = wezterm.action.ActivateTabRelative(1),
},
-- Switch between panes
{
key = 'LeftArrow',
mods = 'SHIFT',
action = wezterm.action.ActivatePaneDirection('Prev'),
},
{
key = 'RightArrow',
mods = 'SHIFT',
action = wezterm.action.ActivatePaneDirection('Next'),
},
-- Jump word to the left
{
key = 'LeftArrow',
mods = 'OPT',
action = wezterm.action.SendKey({ key = 'b', mods = 'ALT' }),
},
-- Jump word to the right
{
key = 'RightArrow',
mods = 'OPT',
action = wezterm.action.SendKey({ key = 'f', mods = 'ALT' }),
},
-- Go to beginning of line
{
key = 'LeftArrow',
mods = 'CMD',
action = wezterm.action.SendKey({
key = 'a',
mods = 'CTRL',
}),
},
-- Go to end of line
{
key = 'RightArrow',
mods = 'CMD',
action = wezterm.action.SendKey({ key = 'e', mods = 'CTRL' }),
},
-- Delete line to the left of the cursor
-- TODO: It actually deletes the whole line, but it's close enough
{
key = 'Backspace',
mods = 'CMD',
action = wezterm.action.SendKey({ key = 'u', mods = 'CTRL' }),
},
-- Case-insensitive search
{
key = 'f',
mods = 'CMD',
action = wezterm.action.Search({ CaseInSensitiveString = '' }),
},
-- Open WezTerm config file quickly
-- {
-- key = ',',
-- mods = 'CMD',
-- action = wezterm.action.SpawnCommandInNewTab {
-- cwd = os.getenv('WEZTERM_CONFIG_DIR'),
-- set_environment_variables = {
-- TERM = 'screen-256color',
-- },
-- args = {
-- '/Applications/CotEditor.app/Contents/MacOS/CotEditor',
-- os.getenv('WEZTERM_CONFIG_FILE'),
-- },
-- },
-- },
-- Disable some default hotkeys
{
key = 'Enter',
mods = 'OPT',
action = wezterm.action.DisableDefaultAssignment,
},
-- Rename tab title
{
key = 'R',
mods = 'CMD|SHIFT',
action = wezterm.action.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),
},
},
}
-- Mouse
config.mouse_bindings = {
-- Change the default click behavior so that it only selects
-- text and doesn't open hyperlinks
{
event = { Up = { streak = 1, button = 'Left' } },
mods = 'NONE',
action = wezterm.action.CompleteSelection('ClipboardAndPrimarySelection'),
},
-- Open links on Cmd+click
{
event = { Up = { streak = 1, button = 'Left' } },
mods = 'CMD',
action = wezterm.action.OpenLinkAtMouseCursor,
},
}
-- Visual bell
config.audible_bell = 'Disabled'
config.visual_bell = {
target = "CursorColor",
fade_in_function = "EaseIn",
fade_in_duration_ms = 150,
fade_out_function = "EaseOut",
fade_out_duration_ms = 300,
}
-- Misc
config.adjust_window_size_when_changing_font_size = false
config.bold_brightens_ansi_colors = 'No'
config.cursor_thickness = 2
config.default_cursor_style = 'SteadyBar'
config.default_cwd = wezterm.home_dir
config.font_size = 13
config.hyperlink_rules = wezterm.default_hyperlink_rules()
config.inactive_pane_hsb = { saturation = 1.0, brightness = 0.8}
config.line_height = 1.1
config.scrollback_lines = 10000
config.show_new_tab_button_in_tab_bar = false
config.switch_to_last_active_tab_when_closing_tab = true
config.tab_max_width = 60
config.use_fancy_tab_bar = true
config.window_close_confirmation = 'NeverPrompt'
config.window_decorations = "INTEGRATED_BUTTONS|RESIZE"
config.window_padding = { left = 8, right = 8, top = 12, bottom = 8}
local function get_current_working_dir(tab)
local current_dir = tab.active_pane and tab.active_pane.current_working_dir or { file_path = '' }
local HOME_DIR = string.format('file://%s', os.getenv('HOME'))
return current_dir == HOME_DIR and '.'
or string.gsub(current_dir.file_path, '(.*[/\\])(.*)', '%2')
end
-- Set tab title to the one that was set via `tab:set_title()`
-- or fall back to the current working directory as a title
-- wezterm.on('format-tab-title', function(tab, tabs, panes, config, hover, max_width)
-- local index = tonumber(tab.tab_index) + 1
-- local custom_title = tab.tab_title
-- local title = get_current_working_dir(tab)
--
-- if custom_title and #custom_title > 0 then
-- title = custom_title
-- end
--
-- return string.format(' %s•%s ', index, title)
-- end)
--
-- Set window title to the current working directory
wezterm.on('format-window-title', function(tab, pane, tabs, panes, config)
return get_current_working_dir(tab)
end)
-- Set the correct window size at the startup
wezterm.on('gui-startup', function(cmd)
local active_screen = wezterm.gui.screens()["active"]
local _, _, window = wezterm.mux.spawn_window(cmd or {})
-- MacBook Pro 14" 2023
if active_screen.width <= 3024 then
-- Laptop: open full screen
window:gui_window():maximize()
else
-- Desktop: place on the right half of the screen
window:gui_window():set_position(active_screen.width / 2, 0)
window:gui_window():set_inner_size(active_screen.width / 2, active_screen.height)
end
end)
-- Custom title and icon based on: https://github.com/protiumx/.dotfiles/blob/854d4b159a0a0512dc24cbc840af467ac84085f8/stow/wezterm/.config/wezterm/wezterm.lua#L291-L319
local process_icons = {
["bash"] = wezterm.nerdfonts.cod_terminal_bash,
["btm"] = wezterm.nerdfonts.mdi_chart_donut_variant,
["cargo"] = wezterm.nerdfonts.dev_rust,
["curl"] = wezterm.nerdfonts.mdi_flattr,
["docker"] = wezterm.nerdfonts.linux_docker,
["docker-compose"] = wezterm.nerdfonts.linux_docker,
["gh"] = wezterm.nerdfonts.dev_github_badge,
["git"] = wezterm.nerdfonts.fa_git,
["go"] = wezterm.nerdfonts.seti_go,
["htop"] = wezterm.nerdfonts.mdi_chart_donut_variant,
["kubectl"] = wezterm.nerdfonts.linux_docker,
["kuberlr"] = wezterm.nerdfonts.linux_docker,
["lazydocker"] = wezterm.nerdfonts.linux_docker,
["lazygit"] = wezterm.nerdfonts.oct_git_compare,
["lua"] = wezterm.nerdfonts.seti_lua,
["make"] = wezterm.nerdfonts.seti_makefile,
["node"] = wezterm.nerdfonts.mdi_hexagon,
["nvim"] = wezterm.nerdfonts.custom_vim,
["psql"] = "",
["ruby"] = wezterm.nerdfonts.cod_ruby,
["stern"] = wezterm.nerdfonts.linux_docker,
["sudo"] = wezterm.nerdfonts.fa_hashtag,
["usql"] = "",
["vim"] = wezterm.nerdfonts.dev_vim,
["wget"] = wezterm.nerdfonts.mdi_arrow_down_box,
["zsh"] = wezterm.nerdfonts.dev_terminal,
}
-- Return the Tab's current working directory
local function get_cwd(tab)
return tab.active_pane.current_working_dir.file_path or ""
end
-- Remove all path components and return only the last value
local function remove_abs_path(path) return path:gsub("(.*[/\\])(.*)", "%2") end
-- Return the pretty path of the tab's current working directory
local function get_display_cwd(tab)
local current_dir = get_cwd(tab)
local HOME_DIR = string.format("file://%s", os.getenv("HOME"))
return current_dir == HOME_DIR and "~/" or remove_abs_path(current_dir)
end
-- Return the concise name or icon of the running process for display
local function get_process(tab)
if not tab.active_pane or tab.active_pane.foreground_process_name == "" then return "[?]" end
local process_name = remove_abs_path(tab.active_pane.foreground_process_name)
if process_name:find("kubectl") then process_name = "kubectl" end
return process_icons[process_name] or string.format("[%s]", process_name)
end
-- Pretty format the tab title
local function format_title(tab)
local cwd = get_display_cwd(tab)
local process = get_process(tab)
local active_title = tab.active_pane.title
if active_title:find("- NVIM") then active_title = active_title:gsub("^([^ ]+) .*", "%1") end
local description = (not active_title or active_title == cwd) and "~" or active_title
return string.format(" %s %s/ %s ", process, cwd, description)
end
-- Determine if a tab has unseen output since last visited
local function has_unseen_output(tab)
if not tab.is_active then
for _, pane in ipairs(tab.panes) do
if pane.has_unseen_output then return true end
end
end
return false
end
-- Returns manually set title (from `tab:set_title()` or `wezterm cli set-tab-title`) or creates a new one
local function get_tab_title(tab)
local title = tab.tab_title
if title and #title > 0 then return title end
return format_title(tab)
end
-- Convert arbitrary strings to a unique hex color value
-- Based on: https://stackoverflow.com/a/3426956/3219667
local function string_to_color(str)
-- Convert the string to a unique integer
local hash = 0
for i = 1, #str do
hash = string.byte(str, i) + ((hash << 5) - hash)
end
-- Convert the integer to a unique color
local c = string.format("%06X", hash & 0x00FFFFFF)
return "#" .. (string.rep("0", 6 - #c) .. c):upper()
end
local function select_contrasting_fg_color(hex_color)
-- Note: this could use `return color:complement_ryb()` instead if you prefer or other builtins!
local color = wezterm.color.parse(hex_color)
---@diagnostic disable-next-line: unused-local
local lightness, _a, _b, _alpha = color:laba()
if lightness > 55 then
return "#000000" -- Black has higher contrast with colors perceived to be "bright"
end
return "#FFFFFF" -- White has higher contrast
end
-- -- Inline tests
-- local testColor = string_to_color("/Users/kyleking/Developer/ProjectA")
-- assert(testColor == "#EBD168", "Unexpected color value for test hash (" .. testColor .. ")")
-- assert(select_contrasting_fg_color("#494CED") == "#FFFFFF", "Expected higher contrast with white")
-- assert(select_contrasting_fg_color("#128b26") == "#FFFFFF", "Expected higher contrast with white")
-- assert(select_contrasting_fg_color("#58f5a6") == "#000000", "Expected higher contrast with black")
-- assert(select_contrasting_fg_color("#EBD168") == "#000000", "Expected higher contrast with black")
--
-- On format tab title events, override the default handling to return a custom title
-- Docs: https://wezfurlong.org/wezterm/config/lua/window-events/format-tab-title.html
---@diagnostic disable-next-line: unused-local
wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
local title = get_tab_title(tab)
local color = string_to_color(get_cwd(tab))
if tab.is_active then
return {
{ Attribute = { Intensity = "Bold" } },
{ Background = { Color = color } },
{ Foreground = { Color = select_contrasting_fg_color(color) } },
{ Text = title },
}
end
if has_unseen_output(tab) then
return {
{ Foreground = { Color = "#EBD168" } },
{ Text = title },
}
end
return title
end)
--- 8< -- 8< ---
return config