pub struct EditorSource<R: EditorRunner = RealEditorRunner> { /* private fields */ }Expand description
Collect input via an external text editor.
Opens the user’s preferred editor (from $VISUAL or $EDITOR) with a
temporary file, waits for the user to save and close, then reads the result.
§Editor Detection
Editors are detected in this order:
$VISUALenvironment variable (supports GUI editors)$EDITORenvironment variable- Platform fallbacks:
vim,vi,nanoon Unix;notepadon Windows
§Example
use standout_input::{InputChain, ArgSource, EditorSource};
// Fall back to editor if no CLI argument
let chain = InputChain::<String>::new()
.try_source(ArgSource::new("message"))
.try_source(EditorSource::new());
let message = chain.resolve(&matches)?;§Configuration
let source = EditorSource::new()
.initial_content("# Enter your message\n\n")
.extension(".md")
.require_save(true);Implementations§
Source§impl EditorSource<RealEditorRunner>
impl EditorSource<RealEditorRunner>
Source§impl<R: EditorRunner> EditorSource<R>
impl<R: EditorRunner> EditorSource<R>
Sourcepub fn with_runner(runner: R) -> Self
pub fn with_runner(runner: R) -> Self
Create an editor source with a custom runner.
Primarily used for testing to mock editor invocation.
Sourcepub fn initial_content(self, content: impl Into<String>) -> Self
pub fn initial_content(self, content: impl Into<String>) -> Self
Set initial content to populate the editor with.
This can be used to provide a template or instructions.
Sourcepub fn extension(self, ext: impl Into<String>) -> Self
pub fn extension(self, ext: impl Into<String>) -> Self
Set the file extension for the temporary file.
This affects syntax highlighting in the editor.
Default is .txt.
Sourcepub fn require_save(self, require: bool) -> Self
pub fn require_save(self, require: bool) -> Self
Require the user to actually save the file.
If true, the source will return None if the file’s modification
time hasn’t changed (i.e., the user closed without saving).
Default is false.
Source§impl<R: EditorRunner + 'static> EditorSource<R>
impl<R: EditorRunner + 'static> EditorSource<R>
Sourcepub fn prompt(&self) -> Result<String, InputError>
pub fn prompt(&self) -> Result<String, InputError>
Open the editor and return the saved content.
This is the standalone counterpart to InputCollector::collect:
it skips the chain machinery (no &ArgMatches to plumb through) and
is intended for wizard or REPL flows that drive standout themselves.
Returns InputError::NoInput if stdin is not a TTY or no editor
can be detected (the same conditions under which a chain would skip
this source). User cancellation is reported as
InputError::EditorCancelled when require_save is set and the
user exits without saving.
Routes through any installed
PromptResponder, so wizard tests can
supply the editor’s “saved” content directly without launching
$EDITOR.
Trait Implementations§
Source§impl<R: Clone + EditorRunner> Clone for EditorSource<R>
impl<R: Clone + EditorRunner> Clone for EditorSource<R>
Source§fn clone(&self) -> EditorSource<R>
fn clone(&self) -> EditorSource<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for EditorSource<RealEditorRunner>
impl Default for EditorSource<RealEditorRunner>
Source§impl<R: EditorRunner + 'static> InputCollector<String> for EditorSource<R>
impl<R: EditorRunner + 'static> InputCollector<String> for EditorSource<R>
Source§fn is_available(&self, _matches: &ArgMatches) -> bool
fn is_available(&self, _matches: &ArgMatches) -> bool
Source§fn collect(&self, _matches: &ArgMatches) -> Result<Option<String>, InputError>
fn collect(&self, _matches: &ArgMatches) -> Result<Option<String>, InputError>
Auto Trait Implementations§
impl<R> Freeze for EditorSource<R>
impl<R> RefUnwindSafe for EditorSource<R>where
R: RefUnwindSafe,
impl<R> Send for EditorSource<R>
impl<R> Sync for EditorSource<R>
impl<R> Unpin for EditorSource<R>
impl<R> UnsafeUnpin for EditorSource<R>
impl<R> UnwindSafe for EditorSource<R>where
R: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more