Skip to main content

reovim_module_commands/
session.rs

1//! Session management commands.
2//!
3//! Commands for managing client sessions and server connections:
4//! - `:detach` - Detach from server (server continues)
5//! - `:servers` - List running server instances
6//! - `:kill-server` - Kill the current server
7
8use {
9    reovim_driver_command::{
10        Command, CommandContext, CommandHandler, CommandResult, RuntimeSignal,
11    },
12    reovim_driver_session::SessionRuntime,
13    reovim_kernel::api::v1::{CommandId, ModuleId},
14};
15
16const COMMANDS_MODULE: ModuleId = ModuleId::new("commands");
17
18// ============================================================================
19// Detach Command
20// ============================================================================
21
22/// Detach from the current server session.
23///
24/// The server continues running after detach, allowing other clients
25/// to connect. The TUI client receives a DETACH notification and
26/// disconnects gracefully.
27///
28/// Usage: `:detach`
29pub struct DetachCommand;
30
31impl Command for DetachCommand {
32    fn id(&self) -> CommandId {
33        CommandId::new(COMMANDS_MODULE, "detach")
34    }
35
36    fn description(&self) -> &'static str {
37        "Detach from the server (server continues running)"
38    }
39
40    fn names(&self) -> &[&'static str] {
41        &["detach"]
42    }
43}
44
45impl CommandHandler for DetachCommand {
46    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
47        runtime.signal(RuntimeSignal::Quit);
48        CommandResult::Success
49    }
50}
51
52// ============================================================================
53// Servers Command
54// ============================================================================
55
56/// List running server instances.
57///
58/// Usage: `:servers`
59pub struct ServersCommand;
60
61impl Command for ServersCommand {
62    fn id(&self) -> CommandId {
63        CommandId::new(COMMANDS_MODULE, "servers")
64    }
65
66    fn description(&self) -> &'static str {
67        "List running server instances"
68    }
69
70    fn names(&self) -> &[&'static str] {
71        &["servers"]
72    }
73}
74
75impl CommandHandler for ServersCommand {
76    fn execute(&self, _runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
77        // Note: The actual listing is handled by the runner.
78        CommandResult::Success
79    }
80}
81
82// ============================================================================
83// KillServer Command
84// ============================================================================
85
86/// Kill the current server.
87///
88/// Terminates the current server process. All connected clients are
89/// disconnected and unsaved changes may be lost.
90///
91/// Usage: `:kill-server`
92pub struct KillServerCommand;
93
94impl Command for KillServerCommand {
95    fn id(&self) -> CommandId {
96        CommandId::new(COMMANDS_MODULE, "kill-server")
97    }
98
99    fn description(&self) -> &'static str {
100        "Kill the current server (terminates all sessions)"
101    }
102
103    fn names(&self) -> &[&'static str] {
104        &["kill-server", "killserver"]
105    }
106}
107
108impl CommandHandler for KillServerCommand {
109    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
110        runtime.signal(RuntimeSignal::Quit);
111        CommandResult::Success
112    }
113}
114
115// ============================================================================
116// Command Collection
117// ============================================================================
118
119/// Get all session management command handlers.
120#[must_use]
121pub fn command_handlers() -> Vec<Box<dyn CommandHandler>> {
122    vec![
123        Box::new(DetachCommand),
124        Box::new(ServersCommand),
125        Box::new(KillServerCommand),
126    ]
127}
128
129// ============================================================================
130// Tests
131// ============================================================================
132
133#[cfg(test)]
134#[path = "session_tests.rs"]
135mod tests;