pub struct Workspace {
pub session_name: String,
pub start_directory: Option<PathBuf>,
pub environment: Vec<(String, String)>,
pub options: Vec<(String, String)>,
pub global_options: Vec<(String, String)>,
pub shell_command_before: Vec<ShellCommand>,
pub suppress_history: bool,
pub windows: Vec<WindowConfig>,
pub unsupported_keys: Vec<String>,
}Expand description
One workspace: a session and the windows it should contain.
Fields§
§session_name: StringThe session name to create.
start_directory: Option<PathBuf>A working directory inherited by windows that do not set their own.
environment: Vec<(String, String)>Environment variables to set on the session.
options: Vec<(String, String)>Session options to apply once the session exists.
global_options: Vec<(String, String)>Global options to apply once the session exists.
shell_command_before: Vec<ShellCommand>Commands run in every pane before its own, in order.
suppress_history: boolWhether to keep pane commands out of the shell’s history.
A file that does not say reads as true, as in tmuxp.
windows: Vec<WindowConfig>The windows to create, in order.
unsupported_keys: Vec<String>Keys this parser recognized but does not act on.
Reported rather than dropped, so a caller can say what part of a file was ignored instead of leaving the difference to be discovered later.
Implementations§
Source§impl Workspace
impl Workspace
Sourcepub fn to_yaml(&self) -> String
pub fn to_yaml(&self) -> String
Render this workspace as tmuxp-style YAML.
Emits the keys this crate acts on and nothing else, so a document that
came from Self::from_yaml and back may be shorter than it started:
what is dropped is what unsupported_keys already named.
§Examples
use tmux_workspace::Workspace;
let workspace = Workspace::from_yaml(
"
session_name: demo
windows:
- window_name: editor
panes: [htop]
",
)?;
// What it writes, it can read.
assert_eq!(Workspace::from_yaml(&workspace.to_yaml())?, workspace);Source§impl Workspace
impl Workspace
Sourcepub fn from_yaml(source: &str) -> Result<Self, ConfigError>
pub fn from_yaml(source: &str) -> Result<Self, ConfigError>
Parse one workspace from tmuxp-style YAML.
This accepts the shape tmuxp uses for the parts a builder needs. A key
this parser does not act on is recorded rather than rejected, so a
richer tmuxp file still loads; Self::from_yaml_strict refuses one
instead.
As tmuxp does, it expands ~ and $NAME or ${NAME} from this
process’s environment in names, start directories, and environment
and option values, leaving an unset variable as written; commands are
typed as written, for the pane’s shell to expand. A start directory
that begins with . is relative to the one it inherits, or to the
current directory at the top: Self::from_file uses the file’s
directory instead.
§Errors
Returns an error when the document is not valid YAML, does not hold
exactly one workspace, or is missing session_name. Every error
except the document count names the line and column to look at.
§Examples
use tmux_workspace::Workspace;
let workspace = Workspace::from_yaml(
"
session_name: demo
windows:
- window_name: editor
panes:
- echo one
- shell_command: echo two
",
)?;
assert_eq!(workspace.session_name, "demo");
assert_eq!(workspace.windows.len(), 1);
assert_eq!(workspace.windows[0].panes.len(), 2);Sourcepub fn from_yaml_strict(source: &str) -> Result<Self, ConfigError>
pub fn from_yaml_strict(source: &str) -> Result<Self, ConfigError>
Parse as Self::from_yaml does, refusing a key that is neither this
parser’s own vocabulary nor tmuxp’s, unless it starts with x-.
This is the policy the tmux-workspace command applies to every
document it loads.
§Errors
What Self::from_yaml returns, and also an unrecognized key.
§Examples
use tmux_workspace::Workspace;
let error = Workspace::from_yaml_strict("session_name: demo\nwindows: []\nfrobnicate: 1\n")
.expect_err("an unrecognized key is refused");
assert!(error.to_string().contains("frobnicate"));Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
Read and parse one workspace file, as tmuxp load does.
Everything Self::from_yaml says holds, except that a start
directory beginning with . and inheriting none is relative to the
file’s directory. JSON is read too, as the YAML it is a subset of.
§Errors
Returns ConfigError::Read when the file cannot be read, and
otherwise what Self::from_yaml returns.
§Examples
use tmux_workspace::Workspace;
let project = tempfile::tempdir()?;
let file = project.path().join(".tmuxp.yaml");
std::fs::write(&file, "session_name: project\nstart_directory: ./\nwindows: []\n")?;
let workspace = Workspace::from_file(&file)?;
assert_eq!(workspace.start_directory.as_deref(), Some(project.path()));Sourcepub fn from_file_strict(path: impl AsRef<Path>) -> Result<Self, ConfigError>
pub fn from_file_strict(path: impl AsRef<Path>) -> Result<Self, ConfigError>
Read as Self::from_file does, under Self::from_yaml_strict’s
policy.
§Errors
What Self::from_file returns, and also an unrecognized key.