From 4c6b03acc16c968663d2875556aae25ef6dac87c Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sat, 4 Nov 2023 11:20:50 +0100 Subject: feat(sessions): resurrect sessions through the session-manager (and plugin API) (#2902) * working with table and scrolling * ui and functionality complete * fix formatting * refactor: background jobs * style(fmt): rustfmt --- zellij-utils/src/plugin_api/event.proto | 6 ++++ zellij-utils/src/plugin_api/event.rs | 43 +++++++++++++++++++++--- zellij-utils/src/plugin_api/plugin_command.proto | 3 ++ zellij-utils/src/plugin_api/plugin_command.rs | 15 +++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) (limited to 'zellij-utils/src/plugin_api') diff --git a/zellij-utils/src/plugin_api/event.proto b/zellij-utils/src/plugin_api/event.proto index cfffe06f5..0d964a354 100644 --- a/zellij-utils/src/plugin_api/event.proto +++ b/zellij-utils/src/plugin_api/event.proto @@ -70,6 +70,7 @@ message Event { message SessionUpdatePayload { repeated SessionManifest session_manifests = 1; + repeated ResurrectableSession resurrectable_sessions = 2; } message RunCommandResultPayload { @@ -153,6 +154,11 @@ message SessionManifest { bool is_current_session = 5; } +message ResurrectableSession { + string name = 1; + uint64 creation_time = 2; +} + message PaneInfo { uint32 id = 1; bool is_plugin = 2; diff --git a/zellij-utils/src/plugin_api/event.rs b/zellij-utils/src/plugin_api/event.rs index a322ec3bf..d9e5341d6 100644 --- a/zellij-utils/src/plugin_api/event.rs +++ b/zellij-utils/src/plugin_api/event.rs @@ -6,6 +6,7 @@ pub use super::generated_api::api::{ EventType as ProtobufEventType, InputModeKeybinds as ProtobufInputModeKeybinds, KeyBind as ProtobufKeyBind, ModeUpdatePayload as ProtobufModeUpdatePayload, PaneInfo as ProtobufPaneInfo, PaneManifest as ProtobufPaneManifest, + ResurrectableSession as ProtobufResurrectableSession, SessionManifest as ProtobufSessionManifest, TabInfo as ProtobufTabInfo, *, }, input_mode::InputMode as ProtobufInputMode, @@ -23,6 +24,7 @@ use crate::input::actions::Action; use std::collections::{HashMap, HashSet}; use std::convert::TryFrom; use std::path::PathBuf; +use std::time::Duration; impl TryFrom for Event { type Error = &'static str; @@ -176,10 +178,19 @@ impl TryFrom for Event { protobuf_session_update_payload, )) => { let mut session_infos: Vec = vec![]; + let mut resurrectable_sessions: Vec<(String, Duration)> = vec![]; for protobuf_session_info in protobuf_session_update_payload.session_manifests { session_infos.push(SessionInfo::try_from(protobuf_session_info)?); } - Ok(Event::SessionUpdate(session_infos)) + for protobuf_resurrectable_session in + protobuf_session_update_payload.resurrectable_sessions + { + resurrectable_sessions.push(protobuf_resurrectable_session.into()); + } + Ok(Event::SessionUpdate( + session_infos, + resurrectable_sessions.into(), + )) }, _ => Err("Malformed payload for the SessionUpdate Event"), }, @@ -359,13 +370,18 @@ impl TryFrom for ProtobufEvent { )), }) }, - Event::SessionUpdate(session_infos) => { + Event::SessionUpdate(session_infos, resurrectable_sessions) => { let mut protobuf_session_manifests = vec![]; for session_info in session_infos { protobuf_session_manifests.push(session_info.try_into()?); } + let mut protobuf_resurrectable_sessions = vec![]; + for resurrectable_session in resurrectable_sessions { + protobuf_resurrectable_sessions.push(resurrectable_session.into()); + } let session_update_payload = SessionUpdatePayload { session_manifests: protobuf_session_manifests, + resurrectable_sessions: protobuf_resurrectable_sessions, }; Ok(ProtobufEvent { name: ProtobufEventType::SessionUpdate as i32, @@ -887,6 +903,24 @@ impl TryFrom for ProtobufEventType { } } +impl From for (String, Duration) { + fn from(protobuf_resurrectable_session: ProtobufResurrectableSession) -> (String, Duration) { + ( + protobuf_resurrectable_session.name, + Duration::from_secs(protobuf_resurrectable_session.creation_time), + ) + } +} + +impl From<(String, Duration)> for ProtobufResurrectableSession { + fn from(session_name_and_creation_time: (String, Duration)) -> ProtobufResurrectableSession { + ProtobufResurrectableSession { + name: session_name_and_creation_time.0, + creation_time: session_name_and_creation_time.1.as_secs(), + } + } +} + #[test] fn serialize_mode_update_event() { use prost::Message; @@ -1249,7 +1283,7 @@ fn serialize_file_system_delete_event() { #[test] fn serialize_session_update_event() { use prost::Message; - let session_update_event = Event::SessionUpdate(Default::default()); + let session_update_event = Event::SessionUpdate(Default::default(), Default::default()); let protobuf_event: ProtobufEvent = session_update_event.clone().try_into().unwrap(); let serialized_protobuf_event = protobuf_event.encode_to_vec(); let deserialized_protobuf_event: ProtobufEvent = @@ -1360,8 +1394,9 @@ fn serialize_session_update_event_with_non_default_values() { is_current_session: false, }; let session_infos = vec![session_info_1, session_info_2]; + let resurrectable_sessions = vec![]; - let session_update_event = Event::SessionUpdate(session_infos); + let session_update_event = Event::SessionUpdate(session_infos, resurrectable_sessions); let protobuf_event: ProtobufEvent = session_update_event.clone().try_into().unwrap(); let serialized_protobuf_event = protobuf_event.encode_to_vec(); let deserialized_protobuf_event: ProtobufEvent = diff --git a/zellij-utils/src/plugin_api/plugin_command.proto b/zellij-utils/src/plugin_api/plugin_command.proto index 93e7d6b2b..0b950ff0e 100644 --- a/zellij-utils/src/plugin_api/plugin_command.proto +++ b/zellij-utils/src/plugin_api/plugin_command.proto @@ -84,6 +84,8 @@ enum CommandName { OpenFileInPlace = 70; RunCommand = 71; WebRequest = 72; + DeleteDeadSession = 73; + DeleteAllDeadSessions = 74; } message PluginCommand { @@ -132,6 +134,7 @@ message PluginCommand { OpenCommandPanePayload open_command_pane_in_place_payload = 42; RunCommandPayload run_command_payload = 43; WebRequestPayload web_request_payload = 44; + string delete_dead_session_payload = 45; } } diff --git a/zellij-utils/src/plugin_api/plugin_command.rs b/zellij-utils/src/plugin_api/plugin_command.rs index 84c083ad5..6b70b18eb 100644 --- a/zellij-utils/src/plugin_api/plugin_command.rs +++ b/zellij-utils/src/plugin_api/plugin_command.rs @@ -628,6 +628,13 @@ impl TryFrom for PluginCommand { }, _ => Err("Mismatched payload for WebRequest"), }, + Some(CommandName::DeleteDeadSession) => match protobuf_plugin_command.payload { + Some(Payload::DeleteDeadSessionPayload(dead_session_name)) => { + Ok(PluginCommand::DeleteDeadSession(dead_session_name)) + }, + _ => Err("Mismatched payload for DeleteDeadSession"), + }, + Some(CommandName::DeleteAllDeadSessions) => Ok(PluginCommand::DeleteAllDeadSessions), None => Err("Unrecognized plugin command"), } } @@ -1044,6 +1051,14 @@ impl TryFrom for ProtobufPluginCommand { })), }) }, + PluginCommand::DeleteDeadSession(dead_session_name) => Ok(ProtobufPluginCommand { + name: CommandName::DeleteDeadSession as i32, + payload: Some(Payload::DeleteDeadSessionPayload(dead_session_name)), + }), + PluginCommand::DeleteAllDeadSessions => Ok(ProtobufPluginCommand { + name: CommandName::DeleteAllDeadSessions as i32, + payload: None, + }), } } } -- cgit v1.2.3