use std::ffi::OsString;
use super::SESSION_FORMAT;
use super::{Effects, Op, Operation, Safety, Scope, SessionSlot, Slot};
use crate::Command;
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NewSession {
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::argument",
deserialize_with = "crate::plan::wire::parse_argument"
)
)]
name: OsString,
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::optional_argument",
deserialize_with = "crate::plan::wire::parse_optional_argument"
)
)]
start_directory: Option<OsString>,
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::plan::wire::optional_argument",
deserialize_with = "crate::plan::wire::parse_optional_argument"
)
)]
window_name: Option<OsString>,
}
impl NewSession {
#[must_use]
pub fn new(name: impl Into<OsString>) -> Self {
Self {
name: name.into(),
start_directory: None,
window_name: None,
}
}
#[must_use]
pub fn start_directory(mut self, directory: impl Into<OsString>) -> Self {
self.start_directory = Some(directory.into());
self
}
#[must_use]
pub fn window_name(mut self, name: impl Into<OsString>) -> Self {
self.window_name = Some(name.into());
self
}
pub(crate) fn render(&self) -> Command {
let mut command = Command::new("new-session")
.arg("-d")
.arg("-P")
.arg("-F")
.arg(SESSION_FORMAT)
.arg("-s")
.arg(self.name.clone());
if let Some(directory) = &self.start_directory {
command = command.arg("-c").arg(directory.clone());
}
if let Some(name) = &self.window_name {
command = command.arg("-n").arg(name.clone());
}
command
}
}
operation!(
NewSession,
creates = Slot<SessionSlot>,
effects = Effects {
creates: Some(Scope::Session),
..Effects::MUTATING
},
safety = Safety::Mutating
);