Struct tuig_ui::attachments::Textbox
source · 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
impl Textbox
sourcepub fn new(text: Vec<Text>) -> Self
pub fn new(text: Vec<Text>) -> Self
Create a new textbox containing the given text.
Examples found in repository?
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();
}
sourcepub fn scroll(self, amt: usize) -> Self
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.
sourcepub fn scroll_bottom(self, v: bool) -> Self
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.
sourcepub fn indent(self, amt: usize) -> Self
pub fn indent(self, amt: usize) -> Self
How much to indent the text.
Defaults to 0, i.e. no indent.
sourcepub fn first_indent(self, amt: usize) -> Self
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.
sourcepub fn render_to(self, sv: ScreenView<'_>) -> TextboxData
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::attach
ing 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?
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();
}