use std::ffi::{OsStr, OsString};
use super::{Chooser, Server};
use crate::client::Client;
use crate::internal::listing;
use crate::{Command, Error};
impl Server {
pub async fn display_popup(
&self,
client: Option<&Client>,
command: impl Into<OsString>,
) -> Result<(), Error> {
let mut popup = Command::new("display-popup").arg("-E");
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), "display-popup")?;
popup = popup
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
listing::mutate(&self.core, "display-popup", popup.arg(command.into())).await
}
pub async fn display_menu(
&self,
client: Option<&Client>,
title: &str,
items: impl IntoIterator<Item = MenuItem>,
) -> Result<(), Error> {
let mut menu = Command::new("display-menu")
.arg("-T")
.arg(OsString::from(title));
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), "display-menu")?;
menu = menu
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
for item in items {
menu = menu.arg(item.label).arg(item.key).arg(item.command);
}
listing::mutate(&self.core, "display-menu", menu).await
}
pub async fn command_prompt(
&self,
client: Option<&Client>,
prompt: Option<&str>,
command: impl Into<OsString>,
) -> Result<(), Error> {
let mut request = Command::new("command-prompt");
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), "command-prompt")?;
request = request
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
if let Some(prompt) = prompt {
request = request.arg("-p").arg(OsString::from(prompt));
}
listing::mutate(&self.core, "command-prompt", request.arg(command.into())).await
}
pub async fn choose(&self, chooser: Chooser, client: Option<&Client>) -> Result<(), Error> {
let name = chooser.command();
let mut request = Command::new(name);
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), name)?;
request = request
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
listing::mutate(&self.core, name, request).await
}
pub async fn find_window(&self, client: Option<&Client>, search: &str) -> Result<(), Error> {
let mut request = Command::new("find-window");
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), "find-window")?;
request = request
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
listing::mutate(
&self.core,
"find-window",
request.arg(OsString::from(search)),
)
.await
}
pub async fn display_panes(&self, client: Option<&Client>) -> Result<(), Error> {
let mut request = Command::new("display-panes");
if let Some(client) = client {
self.core
.require_same_server(client.server_identity(), "display-panes")?;
request = request
.arg("-t")
.arg(client.name().to_string_lossy().into_owned());
}
listing::mutate(&self.core, "display-panes", request).await
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MenuItem {
label: OsString,
key: OsString,
command: OsString,
}
impl MenuItem {
#[must_use]
pub fn new(
label: impl Into<OsString>,
key: impl Into<OsString>,
command: impl Into<OsString>,
) -> Self {
Self {
label: label.into(),
key: key.into(),
command: command.into(),
}
}
#[must_use]
pub fn label(&self) -> &OsStr {
&self.label
}
#[must_use]
pub fn key(&self) -> &OsStr {
&self.key
}
#[must_use]
pub fn command(&self) -> &OsStr {
&self.command
}
}