From 9eea87839214fbe2fc0004a97194a0a395100505 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 22 Mar 2023 19:20:01 +0800 Subject: [PATCH] wezterm: modularize configuration --- wezterm/config/appearance.lua | 34 ++++++ wezterm/config/base.lua | 39 +++++++ wezterm/config/events.lua | 12 +++ wezterm/config/keys.lua | 189 ++++++++++++++++++++++++++++++++ wezterm/wezterm.lua | 195 ++-------------------------------- 5 files changed, 282 insertions(+), 187 deletions(-) create mode 100644 wezterm/config/appearance.lua create mode 100644 wezterm/config/base.lua create mode 100644 wezterm/config/events.lua create mode 100644 wezterm/config/keys.lua diff --git a/wezterm/config/appearance.lua b/wezterm/config/appearance.lua new file mode 100644 index 0000000..2fcb578 --- /dev/null +++ b/wezterm/config/appearance.lua @@ -0,0 +1,34 @@ +-- This module should set the appearance-related options. +local module = {} + +local wezterm = require("wezterm") + +local light_theme = "Albino bark on a tree" +local dark_theme = "Bark on a tree" + +local function scheme_for_appearance() + local scheme = wezterm.gui.get_appearance() + if scheme == "Dark" then + return dark_theme + else + return light_theme + end +end + +local albino_bark_on_a_tree = + wezterm.color.load_base16_scheme(os.getenv("HOME") .. "/library/dotfiles/base16/albino-bark-on-a-tree.yaml") +local bark_on_a_tree = + wezterm.color.load_base16_scheme(os.getenv("HOME") .. "/library/dotfiles/base16/bark-on-a-tree.yaml") + +--- Apply the configuration with the given table. +-- @param config: the table containing Wezterm configuration. +function module.apply_to_config(config) + config.color_schemes = {} + config.color_schemes[light_theme] = albino_bark_on_a_tree + config.color_schemes[dark_theme] = bark_on_a_tree + config.color_scheme = scheme_for_appearance() + + return config +end + +return module diff --git a/wezterm/config/base.lua b/wezterm/config/base.lua new file mode 100644 index 0000000..99e9a3b --- /dev/null +++ b/wezterm/config/base.lua @@ -0,0 +1,39 @@ +-- A local jumpstart for creating my base configuration. +local module = {} + +local wezterm = require("wezterm") + +function module.apply_to_config(config) + config.default_prog = { "bash" } + + -- I'm liking the workflow I have with Kitty so no thanks for the default + -- shortcuts. So this is what it feels like to be obnoxiously stubborn. + config.disable_default_key_bindings = true + + -- Don't tease me with the upcoming releases, man. + config.check_for_updates = false + + -- Enable some things for Wayland. + config.enable_wayland = true + config.force_reverse_video_cursor = true + + -- Desaturate any inactive panes. + config.inactive_pane_hsb = { + saturation = 0.5, + brightness = 0.5, + } + + -- Thankfully, wezterm can detect fontconfig aliases. + config.font = wezterm.font_with_fallback({ + "monospace", + "Noto Color Emoji", + }) + + config.color_scheme = "Batman" + return config +end + +-- The keymod to be used for the entire configuration. +module.keymod = "CTRL|SHIFT" + +return module diff --git a/wezterm/config/events.lua b/wezterm/config/events.lua new file mode 100644 index 0000000..560e32b --- /dev/null +++ b/wezterm/config/events.lua @@ -0,0 +1,12 @@ +local module = {} +local wezterm = require("wezterm") + +wezterm.on("update-right-status", function(window, pane) + local key = window:active_key_table() + if key then + key = "TABLE: " .. key + end + window:set_right_status(key or "") +end) + +return module diff --git a/wezterm/config/keys.lua b/wezterm/config/keys.lua new file mode 100644 index 0000000..1f483d5 --- /dev/null +++ b/wezterm/config/keys.lua @@ -0,0 +1,189 @@ +local wezterm = require("wezterm") +local base = require("config/base") +local events = require("config/events") + +local keymod = base.keymod +local module = {} + +function module.apply_to_config(config) + -- I'm very used to setting as the leader so I'm continuing the tradition. + config.leader = { key = "Space", mods = keymod, timeout_milliseconds = 1000 } + + -- Quick select-related options. Quite similar to Kitty hints which is + -- nice. + config.quick_select_patterns = { + "[0-9a-f]{7,40}", -- SHA1 hashes, usually used for Git. + "[0-9a-f]{7,64}", -- SHA256 hashes, used often for getting hashes for Guix packaging. + "sha256-.{44,128}", -- SHA256 hashes in Base64, used often in getting hashes for Nix packaging. + } + + config.mouse_bindings = { + { + event = { Down = { streak = 3, button = "Left" } }, + action = { SelectTextAtMouseCursor = "SemanticZone" }, + mods = keymod, + }, + } + + -- It also makes use of key tables which is defined after. + config.keys = { + -- Clipboard + { key = "c", mods = keymod, action = wezterm.action({ CopyTo = "Clipboard" }) }, + { key = "v", mods = keymod, action = wezterm.action({ PasteFrom = "Clipboard" }) }, + + -- Font resize. + { key = "+", mods = keymod, action = "IncreaseFontSize" }, + { key = "_", mods = keymod, action = "DecreaseFontSize" }, + { key = ")", mods = keymod, action = "ResetFontSize" }, + + -- Scrollback + { key = "j", mods = keymod, action = wezterm.action({ ScrollByPage = 1 }) }, + { key = "k", mods = keymod, action = wezterm.action({ ScrollByPage = -1 }) }, + { key = "j", mods = "CTRL|ALT", action = wezterm.action({ ScrollToPrompt = 1 }) }, + { key = "k", mods = "CTRL|ALT", action = wezterm.action({ ScrollToPrompt = -1 }) }, + + -- Pane navigation. + { + key = "p", + mods = "LEADER", + action = wezterm.action.ActivateKeyTable({ + name = "pane_navigation", + timeout_milliseconds = 1000, + replace_current = true, + one_shot = true, + }), + }, + { + key = "r", + mods = "LEADER", + action = wezterm.action({ + ActivateKeyTable = { name = "resize_pane", replace_current = true, one_shot = false }, + }), + }, + { key = "h", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Left" }) }, + { key = "l", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Right" }) }, + { key = "LeftArrow", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Left" }) }, + { key = "DownArrow", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Down" }) }, + { key = "UpArrow", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Up" }) }, + { key = "RightArrow", mods = keymod, action = wezterm.action({ ActivatePaneDirection = "Right" }) }, + + -- More pane-related niceties. + { key = "f", mods = "LEADER", action = "TogglePaneZoomState" }, + { key = "f", mods = keymod, action = "TogglePaneZoomState" }, + { key = "n", mods = "LEADER", action = wezterm.action({ SplitHorizontal = { domain = "CurrentPaneDomain" } }) }, + { key = "n", mods = keymod, action = wezterm.action({ SplitHorizontal = { domain = "CurrentPaneDomain" } }) }, + { key = "d", mods = keymod, action = wezterm.action({ CloseCurrentPane = { confirm = false } }) }, + + -- Tab navigation + { + key = "t", + mods = "LEADER", + action = wezterm.action({ + ActivateKeyTable = { + name = "tab_navigation", + timeout_milliseconds = 1000, + replace_current = true, + one_shot = true, + }, + }), + }, + + -- Hints and quick selections + { + key = "h", + mods = "LEADER", + action = wezterm.action({ + ActivateKeyTable = { name = "hints", timeout_milliseconds = 1000, replace_current = true, one_shot = true }, + }), + }, + + { key = "r", mods = keymod, action = "ReloadConfiguration" }, + { key = "t", mods = keymod, action = wezterm.action.ShowDebugOverlay }, + + -- Selection + { key = "Space", mods = "LEADER", action = "QuickSelect" }, + { key = "a", mods = keymod, action = "QuickSelect" }, + { key = "s", mods = keymod, action = wezterm.action({ Search = { CaseSensitiveString = "" } }) }, + } + + config.key_tables = { + hints = { + { key = "g", action = wezterm.action({ Search = { Regex = "[0-9a-f]{6,}" } }) }, + { + key = "h", + action = wezterm.action({ + QuickSelectArgs = { + patterns = { + "[0-9a-f]{7,40}", -- SHA1 hashes, usually used for Git. + "[0-9a-f]{7,64}", -- SHA256 hashes, used often for getting hashes for Guix packaging. + "sha256-[[:alpha:][:digit:]-=+/?]{44}", -- SHA256 hashes in Base64, used often in getting hashes for Nix packaging. + "[[:alpha:][:digit:]-=+/?]{44,64}", + }, + }, + }), + }, + + -- Basically the equivalent of `kitty hints word`. + { + key = "w", + action = wezterm.action({ + QuickSelectArgs = { + patterns = { + "\\S+", + }, + }, + }), + }, + + -- The equivalent to `kitty hints line`. + { + key = "l", + action = wezterm.action({ + QuickSelectArgs = { + patterns = { + ".+", + }, + }, + }), + }, + + { key = "p", action = wezterm.action({ Search = { Regex = "legacyPackages[[:alpha:][:digit:]]+" } }) }, + + { key = "Space", action = "QuickSelect" }, + { key = "s", action = "QuickSelect" }, + { key = "f", action = wezterm.action({ Search = { CaseSensitiveString = "" } }) }, + }, + + pane_navigation = { + { key = "d", action = wezterm.action({ CloseCurrentPane = { confirm = false } }) }, + { key = "h", action = wezterm.action({ ActivatePaneDirection = "Left" }) }, + { key = "j", action = wezterm.action({ ActivatePaneDirection = "Down" }) }, + { key = "k", action = wezterm.action({ ActivatePaneDirection = "Up" }) }, + { key = "l", action = wezterm.action({ ActivatePaneDirection = "Right" }) }, + { key = "n", action = wezterm.action({ SplitHorizontal = { domain = "CurrentPaneDomain" } }) }, + }, + + tab_navigation = { + { key = "d", action = wezterm.action({ CloseCurrentTab = { confirm = false } }) }, + { key = "h", action = wezterm.action({ ActivateTabRelative = -1 }) }, + { key = "j", action = wezterm.action({ ActivateTab = -1 }) }, + { key = "k", action = wezterm.action({ ActivateTab = 0 }) }, + { key = "l", action = wezterm.action({ ActivateTabRelative = 1 }) }, + { key = "n", action = wezterm.action({ SpawnTab = "CurrentPaneDomain" }) }, + }, + + resize_pane = { + { key = "h", action = wezterm.action({ AdjustPaneSize = { "Left", 1 } }) }, + { key = "j", action = wezterm.action({ AdjustPaneSize = { "Down", 1 } }) }, + { key = "k", action = wezterm.action({ AdjustPaneSize = { "Up", 1 } }) }, + { key = "l", action = wezterm.action({ AdjustPaneSize = { "Right", 1 } }) }, + { key = "q", action = "PopKeyTable" }, + { key = "Escape", action = "PopKeyTable" }, + { key = "Enter", action = "PopKeyTable" }, + }, + } + + return config +end + +return module diff --git a/wezterm/wezterm.lua b/wezterm/wezterm.lua index 484abea..4db50fb 100644 --- a/wezterm/wezterm.lua +++ b/wezterm/wezterm.lua @@ -1,190 +1,11 @@ -local wezterm = require("wezterm") -local io = require("io") -local os = require("os") +local base = require("config/base") +local keys = require("config/keys") +local appearance = require("config/appearance") -local keymod = "CTRL|SHIFT" +local config = {} -local light_theme = "Material" -local dark_theme = "MaterialDark" +base.apply_to_config(config) +keys.apply_to_config(config) +appearance.apply_to_config(config) -wezterm.on("toggle-dark-mode", function (window, pane) - local overrides = window:get_config_overrides() or {} - if (overrides.color_scheme == light_theme) then - overrides.color_scheme = dark_theme - else - overrides.color_scheme = light_theme - end - window:set_config_overrides(overrides) -end) - -wezterm.on("read-scrollback", function (window, pane) - local scrollback = pane:get_lines_as_text() - window:perform_action(wezterm.action{ - SpawnCommandInNewWindow = { - args = { - "sh", "-c", - "echo", scrollback, "|", "nvim", name }}}, pane) -end) - -wezterm.on("update-right-status", function (window, pane) - local key = window:active_key_table() - if key then - key = "TABLE: " .. key - end - window:set_right_status(key or "") -end) - -return { - -- Thankfully, wezterm can detect fontconfig aliases. - font = wezterm.font_with_fallback({ - "monospace", - "Noto Color Emoji", - }), - color_scheme = dark_theme, - - default_prog = {"bash"}, - - -- I'm liking the workflow I have with Kitty so no thanks for the default - -- shortcuts. So this is what it feels like to be obnoxiously stubborn. - disable_default_key_bindings = true, - - -- Don't tease me with the upcoming releases, man. - check_for_updates = false, - - -- I'm very used to setting as the leader so I'm continuing the tradition. - leader = { key = "Space", mods = keymod, timeout_milliseconds = 1000 }, - - enable_wayland = true, - force_reverse_video_cursor = true, - - inactive_pane_hsb = { - saturation = 0.5, - brightness = 0.5, - }; - - -- Quick select-related options. Quite similar to Kitty hints which is - -- nice. - quick_select_patterns = { - "[0-9a-f]{7,40}", -- SHA1 hashes, usually used for Git. - "[0-9a-f]{7,64}", -- SHA256 hashes, used often for getting hashes for Guix packaging. - "sha256-.{44,128}", -- SHA256 hashes in Base64, used often in getting hashes for Nix packaging. - }, - - mouse_bindings = { - { event = { Down = { streak = 3, button = "Left" }}, - action = { SelectTextAtMouseCursor = "SemanticZone" }, - mods = keymod }, - }, - - -- It also makes use of key tables which is defined after. - keys = { - -- Clipboard - {key = "c", mods = keymod, action = wezterm.action { CopyTo = "Clipboard" }}, - {key = "v", mods = keymod, action = wezterm.action { PasteFrom = "Clipboard" }}, - {key = "w", mods = keymod, action = wezterm.action { EmitEvent = "toggle-dark-mode" }}, - {key = "e", mods = keymod, action = wezterm.action { EmitEvent = "read-scrollback" }}, - - -- Font resize. - {key = "+", mods = keymod, action = "IncreaseFontSize"}, - {key = "_", mods = keymod, action = "DecreaseFontSize"}, - {key = ")", mods = keymod, action = "ResetFontSize"}, - - -- Scrollback - {key = "j", mods = keymod, action = wezterm.action { ScrollByPage = 1 }}, - {key = "k", mods = keymod, action = wezterm.action { ScrollByPage = -1 }}, - {key = "j", mods = "CTRL|ALT", action = wezterm.action { ScrollToPrompt = 1 }}, - {key = "k", mods = "CTRL|ALT", action = wezterm.action { ScrollToPrompt = -1 }}, - - -- Pane navigation. - {key = "p", mods = "LEADER", action = wezterm.action.ActivateKeyTable { name = "pane_navigation", timeout_milliseconds = 1000, replace_current = true, one_shot = true, }}, - {key = "r", mods = "LEADER", action = wezterm.action { - ActivateKeyTable = { name = "resize_pane", replace_current = true, one_shot = false }}}, - {key = "h", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Left" }}, - {key = "l", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Right" }}, - {key = "LeftArrow", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Left" }}, - {key = "DownArrow", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Down" }}, - {key = "UpArrow", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Up" }}, - {key = "RightArrow", mods = keymod, action = wezterm.action { ActivatePaneDirection = "Right" }}, - - -- More pane-related niceties. - {key = "f", mods = "LEADER", action = "TogglePaneZoomState"}, - {key = "f", mods = keymod, action = "TogglePaneZoomState"}, - {key = "n", mods = "LEADER", action = wezterm.action { SplitHorizontal = { domain = "CurrentPaneDomain" }}}, - {key = "n", mods = keymod, action = wezterm.action { SplitHorizontal = { domain = "CurrentPaneDomain" }}}, - {key = "d", mods = keymod, action = wezterm.action { CloseCurrentPane = { confirm = false }}}, - - -- Tab navigation - {key = "t", mods = "LEADER", action = wezterm.action { - ActivateKeyTable = { name = "tab_navigation", timeout_milliseconds = 1000, replace_current = true, one_shot = true, }}}, - - -- Hints and quick selections - {key = "h", mods = "LEADER", action = wezterm.action { - ActivateKeyTable = { name = "hints", timeout_milliseconds = 1000, replace_current = true, one_shot = true }}}, - - {key = "r", mods = keymod, action = "ReloadConfiguration" }, - - -- Selection - {key = "Space", mods = "LEADER", action = "QuickSelect" }, - {key = "a", mods = keymod, action = "QuickSelect" }, - {key = "s", mods = keymod, action = wezterm.action { Search = { CaseSensitiveString = "" }}}, - }, - - key_tables = { - hints = { - {key = "g", action = wezterm.action { Search = { Regex = "[0-9a-f]{6,}" }}}, - {key = "h", action = wezterm.action { QuickSelectArgs = { - patterns = { - "[0-9a-f]{7,40}", -- SHA1 hashes, usually used for Git. - "[0-9a-f]{7,64}", -- SHA256 hashes, used often for getting hashes for Guix packaging. - "sha256-[[:alpha:][:digit:]-=+/?]{44}", -- SHA256 hashes in Base64, used often in getting hashes for Nix packaging. - "[[:alpha:][:digit:]-=+/?]{44,64}" - }}}}, - - -- Basically the equivalent of `kitty hints word`. - {key = "w", action = wezterm.action { QuickSelectArgs = { - patterns = { - "\\S+" - }}}}, - - -- The equivalent to `kitty hints line`. - {key = "l", action = wezterm.action { QuickSelectArgs = { - patterns = { - ".+" - }}}}, - - {key = "p", action = wezterm.action { Search = { Regex = "legacyPackages[[:alpha:][:digit:]]+" }}}, - - {key = "Space", action = "QuickSelect" }, - {key = "s", action = "QuickSelect" }, - {key = "f", action = wezterm.action { Search = { CaseSensitiveString = "" }}}, - }, - - pane_navigation = { - {key = "d", action = wezterm.action { CloseCurrentPane = { confirm = false }}}, - {key = "h", action = wezterm.action { ActivatePaneDirection = "Left" }}, - {key = "j", action = wezterm.action { ActivatePaneDirection = "Down" }}, - {key = "k", action = wezterm.action { ActivatePaneDirection = "Up" }}, - {key = "l", action = wezterm.action { ActivatePaneDirection = "Right" }}, - {key = "n", action = wezterm.action { SplitHorizontal = { domain = "CurrentPaneDomain" }}}, - }, - - tab_navigation = { - {key = "d", action = wezterm.action { CloseCurrentTab = { confirm = false }}}, - {key = "h", action = wezterm.action { ActivateTabRelative = -1 }}, - {key = "j", action = wezterm.action { ActivateTab = -1 }}, - {key = "k", action = wezterm.action { ActivateTab = 0 }}, - {key = "l", action = wezterm.action { ActivateTabRelative = 1 }}, - {key = "n", action = wezterm.action { SpawnTab = "CurrentPaneDomain" }}, - }, - - resize_pane = { - {key = "h", action = wezterm.action { AdjustPaneSize = { "Left", 1 }}}, - {key = "j", action = wezterm.action { AdjustPaneSize = { "Down", 1 }}}, - {key = "k", action = wezterm.action { AdjustPaneSize = { "Up", 1 }}}, - {key = "l", action = wezterm.action { AdjustPaneSize = { "Right", 1 }}}, - {key = "q", action = "PopKeyTable" }, - {key = "Escape", action = "PopKeyTable" }, - {key = "Enter", action = "PopKeyTable" }, - }, - }, -} +return config