aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-utils/src/plugin_api/plugin_command.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2024-10-15 17:27:44 +0200
committerGitHub <noreply@github.com>2024-10-15 17:27:44 +0200
commit0c9d72c51e451d4b0ff91e0fa1495187c59a48be (patch)
tree56d307d0f6a32caab7f664bc4a6b049de48137fd /zellij-utils/src/plugin_api/plugin_command.rs
parentdocs(changelog): reload config flag fix (diff)
downloadsl0ppij-0c9d72c51e451d4b0ff91e0fa1495187c59a48be.tar.gz
sl0ppij-0c9d72c51e451d4b0ff91e0fa1495187c59a48be.tar.xz
sl0ppij-0c9d72c51e451d4b0ff91e0fa1495187c59a48be.zip
feat(plugins): rebind keys api (#3680)
* feat(plugins): add API to explicitly unbind/rebind specific keys in specific modes * style(fmt): rustfmt
Diffstat (limited to 'zellij-utils/src/plugin_api/plugin_command.rs')
-rw-r--r--zellij-utils/src/plugin_api/plugin_command.rs121
1 files changed, 106 insertions, 15 deletions
diff --git a/zellij-utils/src/plugin_api/plugin_command.rs b/zellij-utils/src/plugin_api/plugin_command.rs
index d455500a..79b08794 100644
--- a/zellij-utils/src/plugin_api/plugin_command.rs
+++ b/zellij-utils/src/plugin_api/plugin_command.rs
@@ -9,28 +9,29 @@ pub use super::generated_api::api::{
FixedOrPercent as ProtobufFixedOrPercent,
FixedOrPercentValue as ProtobufFixedOrPercentValue,
FloatingPaneCoordinates as ProtobufFloatingPaneCoordinates, HidePaneWithIdPayload,
- HttpVerb as ProtobufHttpVerb, IdAndNewName, KillSessionsPayload, LoadNewPluginPayload,
- MessageToPluginPayload, MovePaneWithPaneIdInDirectionPayload, MovePaneWithPaneIdPayload,
- MovePayload, NewPluginArgs as ProtobufNewPluginArgs, NewTabsWithLayoutInfoPayload,
- OpenCommandPanePayload, OpenFilePayload, PageScrollDownInPaneIdPayload,
- PageScrollUpInPaneIdPayload, PaneId as ProtobufPaneId, PaneType as ProtobufPaneType,
- PluginCommand as ProtobufPluginCommand, PluginMessagePayload, ReconfigurePayload,
- ReloadPluginPayload, RequestPluginPermissionPayload, RerunCommandPanePayload,
- ResizePaneIdWithDirectionPayload, ResizePayload, RunCommandPayload,
- ScrollDownInPaneIdPayload, ScrollToBottomInPaneIdPayload, ScrollToTopInPaneIdPayload,
- ScrollUpInPaneIdPayload, SetTimeoutPayload, ShowPaneWithIdPayload, SubscribePayload,
- SwitchSessionPayload, SwitchTabToPayload, TogglePaneEmbedOrEjectForPaneIdPayload,
- TogglePaneIdFullscreenPayload, UnsubscribePayload, WebRequestPayload,
- WriteCharsToPaneIdPayload, WriteToPaneIdPayload,
+ HttpVerb as ProtobufHttpVerb, IdAndNewName, KeyToRebind, KeyToUnbind, KillSessionsPayload,
+ LoadNewPluginPayload, MessageToPluginPayload, MovePaneWithPaneIdInDirectionPayload,
+ MovePaneWithPaneIdPayload, MovePayload, NewPluginArgs as ProtobufNewPluginArgs,
+ NewTabsWithLayoutInfoPayload, OpenCommandPanePayload, OpenFilePayload,
+ PageScrollDownInPaneIdPayload, PageScrollUpInPaneIdPayload, PaneId as ProtobufPaneId,
+ PaneType as ProtobufPaneType, PluginCommand as ProtobufPluginCommand, PluginMessagePayload,
+ RebindKeysPayload, ReconfigurePayload, ReloadPluginPayload, RequestPluginPermissionPayload,
+ RerunCommandPanePayload, ResizePaneIdWithDirectionPayload, ResizePayload,
+ RunCommandPayload, ScrollDownInPaneIdPayload, ScrollToBottomInPaneIdPayload,
+ ScrollToTopInPaneIdPayload, ScrollUpInPaneIdPayload, SetTimeoutPayload,
+ ShowPaneWithIdPayload, SubscribePayload, SwitchSessionPayload, SwitchTabToPayload,
+ TogglePaneEmbedOrEjectForPaneIdPayload, TogglePaneIdFullscreenPayload, UnsubscribePayload,
+ WebRequestPayload, WriteCharsToPaneIdPayload, WriteToPaneIdPayload,
},
plugin_permission::PermissionType as ProtobufPermissionType,
resize::ResizeAction as ProtobufResizeAction,
};
use crate::data::{
- ConnectToSession, FloatingPaneCoordinates, HttpVerb, MessageToPlugin, NewPluginArgs, PaneId,
- PermissionType, PluginCommand,
+ ConnectToSession, FloatingPaneCoordinates, HttpVerb, InputMode, KeyWithModifier,
+ MessageToPlugin, NewPluginArgs, PaneId, PermissionType, PluginCommand,
};
+use crate::input::actions::Action;
use crate::input::layout::SplitSize;
use std::collections::BTreeMap;
@@ -184,6 +185,60 @@ impl TryFrom<PaneId> for ProtobufPaneId {
}
}
+impl TryFrom<(InputMode, KeyWithModifier, Vec<Action>)> for KeyToRebind {
+ type Error = &'static str;
+ fn try_from(
+ key_to_rebind: (InputMode, KeyWithModifier, Vec<Action>),
+ ) -> Result<Self, &'static str> {
+ Ok(KeyToRebind {
+ input_mode: key_to_rebind.0 as i32,
+ key: Some(key_to_rebind.1.try_into()?),
+ actions: key_to_rebind
+ .2
+ .into_iter()
+ .filter_map(|a| a.try_into().ok())
+ .collect(),
+ })
+ }
+}
+
+impl TryFrom<(InputMode, KeyWithModifier)> for KeyToUnbind {
+ type Error = &'static str;
+ fn try_from(key_to_unbind: (InputMode, KeyWithModifier)) -> Result<Self, &'static str> {
+ Ok(KeyToUnbind {
+ input_mode: key_to_unbind.0 as i32,
+ key: Some(key_to_unbind.1.try_into()?),
+ })
+ }
+}
+
+fn key_to_rebind_to_plugin_command_assets(
+ key_to_rebind: KeyToRebind,
+) -> Option<(InputMode, KeyWithModifier, Vec<Action>)> {
+ Some((
+ ProtobufInputMode::from_i32(key_to_rebind.input_mode)?
+ .try_into()
+ .ok()?,
+ key_to_rebind.key?.try_into().ok()?,
+ key_to_rebind
+ .actions
+ .into_iter()
+ .filter_map(|a| a.try_into().ok())
+ .collect(),
+ ))
+}
+
+fn key_to_unbind_to_plugin_command_assets(
+ key_to_unbind: KeyToUnbind,
+) -> Option<(InputMode, KeyWithModifier)> {
+ Some((
+ ProtobufInputMode::from_i32(key_to_unbind.input_mode)?
+ .try_into()
+ .ok()?,
+ key_to_unbind.key?.try_into().ok()?,
+ ))
+}
+
impl TryFrom<ProtobufPluginCommand> for PluginCommand {
type Error = &'static str;
fn try_from(protobuf_plugin_command: ProtobufPluginCommand) -> Result<Self, &'static str> {
@@ -1226,6 +1281,24 @@ impl TryFrom<ProtobufPluginCommand> for PluginCommand {
},
_ => Err("Mismatched payload for LoadNewPlugin"),
},
+ Some(CommandName::RebindKeys) => match protobuf_plugin_command.payload {
+ Some(Payload::RebindKeysPayload(rebind_keys_payload)) => {
+ Ok(PluginCommand::RebindKeys {
+ keys_to_rebind: rebind_keys_payload
+ .keys_to_rebind
+ .into_iter()
+ .filter_map(|k| key_to_rebind_to_plugin_command_assets(k))
+ .collect(),
+ keys_to_unbind: rebind_keys_payload
+ .keys_to_unbind
+ .into_iter()
+ .filter_map(|k| key_to_unbind_to_plugin_command_assets(k))
+ .collect(),
+ write_config_to_disk: rebind_keys_payload.write_config_to_disk,
+ })
+ },
+ _ => Err("Mismatched payload for RebindKeys"),
+ },
None => Err("Unrecognized plugin command"),
}
}
@@ -2031,6 +2104,24 @@ impl TryFrom<PluginCommand> for ProtobufPluginCommand {
should_load_plugin_in_background: load_in_background,
})),
}),
+ PluginCommand::RebindKeys {
+ keys_to_rebind,
+ keys_to_unbind,
+ write_config_to_disk,
+ } => Ok(ProtobufPluginCommand {
+ name: CommandName::RebindKeys as i32,
+ payload: Some(Payload::RebindKeysPayload(RebindKeysPayload {
+ keys_to_rebind: keys_to_rebind
+ .into_iter()
+ .filter_map(|k| k.try_into().ok())
+ .collect(),
+ keys_to_unbind: keys_to_unbind
+ .into_iter()
+ .filter_map(|k| k.try_into().ok())
+ .collect(),
+ write_config_to_disk,
+ })),
+ }),
}
}
}