StaticText

Struct StaticText 

Source
pub struct StaticText { /* private fields */ }
Expand description

Static, unfocusable, formatable display text. May be dependent on other form elements.

§Examples

use tty_interface::Style;

use tty_form::{
    step::CompoundStep,
    control::{Control, StaticText},
};

let mut text = StaticText::new("Hello, world!");
text.set_style(Style::new().set_bold(true));

let mut step = CompoundStep::new();
text.add_to(&mut step);

Implementations§

Source§

impl StaticText

Source

pub fn new(text: &str) -> Self

Create a new static text control with the specified content.

Examples found in repository?
examples/basic.rs (line 36)
19fn execute() -> Result<String> {
20    let mut form = Form::new();
21
22    let mut commit_summary = CompoundStep::new();
23    commit_summary.set_max_line_length(80);
24
25    SelectInput::new(
26        "Select the commit type.",
27        vec![
28            ("feat", "implemented a new feature"),
29            ("bug", "fixed existing behavior"),
30            ("docs", "added documentation"),
31            ("chore", "non-source changes"),
32        ],
33    )
34    .add_to(&mut commit_summary);
35
36    let mut opening_paren = StaticText::new("(");
37    let mut closing_paren = StaticText::new(")");
38
39    let mut scope_input = TextInput::new("Enter the commit's scope.", true);
40
41    let empty_scope = scope_input.set_evaluation(Evaluation::IsEmpty);
42    opening_paren.set_dependency(empty_scope, Action::Hide);
43    closing_paren.set_dependency(empty_scope, Action::Hide);
44
45    let mut breaking_bang = StaticText::new("!");
46    let colon = StaticText::new(": ");
47
48    let description = TextInput::new("Enter the commit's description.", true);
49
50    let mut long_description = TextBlockStep::new("Enter a long-form commit description.");
51    long_description.set_margins(Some(1), Some(1));
52    long_description.set_max_line_length(100);
53
54    let mut breaking_step = YesNoStep::new(
55        "Is this commit a breaking change?",
56        "Enter a description of the breaking change.",
57        "BREAKING CHANGE",
58    );
59
60    let trailers = KeyValueStep::new("Enter any key-value trailers, such as tickets.");
61
62    let breaking_change = breaking_step.set_evaluation(Evaluation::Equal("Yes".to_string()));
63    breaking_bang.set_dependency(breaking_change, Action::Show);
64
65    opening_paren.add_to(&mut commit_summary);
66    scope_input.add_to(&mut commit_summary);
67    closing_paren.add_to(&mut commit_summary);
68    breaking_bang.add_to(&mut commit_summary);
69    colon.add_to(&mut commit_summary);
70    description.add_to(&mut commit_summary);
71    commit_summary.add_to(&mut form);
72    long_description.add_to(&mut form);
73    trailers.add_to(&mut form);
74    breaking_step.add_to(&mut form);
75
76    let mut stdout = stdout();
77    let mut stdin = StdinDevice;
78
79    let mut interface = Interface::new_relative(&mut stdout)?;
80
81    let result = form.execute(&mut interface, &mut stdin);
82    interface.exit()?;
83
84    let mut output = String::new();
85    match result {
86        Ok(value) => output = value,
87        Err(Error::Canceled) => println!("Form canceled."),
88        Err(err) => eprintln!("Unexpected error occurred: {:?}", err),
89    }
90
91    Ok(output)
92}
Source

pub fn set_text(&mut self, text: &str)

Set the text for this control.

Source

pub fn set_style(&mut self, style: Style)

Set the optional style for this control.

Source

pub fn set_dependency(&mut self, id: DependencyId, action: Action)

Sets a dependency on the specified ID, performing some action if it evaluates true.

Examples found in repository?
examples/basic.rs (line 42)
19fn execute() -> Result<String> {
20    let mut form = Form::new();
21
22    let mut commit_summary = CompoundStep::new();
23    commit_summary.set_max_line_length(80);
24
25    SelectInput::new(
26        "Select the commit type.",
27        vec![
28            ("feat", "implemented a new feature"),
29            ("bug", "fixed existing behavior"),
30            ("docs", "added documentation"),
31            ("chore", "non-source changes"),
32        ],
33    )
34    .add_to(&mut commit_summary);
35
36    let mut opening_paren = StaticText::new("(");
37    let mut closing_paren = StaticText::new(")");
38
39    let mut scope_input = TextInput::new("Enter the commit's scope.", true);
40
41    let empty_scope = scope_input.set_evaluation(Evaluation::IsEmpty);
42    opening_paren.set_dependency(empty_scope, Action::Hide);
43    closing_paren.set_dependency(empty_scope, Action::Hide);
44
45    let mut breaking_bang = StaticText::new("!");
46    let colon = StaticText::new(": ");
47
48    let description = TextInput::new("Enter the commit's description.", true);
49
50    let mut long_description = TextBlockStep::new("Enter a long-form commit description.");
51    long_description.set_margins(Some(1), Some(1));
52    long_description.set_max_line_length(100);
53
54    let mut breaking_step = YesNoStep::new(
55        "Is this commit a breaking change?",
56        "Enter a description of the breaking change.",
57        "BREAKING CHANGE",
58    );
59
60    let trailers = KeyValueStep::new("Enter any key-value trailers, such as tickets.");
61
62    let breaking_change = breaking_step.set_evaluation(Evaluation::Equal("Yes".to_string()));
63    breaking_bang.set_dependency(breaking_change, Action::Show);
64
65    opening_paren.add_to(&mut commit_summary);
66    scope_input.add_to(&mut commit_summary);
67    closing_paren.add_to(&mut commit_summary);
68    breaking_bang.add_to(&mut commit_summary);
69    colon.add_to(&mut commit_summary);
70    description.add_to(&mut commit_summary);
71    commit_summary.add_to(&mut form);
72    long_description.add_to(&mut form);
73    trailers.add_to(&mut form);
74    breaking_step.add_to(&mut form);
75
76    let mut stdout = stdout();
77    let mut stdin = StdinDevice;
78
79    let mut interface = Interface::new_relative(&mut stdout)?;
80
81    let result = form.execute(&mut interface, &mut stdin);
82    interface.exit()?;
83
84    let mut output = String::new();
85    match result {
86        Ok(value) => output = value,
87        Err(Error::Canceled) => println!("Form canceled."),
88        Err(err) => eprintln!("Unexpected error occurred: {:?}", err),
89    }
90
91    Ok(output)
92}

Trait Implementations§

Source§

impl Control for StaticText

Source§

fn focusable(&self) -> bool

Whether this control is a focusable input.
Source§

fn update(&mut self, _input: KeyEvent)

Updates the control’s state from the given input event.
Source§

fn help(&self) -> Option<Segment>

This control’s descriptive help text, if available.
Source§

fn text(&self) -> (Segment, Option<u16>)

This control’s rendered contents and an optional offset for the cursor.
Source§

fn drawer(&self) -> Option<DrawerContents>

This control’s drawer contents, if available.
Source§

fn evaluation(&self) -> Option<(DependencyId, Evaluation)>

This control’s dependency evaluation which other controls may react to.
Source§

fn dependency(&self) -> Option<(DependencyId, Action)>

This control’s dependency which it may react to.
Source§

fn evaluate(&self, _evaluation: &Evaluation) -> bool

Perform an evaluation against this control’s current state.
Source§

fn add_to(self, step: &mut CompoundStep)

Finish configuration and add this control to the specified form step.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.