pub struct Textbox { /* private fields */ }
Expand description

A box of text which can be attached to a Region.

Textboxes automatically handle:

  • Word wrapping to fit in their region
  • Indentation, including distinct first line indentation
  • Scrolling to a desired height, relative to the top or bottom

Implementations§

source§

impl Textbox

source

pub fn new(text: Vec<Text>) -> Self

Create a new textbox containing the given text.

Examples found in repository?
examples/basic-tui.rs (line 32)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
fn run(mut iosys: Box<dyn IoSystem>) {
    let mut ti = TextInput::new("> ", 5);
    let mut clicks = 0;
    let mut tui = |region: Region| {
        let [l, m, r] = region.split(cols!(20 "| |" * "#" 11)).unwrap();
        let [lt, lb] = l.split(rows!(* "=" 1)).unwrap();
        let [rt, rb] = r.split(rows!(1 "=" *)).unwrap();
        lt.attach(|i, sv| {
            let txt = text![
                "Hello! Your most recent ", red "action", " was: ",
                bold green "{:?}"(i),
            ];
            Textbox::new(txt).render_to(sv)
        });
        match lb.attach(&mut ti) {
            TextInputResult::Autocomplete { res, .. } => *res = "mlem!".into(),
            TextInputResult::Submit(line) => ti.store(line),
            _ => (),
        }
        m.attach(|i, mut sv: ScreenView| sv.fill(char_for_input(&i)));
        if rt.attach(Button("click me!").hotkey('4')) {
            clicks += 1;
        }
        rb.attach(Textbox::new(text!("{} clicks"(clicks))));
        true
    };

    let mut screen = Screen::new(iosys.size());
    let mut input = Action::Redraw;
    loop {
        screen.resize(iosys.size());
        let root = Region::new(&mut screen, input);
        if !tui(root) {
            break;
        }
        iosys.draw(&screen).expect("failed to render output");
        input = iosys.input().expect("failed to get input");
        if matches!(
            input,
            Action::Closed | Action::KeyPress { key: Key::Escape }
        ) {
            break;
        }
    }
    iosys.stop();
}
source

pub fn scroll(self, amt: usize) -> Self

Set the scroll position of the textbox, i.e. how many lines from the top or bottom should be hidden.

Defaults to 0, i.e. not scrolling at all. Anything that doesn’t fit is simply not visible.

source

pub fn scroll_bottom(self, v: bool) -> Self

Set whether the scroll position should be relative to the top or bottom.

Scrolling from the bottom will also align the bottom of the text with the bottom of the textbox, rather than aligning the tops.

Defaults to false, i.e. by default scrolling is from the top.

source

pub fn indent(self, amt: usize) -> Self

How much to indent the text.

Defaults to 0, i.e. no indent.

source

pub fn first_indent(self, amt: usize) -> Self

How much to specifically indent the first line of each paragraph.

Defaults to being the same as the indent.

source

pub fn render_to(self, sv: ScreenView<'_>) -> TextboxData

Render this textbox to a ScreenView, and return information about the render.

This is functionally equivalent to just directly Region::attaching the textbox, but you may find it useful if e.g. you want the text to depend on the input being handled in that region.

Examples found in repository?
examples/basic-tui.rs (line 32)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
fn run(mut iosys: Box<dyn IoSystem>) {
    let mut ti = TextInput::new("> ", 5);
    let mut clicks = 0;
    let mut tui = |region: Region| {
        let [l, m, r] = region.split(cols!(20 "| |" * "#" 11)).unwrap();
        let [lt, lb] = l.split(rows!(* "=" 1)).unwrap();
        let [rt, rb] = r.split(rows!(1 "=" *)).unwrap();
        lt.attach(|i, sv| {
            let txt = text![
                "Hello! Your most recent ", red "action", " was: ",
                bold green "{:?}"(i),
            ];
            Textbox::new(txt).render_to(sv)
        });
        match lb.attach(&mut ti) {
            TextInputResult::Autocomplete { res, .. } => *res = "mlem!".into(),
            TextInputResult::Submit(line) => ti.store(line),
            _ => (),
        }
        m.attach(|i, mut sv: ScreenView| sv.fill(char_for_input(&i)));
        if rt.attach(Button("click me!").hotkey('4')) {
            clicks += 1;
        }
        rb.attach(Textbox::new(text!("{} clicks"(clicks))));
        true
    };

    let mut screen = Screen::new(iosys.size());
    let mut input = Action::Redraw;
    loop {
        screen.resize(iosys.size());
        let root = Region::new(&mut screen, input);
        if !tui(root) {
            break;
        }
        iosys.draw(&screen).expect("failed to render output");
        input = iosys.input().expect("failed to get input");
        if matches!(
            input,
            Action::Closed | Action::KeyPress { key: Key::Escape }
        ) {
            break;
        }
    }
    iosys.stop();
}

Trait Implementations§

source§

impl<'s> RawAttachment<'s> for Textbox

§

type Output = TextboxData

source§

fn raw_attach(self, _: Action, screen: ScreenView<'s>) -> Self::Output

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'s, RAO, RA> Attachment<'s> for RAwhere RA: RawAttachment<'s, Output = RAO>,

§

type Output = RAO

source§

fn attach(self, region: Region<'s>) -> <RA as Attachment<'s>>::Output

source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.