aboutsummaryrefslogtreecommitdiff
path: root/examples/example-repl-basic/src/main.rs
blob: f2c871e9a0681b0319ed9355402b2438d154fd97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Example REPL Basic
//!
//! > This example demonstrates how to develop a REPL program using the `repl` feature
//!
//! Run:
//! ```bash
//! cargo run --manifest-path examples/example-repl-basic/Cargo.toml --quiet
//! ```

use mingling::{
    hook::ProgramHook,
    prelude::*,
    setup::{BasicREPLOutputSetup, BasicREPLPromptSetup, BasicREPLReadlineSetup},
    this, REPL,
};
use std::{env::current_dir, path::PathBuf};

// Resource to store the current directory
#[derive(Clone)]
struct ResCurrentDir {
    dir: PathBuf,
}

impl Default for ResCurrentDir {
    fn default() -> Self {
        Self {
            dir: current_dir().unwrap(),
        }
    }
}

fn main() {
    let mut program = ThisProgram::new();

    // Resource
    program.with_resource(ResCurrentDir::default());

    // Dispatchers
    program.with_dispatcher(CMDCd);
    program.with_dispatcher(CMDLs);
    program.with_dispatcher(CMDExit);
    program.with_dispatcher(CMDClear);

    // Setups
    // Enable basic std::io::stdin().read_line(&mut input)
    program.with_setup(BasicREPLReadlineSetup);

    // Enable basic output, using println! after Renderer finishes drawing
    program.with_setup(BasicREPLOutputSetup);

    // Enable basic Prompt display, with custom display logic
    program.with_setup(BasicREPLPromptSetup::func(|| {
        // Get the ResCurrentDir resource from the program
        let res = this::<ThisProgram>().res::<ResCurrentDir>().unwrap();
        let dir_str: String = res.dir.to_string_lossy().into();
        let prompt = format!(
            "{}> ",
            dir_str
                .replace(&['/', '\\'][..], ">")
                .trim_start_matches('>')
                .trim_end_matches('>')
        );
        prompt
    }));

    // Add hooks to handle REPL-related events
    program.with_hook(ProgramHook::empty().on_repl_begin(|| {
        // Print welcome message
        println!("Welcome!")
    }));

    // Start the REPL loop
    program.exec_repl();
}

// Create error route
pack!(ErrorDirectoryNotExist = PathBuf);

// Create commands: cd ls exit
dispatcher!("cd", CMDCd => EntryCd);
dispatcher!("ls", CMDLs => EntryLs);
dispatcher!("exit", CMDExit => EntryExit);
dispatcher!("clear", CMDClear => EntryClear);

// Define data needed for the cd command's execution phase
pack!(StateChangeDirectory = String);

// Define data needed for the ls command's rendering phase
pack!(ResultList = Vec<String>);

// Parse cd command arguments
#[chain]
fn parse_cd_args(prev: EntryCd) -> Next {
    let join = prev.pick(()).unpack();
    StateChangeDirectory::new(join)
}

// Execute directory change
#[chain]
fn handle_cd(prev: StateChangeDirectory, current_dir: &mut ResCurrentDir) -> Next {
    use just_fmt::fmt_path::fmt_path;

    let join = prev.inner;
    let new_dir = fmt_path(current_dir.dir.join(join)).unwrap_or_default();

    // If the path is not found, route to error handling
    if !new_dir.exists() {
        return ErrorDirectoryNotExist::new(new_dir).to_render();
    }

    current_dir.dir = new_dir;
    empty_result!()
}

// Get directory contents via the CurrentDir resource
#[chain]
fn handle_ls(_prev: EntryLs, current_dir: &ResCurrentDir) -> Next {
    let dir = &current_dir.dir;
    let entries: Vec<String> = std::fs::read_dir(dir)
        .into_iter()
        .flat_map(|rd| rd.filter_map(|e| e.ok()))
        .map(|e| {
            let name = e.file_name().to_string_lossy().to_string();
            if e.file_type().map(|t| t.is_dir()).unwrap_or(false) {
                format!("{}/", name)
            } else {
                name
            }
        })
        .collect();

    // Render ResultList
    ResultList::new(entries).to_render()
}

// Render ResultList data
#[renderer]
fn render_list(list: ResultList) {
    for item in list.inner {
        r_println!("{}", item)
    }
}

// Handle exit command event
#[chain]
fn handle_exit(
    _prev: EntryExit,
    repl: &mut REPL, // Import REPL resource, registered in `exec_repl`, usable directly
) {
    // Set the REPL exit flag; REPL will exit after this loop iteration
    repl.exit = true;
}

// Handle clear command event
#[chain]
fn handle_clear(_prev: EntryClear) {
    // Clear the terminal screen
    print!("\x1B[2J\x1B[1;1H");
}

// Handle path not found event
#[renderer]
fn render_error_directory_not_exist(err: ErrorDirectoryNotExist) {
    r_println!("Directory not found: {}", err.inner.display())
}

// Handle dispatcher not found event
#[renderer]
fn dispatcher_not_found(prev: DispatcherNotFound) {
    r_println!("Command not found: \"{}\"", prev.join(", "))
}

gen_program!();