Struct requestty::question::EditorBuilder[][src]

pub struct EditorBuilder<'a> { /* fields omitted */ }
Expand description

The builder for the editor prompt.

Once the user exits their editor, the contents of the temporary file are read in as the result. The editor to use is determined by the $VISUAL or $EDITOR environment variables. If neither of those are present, vim (for unix) or notepad (for windows) is used.

See the various methods for more details on each available option.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .message("Please enter a short description about yourself")
    .extension(".md")
    .build();

Implementations

The message to display when the prompt is rendered in the terminal.

It can be either a String or a FnOnce that returns a String. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, the message defaults to “<name>: “.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .message("Please enter a short description about yourself")
    .build();

Whether to ask the question (true) or not (false).

It can be either a bool or a FnOnce that returns a bool. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, it defaults to true.

Examples

use requestty::{Question, Answers};

let editor = Question::editor("description")
    .when(|previous_answers: &Answers| match previous_answers.get("anonymous") {
        Some(ans) => !ans.as_bool().unwrap(),
        None => true,
    })
    .build();

Prompt the question even if it is answered.

By default if an answer with the given name already exists, the question will be skipped. This can be overridden by setting ask_if_answered is set to true.

If this is not given, it defaults to false.

If you need to dynamically decide whether the question should be asked, use when.

Examples

use requestty::{Question, Answers};

let editor = Question::editor("description")
    .ask_if_answered(true)
    .build();

Set a default value for the file

If set, when the user first opens the file, it will contain the default value. Subsequent times will contain what was last written.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .default("My name is ")
    .build();

Set an extension on the temporary file

If set, the extension will be concatenated with the randomly generated filename. This is a useful way to signify accepted styles of input, and provide syntax highlighting on supported editors.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .extension(".md")
    .build();

Function to change the final submitted value before it is displayed to the user and added to the Answers.

It is a FnOnce that is given the answer and the previous Answers, and should return the new answer.

This will be called after the answer has been validated.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .filter(|description, previous_answers| parse_markdown(description))
    .build();

Function to validate the submitted value before it’s returned.

It is a FnMut that is given the answer and the previous Answers, and should return Ok(()) if the given answer is valid. If it is invalid, it should return an Err with the error message to display to the user.

This will be called when the user presses the Enter key.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .validate(|description, previous_answers| if description.lines().count() >= 2 {
        Ok(())
    } else {
        Err("Please enter a few lines".to_owned())
    })
    .build();

Change the way the answer looks when displayed to the user.

It is a FnOnce that is given the answer, previous Answers and the Backend to display the answer on. After the transform is called, a new line is also added.

It will only be called once the user finishes answering the question.

Examples

use requestty::Question;

let editor = Question::editor("description")
    .transform(|description, previous_answers, backend| {
        write!(backend, "\n{}", description)
    })
    .build();

Consumes the builder returning a Question

Trait Implementations

Formats the value using the given formatter. Read more

Consumes the builder returning a Question

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.