Dynamically select pre-defined configuration snippets #5435
pkazmier
started this conversation in
Show and tell
Replies: 1 comment 2 replies
-
Thank you for sharing! This is exactly the configuration I wanted to set up a “workspace/project navigator”. I haven’t learned lua that much yet but, I think the only difference would be using a file to store them so I can add / remove projects. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
You can find the precursor of this discussion in #5369.
Have you ever wanted to easily tweak one or two settings of your wezterm config dynamically? Maybe you want to quickly switch fonts, font size, leading (line spacing), or color schemes? If you have, you might be interested in my config-selector, which allows you to define a list of items and a pre-defined config override to execute upon selection. For example:
Screen.Recording.2024-05-17.at.6.53.42.AM.mov
For example, in my wezterm config, I define several selectors:
The
subdir
option passed toselector.new
is a subdirectory within your wezterm configuration directory that should contain one or more lua modules. Each module represents one or more items to appear in the menu and the action to take when selected. The name of the module is irrelevant as it is not used. It must be a valid lua module name of course. For example, here are the contents of my wezterm directory:The lua module in a subdirectory must export two functions:
init
andactivate
. Theinit
function will receive no arguments. It must return one of the following:"item name"
: a string that is used as the name of the item in the selector{ label = "item name" }
: a table with alabel
key that is used as the name of the item in the selector.{ label = "item name", value = 10 }
: a table that also includes avalue
key that will be passed to theactivate
function when the item is selected.{ "item1", "item2" }
or{{ label = "item1", value = 10 }, { label = "item2", value = 20 }}
: a list of any of the above if you want to define more than one item per lua module.The
activate
function will receive three arguments:config
: a wezterm config overrides table that you will use to override configuration items.name
: the name of the item that was selected as defined by theinit
function. This is useful to distinguish between items if yourinit
returned more than one item.value
: the value of the item that was selected as defined by theinit
function ornil
if not defined. This is useful to distinguish between items if yourinit
returned more than one item.For example, let's look at
menlo.lua
in myfonts
directory. Theinit
function returns a single string. Because this lua module only defines one item, I don't use the additional arguments (name
andvalue
) passed toactivate
because I don't have to distinguish between items selected—there is only one defined in the module!Let's look at my
colorschemes
directory, which has a mix of lua modules that define single items (all theminihues-*
files look similar to the example above), but it also includes one module calledbuiltin-colorschemes.lua
that defines several items to be added to the menu. In this file, you can see that theinit
function returns a table of items. Each table includes alabel
, which is used as the item name in the menu, but also avalue
that is used inactivate
if the item is selected. In this case, thevalue
is the official name of the scheme, but I want the display name to be prettier, so I define a separatelabel
andvalue
.If I did not care about having a prettier item name, then I could simplify the above to the following by just using the official scheme name as the menu items. And if selected, use the 2nd argument to
activate
, which will be the name of the scheme:Finally, as a last example, let's look at my
sizes
directory where I have a single lua module defined in a file calledsizes.lua
(again, name of the file is irrelevant). In this case, I didn't want to define a single lua module for each possible font size. I could, but it would be tedious, so I instead return a table of items, each with alabel
andvalue
key. This allows me to populate the menu with 8pt to 30pt items in a single file. Because I want a prettier item name, I append "pt" to the label. And since we've defined more than one item in this module, I distinguish which one was selected in myactivate
function via thevalue
argument.In the above example, you might have noticed that I also included a single string "Default" in the
sizes
table. This is allowed as mentioned above when I talk about the possible return values frominit
. Because "Default" is not a table with a value associated with it, when it is selected by a user, thevalue
argument inactivate
isnil
. This is convenient as we use that to remove any existing overrides set inconfig.font_size
. It allows us to revert back to whatever your default was in your main configuration.I hope you find this useful. It's been a game changer for me!
Beta Was this translation helpful? Give feedback.
All reactions