cli_printer/examples/
combine.rs

1use std::{cell::RefCell, io, rc::Rc};
2
3use crate::{
4    core::{
5        interfaces::WidgetRoot,
6        utils::{Action, IconAndLabel, RenderWidget},
7        view::SectionsView,
8    },
9    styles::{ICON_CHECK, ICON_QUESTION},
10    widgets::{Input, ListSelected, TextBlock},
11};
12
13#[derive(Clone, Debug)]
14struct GlobalState {
15    pub option_list: Option<String>,
16    pub input_user: Option<String>,
17}
18
19pub fn combine_widgets() -> io::Result<()> {
20    let mut stdout = io::stdout();
21    let options = vec!["Option1", "Option2", "Option3", "Nothing"];
22
23    let mut list_selected: ListSelected<Rc<RefCell<GlobalState>>> = ListSelected::new(options);
24    list_selected.add_text_init("? ", "Choose an option: ");
25    list_selected.add_text_final("√ ", "Option selected: ");
26
27    list_selected.after(|list_state, context_state| {
28        if list_state.is_selected {
29            if list_state.offset == list_state.length - 1 {
30                context_state.borrow_mut().option_list = None;
31                return Action::Exit;
32            }
33            context_state.borrow_mut().option_list = list_state.current_option.clone();
34            return Action::Next;
35        }
36        Action::KeepSection
37    });
38
39    let mut input_user: Input<Rc<RefCell<GlobalState>>> = Input::new(
40        IconAndLabel(ICON_QUESTION, "Write your name: "),
41        IconAndLabel(ICON_CHECK, "Your name is: "),
42    );
43
44    input_user.after(|this_input, context_state| {
45        if this_input.complete_input && this_input.input.len() > 0 {
46            context_state.borrow_mut().input_user = Some(this_input.input.to_string());
47            return Action::Next;
48        }
49        Action::KeepSection
50    });
51
52    let mut section_list = SectionsView::new(GlobalState {
53        option_list: None,
54        input_user: None,
55    });
56
57    let mut text1: TextBlock<Rc<RefCell<GlobalState>>> = TextBlock::new("Removing: ");
58    text1.before(|local_state, global_state| {
59        let before_content = global_state
60            .borrow()
61            .option_list
62            .as_ref()
63            .unwrap()
64            .to_owned();
65
66        local_state.text.push_str(&before_content);
67        RenderWidget::Yes
68    });
69
70    section_list.child(input_user);
71    section_list.child(list_selected);
72    section_list.child(text1);
73    section_list.render(&mut stdout)?;
74    Ok(())
75}