pub struct EditorBuilder<'a> { /* private fields */ }
Expand description
A builder for customizing an Editor. In simple cases you can just use Editor::new and don’t have to interact with this struct. See crate-level documentation for more details and examples.
§Example
The builder works by calling one or more “source” methods. Each source may (or may not) provide an editor command. The first source that provides a command will be used, and subsequent sources will be ignored. For example, here’s a builder that uses 3 sources:
- User’s configured editor
- Environment variables
- Static fallback
use editor_command::EditorBuilder;
use std::process::Command;
std::env::set_var("VISUAL", "vim"); // This gets overridden
let editor = EditorBuilder::new()
.string(configured_editor())
.environment()
// If both VISUAL and EDITOR are undefined, we'll fall back to this
.string(Some("vi"))
.build()
.unwrap();
let command = editor.open("file.txt");
assert_eq!(command.get_program(), "code");
assert_eq!(command.get_args().collect::<Vec<_>>(), &["--wait", "file.txt"]);
fn configured_editor() -> Option<String> {
// In reality this would load from a config file or similar
Some("code --wait".into())
}
§Lifetimes
EditorBuilder accepts a lifetime parameter, which is bound to the string
data it contains (both command strings and paths). This is to prevent
unnecessary cloning when building editors from &str
s. If you need
the instance of EditorBuilder to be 'static
, e.g. so it can be returned
from a function, you can simply use EditorBuilder<'static>
. Internally,
all strings are stored as Cows, so clones will be made as necessary. Once
the builder is converted into an Editor, all strings will be cloned.
use editor_command::EditorBuilder;
/// This is a contrived example of returning a command with owned data
fn get_editor_builder<'a>(command: &'a str) -> EditorBuilder<'static> {
// The lifetime bounds enforce the .to_owned() call
EditorBuilder::new().string(Some(command.to_owned()))
}
let editor = get_editor_builder("vim").build().unwrap();
assert_eq!(editor.open("file").get_program(), "vim");
Implementations§
Source§impl<'a> EditorBuilder<'a>
impl<'a> EditorBuilder<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new editor command with no sources. You probably want to call environment on the returned value.
Sourcepub fn string(self, source: Option<impl Into<Cow<'a, str>>>) -> Self
pub fn string(self, source: Option<impl Into<Cow<'a, str>>>) -> Self
Load the editor command from a string. This is useful for static
defaults or external sources such as a configuration file. This accepts
an Option
so you can easily build a chain of sources that may or may
not be defined.
Sourcepub fn environment(self) -> Self
pub fn environment(self) -> Self
Load the editor command from the VISUAL
and EDITOR
environment
variables, in that order. The variables will be evaluated immediately,
not during build.
Trait Implementations§
Source§impl<'a> Clone for EditorBuilder<'a>
impl<'a> Clone for EditorBuilder<'a>
Source§fn clone(&self) -> EditorBuilder<'a>
fn clone(&self) -> EditorBuilder<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more