Files
LLeMbas-TTY/config/src/keyassignment.rs
T

425 lines
11 KiB
Rust

use crate::de_notnan;
use crate::keys::KeyNoAction;
use luahelper::impl_lua_conversion;
use ordered_float::NotNan;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::path::PathBuf;
use wezterm_input_types::{KeyCode, Modifiers};
use wezterm_term::input::MouseButton;
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LauncherActionArgs {
pub flags: LauncherFlags,
pub title: Option<String>,
}
bitflags::bitflags! {
#[derive(Default, Deserialize, Serialize)]
#[serde(try_from="String", into="String")]
pub struct LauncherFlags :u32 {
const ZERO = 0;
const FUZZY = 1;
const TABS = 2;
const LAUNCH_MENU_ITEMS = 4;
const DOMAINS = 8;
const KEY_ASSIGNMENTS = 16;
const WORKSPACES = 32;
const COMMANDS = 64;
}
}
impl Into<String> for LauncherFlags {
fn into(self) -> String {
self.to_string()
}
}
impl ToString for LauncherFlags {
fn to_string(&self) -> String {
let mut s = vec![];
if self.contains(Self::FUZZY) {
s.push("FUZZY");
}
if self.contains(Self::TABS) {
s.push("TABS");
}
if self.contains(Self::LAUNCH_MENU_ITEMS) {
s.push("LAUNCH_MENU_ITEMS");
}
if self.contains(Self::DOMAINS) {
s.push("DOMAINS");
}
if self.contains(Self::KEY_ASSIGNMENTS) {
s.push("KEY_ASSIGNMENTS");
}
if self.contains(Self::WORKSPACES) {
s.push("WORKSPACES");
}
if self.contains(Self::COMMANDS) {
s.push("COMMANDS");
}
s.join("|")
}
}
impl TryFrom<String> for LauncherFlags {
type Error = String;
fn try_from(s: String) -> Result<Self, String> {
let mut flags = LauncherFlags::default();
for ele in s.split('|') {
let ele = ele.trim();
match ele {
"FUZZY" => flags |= Self::FUZZY,
"TABS" => flags |= Self::TABS,
"LAUNCH_MENU_ITEMS" => flags |= Self::LAUNCH_MENU_ITEMS,
"DOMAINS" => flags |= Self::DOMAINS,
"KEY_ASSIGNMENTS" => flags |= Self::KEY_ASSIGNMENTS,
"WORKSPACES" => flags |= Self::WORKSPACES,
"COMMANDS" => flags |= Self::COMMANDS,
_ => {
return Err(format!("invalid LauncherFlags `{}` in `{}`", ele, s));
}
}
}
Ok(flags)
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
pub enum SelectionMode {
Cell,
Word,
Line,
SemanticZone,
Block,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum Pattern {
CaseSensitiveString(String),
CaseInSensitiveString(String),
Regex(String),
}
impl Default for Pattern {
fn default() -> Self {
Self::CaseSensitiveString("".to_string())
}
}
impl std::ops::Deref for Pattern {
type Target = String;
fn deref(&self) -> &String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
impl std::ops::DerefMut for Pattern {
fn deref_mut(&mut self) -> &mut String {
match self {
Pattern::CaseSensitiveString(s) => s,
Pattern::CaseInSensitiveString(s) => s,
Pattern::Regex(s) => s,
}
}
}
/// A mouse event that can trigger an action
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub enum MouseEventTrigger {
/// Mouse button is pressed. streak is how many times in a row
/// it was pressed.
Down { streak: usize, button: MouseButton },
/// Mouse button is held down while the cursor is moving. streak is how many times in a row
/// it was pressed, with the last of those being held to form the drag.
Drag { streak: usize, button: MouseButton },
/// Mouse button is being released. streak is how many times
/// in a row it was pressed and released.
Up { streak: usize, button: MouseButton },
}
/// When spawning a tab, specify which domain should be used to
/// host/spawn that tab.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum SpawnTabDomain {
/// Use the default domain
DefaultDomain,
/// Use the domain from the current tab in the associated window
CurrentPaneDomain,
/// Use a specific domain by name
DomainName(String),
/// Use a specific domain by id
DomainId(usize),
}
impl Default for SpawnTabDomain {
fn default() -> Self {
Self::CurrentPaneDomain
}
}
#[derive(Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct SpawnCommand {
/// Optional descriptive label
pub label: Option<String>,
/// The command line to use.
/// If omitted, the default command associated with the
/// domain will be used instead, which is typically the
/// shell for the user.
pub args: Option<Vec<String>>,
/// Specifies the current working directory for the command.
/// If omitted, a default will be used; typically that will
/// be the home directory of the user, but may also be the
/// current working directory of the wezterm process when
/// it was launched, or for some domains it may be some
/// other location appropriate to the domain.
pub cwd: Option<PathBuf>,
/// Specifies a map of environment variables that should be set.
/// Whether this is used depends on the domain.
#[serde(default)]
pub set_environment_variables: HashMap<String, String>,
#[serde(default)]
pub domain: SpawnTabDomain,
}
impl std::fmt::Debug for SpawnCommand {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "{}", self)
}
}
impl std::fmt::Display for SpawnCommand {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "SpawnCommand")?;
if let Some(label) = &self.label {
write!(fmt, " label='{}'", label)?;
}
write!(fmt, " domain={:?}", self.domain)?;
if let Some(args) = &self.args {
write!(fmt, " args={:?}", args)?;
}
if let Some(cwd) = &self.cwd {
write!(fmt, " cwd={}", cwd.display())?;
}
for (k, v) in &self.set_environment_variables {
write!(fmt, " {}={}", k, v)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub enum PaneDirection {
Up,
Down,
Left,
Right,
Next,
Prev,
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum ScrollbackEraseMode {
ScrollbackOnly,
ScrollbackAndViewport,
}
impl Default for ScrollbackEraseMode {
fn default() -> Self {
Self::ScrollbackOnly
}
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub enum ClipboardCopyDestination {
Clipboard,
PrimarySelection,
ClipboardAndPrimarySelection,
}
impl Default for ClipboardCopyDestination {
fn default() -> Self {
Self::ClipboardAndPrimarySelection
}
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub enum ClipboardPasteSource {
Clipboard,
PrimarySelection,
}
impl Default for ClipboardPasteSource {
fn default() -> Self {
Self::Clipboard
}
}
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct QuickSelectArguments {
/// Overrides the main quick_select_alphabet config
#[serde(default)]
pub alphabet: String,
/// Overrides the main quick_select_patterns config
#[serde(default)]
pub patterns: Vec<String>,
#[serde(default)]
pub action: Option<Box<KeyAssignment>>,
/// Label to use in place of "copy" when `action` is set
#[serde(default)]
pub label: String,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum KeyAssignment {
SpawnTab(SpawnTabDomain),
SpawnWindow,
ToggleFullScreen,
Copy,
CopyTo(ClipboardCopyDestination),
Paste,
PastePrimarySelection,
PasteFrom(ClipboardPasteSource),
ActivateTabRelative(isize),
ActivateTabRelativeNoWrap(isize),
IncreaseFontSize,
DecreaseFontSize,
ResetFontSize,
ResetFontAndWindowSize,
ActivateTab(isize),
ActivateLastTab,
SendString(String),
SendKey(KeyNoAction),
Nop,
DisableDefaultAssignment,
Hide,
Show,
CloseCurrentTab {
confirm: bool,
},
ReloadConfiguration,
MoveTabRelative(isize),
MoveTab(usize),
#[serde(deserialize_with = "de_notnan")]
ScrollByPage(NotNan<f64>),
ScrollByLine(isize),
ScrollToPrompt(isize),
ScrollToTop,
ScrollToBottom,
ShowTabNavigator,
ShowDebugOverlay,
HideApplication,
QuitApplication,
SpawnCommandInNewTab(SpawnCommand),
SpawnCommandInNewWindow(SpawnCommand),
SplitHorizontal(SpawnCommand),
SplitVertical(SpawnCommand),
ShowLauncher,
ShowLauncherArgs(LauncherActionArgs),
ClearScrollback(ScrollbackEraseMode),
Search(Pattern),
ActivateCopyMode,
SelectTextAtMouseCursor(SelectionMode),
ExtendSelectionToMouseCursor(Option<SelectionMode>),
OpenLinkAtMouseCursor,
ClearSelection,
CompleteSelection(ClipboardCopyDestination),
CompleteSelectionOrOpenLinkAtMouseCursor(ClipboardCopyDestination),
StartWindowDrag,
AdjustPaneSize(PaneDirection, usize),
ActivatePaneDirection(PaneDirection),
ActivatePaneByIndex(usize),
TogglePaneZoomState,
CloseCurrentPane {
confirm: bool,
},
EmitEvent(String),
QuickSelect,
QuickSelectArgs(QuickSelectArguments),
Multiple(Vec<KeyAssignment>),
SwitchToWorkspace {
name: Option<String>,
spawn: Option<SpawnCommand>,
},
SwitchWorkspaceRelative(isize),
ActivateKeyTable {
name: String,
#[serde(default)]
timeout_milliseconds: Option<u64>,
#[serde(default)]
replace_current: bool,
#[serde(default = "crate::default_true")]
one_shot: bool,
},
PopKeyTable,
ClearKeyTableStack,
DetachDomain(SpawnTabDomain),
AttachDomain(String),
CopyMode(CopyModeAssignment),
}
impl_lua_conversion!(KeyAssignment);
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum CopyModeAssignment {
MoveToViewportBottom,
MoveToViewportTop,
MoveToViewportMiddle,
MoveToScrollbackTop,
MoveToScrollbackBottom,
ToggleSelectionByCell,
SetSelectionMode(Option<SelectionMode>),
MoveToStartOfLineContent,
MoveToEndOfLineContent,
MoveToStartOfLine,
MoveToStartOfNextLine,
MoveBackwardWord,
MoveForwardWord,
MoveRight,
MoveLeft,
MoveUp,
MoveDown,
PageUp,
PageDown,
Close,
PriorMatch,
NextMatch,
PriorMatchPage,
NextMatchPage,
CycleMatchType,
ClearPattern,
EditPattern,
AcceptPattern,
}
impl_lua_conversion!(CopyModeAssignment);
pub type KeyTable = HashMap<(KeyCode, Modifiers), KeyTableEntry>;
#[derive(Debug, Clone, Default)]
pub struct KeyTables {
pub default: KeyTable,
pub by_name: HashMap<String, KeyTable>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyTableEntry {
pub action: KeyAssignment,
}