1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

Add permute_any_mods and permute_any_or_no_mods functions

refs: #288
This commit is contained in:
Wez Furlong 2020-10-10 15:11:21 -07:00
parent 46651caaf8
commit ab6cda81aa
4 changed files with 445 additions and 0 deletions

View File

@ -162,6 +162,10 @@ where
let s = String::deserialize(deserializer)?;
let mut mods = Modifiers::NONE;
for ele in s.split('|') {
// Allow for whitespace; debug printing Modifiers includes spaces
// around the `|` so it is desirable to be able to reverse that
// encoding here.
let ele = ele.trim();
if ele == "SHIFT" {
mods |= Modifiers::SHIFT;
} else if ele == "ALT" || ele == "OPT" || ele == "META" {

View File

@ -2,10 +2,12 @@ use crate::{FontAttributes, TextStyle};
use anyhow::anyhow;
use bstr::BString;
pub use luahelper::*;
use mlua::ToLua;
use mlua::{Lua, Table, Value};
use serde::*;
use smol::prelude::*;
use std::path::Path;
use termwiz::input::Modifiers;
/// Set up a lua context for executing some code.
/// The path to the directory containing the configuration is
@ -95,6 +97,11 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
)?;
wezterm_mod.set("hostname", lua.create_function(hostname)?)?;
wezterm_mod.set("action", lua.create_function(action)?)?;
wezterm_mod.set("permute_any_mods", lua.create_function(permute_any_mods)?)?;
wezterm_mod.set(
"permute_any_or_no_mods",
lua.create_function(permute_any_or_no_mods)?,
)?;
wezterm_mod.set("read_dir", lua.create_async_function(read_dir)?)?;
wezterm_mod.set("glob", lua.create_async_function(glob)?)?;
@ -406,6 +413,49 @@ async fn run_child_process<'lua>(
))
}
fn permute_any_mods<'lua>(
lua: &'lua Lua,
item: mlua::Table,
) -> mlua::Result<Vec<mlua::Value<'lua>>> {
permute_mods(lua, item, false)
}
fn permute_any_or_no_mods<'lua>(
lua: &'lua Lua,
item: mlua::Table,
) -> mlua::Result<Vec<mlua::Value<'lua>>> {
permute_mods(lua, item, true)
}
fn permute_mods<'lua>(
lua: &'lua Lua,
item: mlua::Table,
allow_none: bool,
) -> mlua::Result<Vec<mlua::Value<'lua>>> {
let mut result = vec![];
for ctrl in &[Modifiers::NONE, Modifiers::CTRL] {
for shift in &[Modifiers::NONE, Modifiers::SHIFT] {
for alt in &[Modifiers::NONE, Modifiers::ALT] {
for sup in &[Modifiers::NONE, Modifiers::SUPER] {
let flags = *ctrl | *shift | *alt | *sup;
if flags == Modifiers::NONE && !allow_none {
continue;
}
let new_item = lua.create_table()?;
for pair in item.clone().pairs::<mlua::Value, mlua::Value>() {
let (k, v) = pair?;
new_item.set(k, v)?;
}
new_item.set("mods", format!("{:?}", flags))?;
result.push(new_item.to_lua(lua)?);
}
}
}
}
Ok(result)
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -0,0 +1,191 @@
# `wezterm.permute_any_mods(table)`
*Since: nightly builds only*
This function is intended to help with generating key or mouse binding
entries that should apply regardless of the combination of modifier keys
pressed.
For each combination of modifiers `CTRL`, `ALT`, `SHIFT` and `SUPER`,
the supplied table value is copied and has `mods = <value>` set into
the copy.
An entry for `NONE` is *NOT* generated (this is the only
difference between `permute_any_mods` and `permute_any_or_no_mods`).
An array holding all of those combinations is returned.
The array can be unpacked into a lua table initializer and used like this:
```lua
local wezterm = require 'wezterm';
return {
mouse_bindings = {
table.unpack(wezterm.permute_any_mods({
event={Down={streak=1, button="Middle"}},
action="PastePrimarySelection"
}))
}
}
```
This is equivalent to writing this out, but is much less verbose:
```lua
return {
mouse_bindings = {
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | CTRL | SUPER",
},
}
}
```

View File

@ -0,0 +1,200 @@
# `wezterm.permute_any_or_no_mods(table)`
*Since: nightly builds only*
This function is intended to help with generating key or mouse binding
entries that should apply regardless of the combination of modifier keys
pressed.
For each combination of modifiers `CTRL`, `ALT`, `SHIFT` and `SUPER`,
the supplied table value is copied and has `mods = <value>` set into
the copy.
In addition, an entry for `NONE` *is* generated (this is the only
difference between `permute_any_mods` and `permute_any_or_no_mods`).
An array holding all of those combinations is returned.
The array can be unpacked into a lua table initializer and used like this:
```lua
local wezterm = require 'wezterm';
return {
mouse_bindings = {
table.unpack(wezterm.permute_any_mods({
event={Down={streak=1, button="Middle"}},
action="PastePrimarySelection"
}))
}
}
```
This is equivalent to writing this out, but is much less verbose:
```lua
return {
mouse_bindings = {
{
action= "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "NONE",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "ALT | CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | CTRL | SUPER",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | CTRL",
},
{
action = "PastePrimarySelection",
event = {
Down = {
button = "Middle",
streak = 1,
},
},
mods = "SHIFT | ALT | CTRL | SUPER",
},
}
}
```