aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-server/src/session_layout_metadata.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2024-04-30 15:21:04 +0200
committerGitHub <noreply@github.com>2024-04-30 15:21:04 +0200
commit64ce7a7d751b3b03403af0e99bab6f1c64d6d56e (patch)
tree216050770c518e3be7507e0d08d659c41cf47de7 /zellij-server/src/session_layout_metadata.rs
parentdocs(changelog): layoutinfo newtabs plugin api (diff)
downloadsl0ppij-64ce7a7d751b3b03403af0e99bab6f1c64d6d56e.tar.gz
sl0ppij-64ce7a7d751b3b03403af0e99bab6f1c64d6d56e.tar.xz
sl0ppij-64ce7a7d751b3b03403af0e99bab6f1c64d6d56e.zip
feat(cli): list clients, their focused pane_id and the running command (#3314)
* feat(cli): list clients * style(fmt): rustfmt
Diffstat (limited to 'zellij-server/src/session_layout_metadata.rs')
-rw-r--r--zellij-server/src/session_layout_metadata.rs99
1 files changed, 97 insertions, 2 deletions
diff --git a/zellij-server/src/session_layout_metadata.rs b/zellij-server/src/session_layout_metadata.rs
index da19bc90..0ca25804 100644
--- a/zellij-server/src/session_layout_metadata.rs
+++ b/zellij-server/src/session_layout_metadata.rs
@@ -1,12 +1,16 @@
use crate::panes::PaneId;
-use std::collections::HashMap;
+use crate::ClientId;
+use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
use zellij_utils::common_path::common_path_all;
use zellij_utils::pane_size::PaneGeom;
use zellij_utils::{
input::command::RunCommand,
input::layout::{Layout, Run, RunPlugin, RunPluginOrAlias},
- session_serialization::{GlobalLayoutManifest, PaneLayoutManifest, TabLayoutManifest},
+ session_serialization::{
+ extract_command_and_args, extract_edit_and_line_number, extract_plugin_and_config,
+ GlobalLayoutManifest, PaneLayoutManifest, TabLayoutManifest,
+ },
};
#[derive(Default, Debug, Clone)]
@@ -14,6 +18,7 @@ pub struct SessionLayoutMetadata {
default_layout: Box<Layout>,
global_cwd: Option<PathBuf>,
pub default_shell: Option<PathBuf>,
+ pub default_editor: Option<PathBuf>,
tabs: Vec<TabLayoutMetadata>,
}
@@ -53,6 +58,29 @@ impl SessionLayoutMetadata {
}
}
}
+ pub fn list_clients_metadata(&self) -> String {
+ let mut clients_metadata: BTreeMap<ClientId, ClientMetadata> = BTreeMap::new();
+ for tab in &self.tabs {
+ let panes = if tab.hide_floating_panes {
+ &tab.tiled_panes
+ } else {
+ &tab.floating_panes
+ };
+ for pane in panes {
+ for focused_client in &pane.focused_clients {
+ clients_metadata.insert(
+ *focused_client,
+ ClientMetadata {
+ pane_id: pane.id.clone(),
+ command: pane.run.clone(),
+ },
+ );
+ }
+ }
+ }
+
+ ClientMetadata::render_many(clients_metadata, &self.default_editor)
+ }
fn is_default_shell(
default_shell: Option<&PathBuf>,
command_name: &String,
@@ -192,6 +220,15 @@ impl SessionLayoutMetadata {
}
}
}
+ pub fn update_default_editor(&mut self, default_editor: &Option<PathBuf>) {
+ let default_editor = default_editor.clone().unwrap_or_else(|| {
+ PathBuf::from(
+ std::env::var("EDITOR")
+ .unwrap_or_else(|_| std::env::var("VISUAL").unwrap_or_else(|_| "vi".into())),
+ )
+ });
+ self.default_editor = Some(default_editor);
+ }
}
impl Into<GlobalLayoutManifest> for SessionLayoutMetadata {
@@ -253,6 +290,7 @@ pub struct PaneLayoutMetadata {
title: Option<String>,
is_focused: bool,
pane_contents: Option<String>,
+ focused_clients: Vec<ClientId>,
}
impl PaneLayoutMetadata {
@@ -264,6 +302,7 @@ impl PaneLayoutMetadata {
title: Option<String>,
is_focused: bool,
pane_contents: Option<String>,
+ focused_clients: Vec<ClientId>,
) -> Self {
PaneLayoutMetadata {
id,
@@ -274,6 +313,62 @@ impl PaneLayoutMetadata {
title,
is_focused,
pane_contents,
+ focused_clients,
+ }
+ }
+}
+
+struct ClientMetadata {
+ pane_id: PaneId,
+ command: Option<Run>,
+}
+impl ClientMetadata {
+ pub fn stringify_pane_id(&self) -> String {
+ match self.pane_id {
+ PaneId::Terminal(terminal_id) => format!("terminal_{}", terminal_id),
+ PaneId::Plugin(plugin_id) => format!("plugin_{}", plugin_id),
+ }
+ }
+ pub fn stringify_command(&self, editor: &Option<PathBuf>) -> String {
+ let stringified = match &self.command {
+ Some(Run::Command(..)) => {
+ let (command, args) = extract_command_and_args(&self.command);
+ command.map(|c| format!("{} {}", c, args.join(" ")))
+ },
+ Some(Run::EditFile(..)) => {
+ let (file_to_edit, _line_number) = extract_edit_and_line_number(&self.command);
+ editor.as_ref().and_then(|editor| {
+ file_to_edit
+ .map(|file_to_edit| format!("{} {}", editor.display(), file_to_edit))
+ })
+ },
+ Some(Run::Plugin(..)) => {
+ let (plugin, _plugin_config) = extract_plugin_and_config(&self.command);
+ plugin.map(|p| format!("{}", p))
+ },
+ _ => None,
+ };
+ stringified.unwrap_or("N/A".to_owned())
+ }
+ pub fn render_many(
+ clients_metadata: BTreeMap<ClientId, ClientMetadata>,
+ default_editor: &Option<PathBuf>,
+ ) -> String {
+ let mut lines = vec![];
+ lines.push(String::from("CLIENT_ID ZELLIJ_PANE_ID RUNNING_COMMAND"));
+
+ for (client_id, client_metadata) in clients_metadata.iter() {
+ // 9 - CLIENT_ID, 14 - ZELLIJ_PANE_ID, 15 - RUNNING_COMMAND
+ lines.push(format!(
+ "{} {} {}",
+ format!("{0: <9}", client_id),
+ format!("{0: <14}", client_metadata.stringify_pane_id()),
+ format!(
+ "{0: <15}",
+ client_metadata.stringify_command(default_editor)
+ )
+ ));
}
+ lines.join("\n")
}
}