tui_commander/
commander.rs

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::collections::HashMap;

use crate::command::CommandBox;
use crate::Command;

pub struct Commander<Context> {
    command_builders: HashMap<&'static str, CommandFuncs<Context>>,

    command_str: String,

    search_engine: nucleo_matcher::Matcher,
    active: bool,
}

impl<Context> Commander<Context>
where
    Context: crate::Context,
{
    pub fn builder() -> CommanderBuilder<Context> {
        CommanderBuilder {
            case_sensitive: false,
            command_builders: HashMap::new(),
        }
    }

    #[inline]
    pub fn is_active(&self) -> bool {
        self.active
    }

    #[inline]
    pub fn start(&mut self) {
        self.active = true
    }

    #[inline]
    pub fn reset(&mut self) {
        self.active = false
    }

    pub fn suggestions(&mut self) -> Vec<String> {
        let commands = self
            .command_names()
            .map(ToString::to_string)
            .collect::<Vec<String>>();

        let Some((command, _args)) = self.get_command_args() else {
            return commands;
        };

        nucleo_matcher::pattern::Pattern::new(
            command,
            nucleo_matcher::pattern::CaseMatching::Ignore,
            nucleo_matcher::pattern::Normalization::Never,
            nucleo_matcher::pattern::AtomKind::Fuzzy,
        )
        .match_list(commands, &mut self.search_engine)
        .into_iter()
        .map(|tpl| tpl.0)
        .map(|s| s.to_string())
        .collect::<Vec<String>>()
    }

    fn command_names(&self) -> impl Iterator<Item = &str> {
        self.command_builders.keys().copied()
    }

    pub fn is_unknown_command(&mut self) -> bool {
        self.suggestions().is_empty()
    }

    pub fn set_input(&mut self, input: String) {
        self.command_str = input;
    }

    pub fn execute(&mut self, context: &mut Context) -> Result<(), CommanderError> {
        let Some((command, args)) = self.get_command_args() else {
            return Err(CommanderError::EmptyCommand);
        };

        let Some(command_funcs) = self.command_builders.get(command) else {
            return Err(CommanderError::UnknownCommand(self.command_str.clone()));
        };

        let commandbox = (command_funcs.builder)(command)?;
        let args = args.into_iter().map(ToString::to_string).collect();
        commandbox
            .0
            .execute(args, context)
            .map_err(CommanderError::Command)
    }

    fn find_command_funcs_for_command(
        &self,
        command: &str,
    ) -> Result<&CommandFuncs<Context>, CommanderError> {
        self.command_builders
            .get(command)
            .ok_or_else(|| CommanderError::UnknownCommand(self.command_str.clone()))
    }

    fn get_command_args(&self) -> Option<(&str, Vec<&str>)> {
        let mut it = self.command_str.split(' ');
        let command = it.next()?;
        let args = it.collect();
        Some((command, args))
    }

    pub(crate) fn current_args_are_valid(&self) -> Result<bool, CommanderError> {
        let Some((command, args)) = self.get_command_args() else {
            return Err(CommanderError::EmptyCommand);
        };

        let funcs = self.find_command_funcs_for_command(command)?;
        Ok((funcs.arg_validator)(&args))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum CommanderError {
    #[error("Command execution errored")]
    Command(Box<dyn std::error::Error + Send + Sync + 'static>),

    #[error("Empty command string")]
    EmptyCommand,

    #[error("Unknown command {}", .0)]
    UnknownCommand(String),
}

struct CommandFuncs<Context> {
    builder: CommandBuilderFn<Context>,
    arg_validator: CommandArgValidatorFn,
}

type CommandBuilderFn<Context> = Box<dyn Fn(&str) -> Result<CommandBox<Context>, CommanderError>>;
type CommandArgValidatorFn = Box<dyn Fn(&[&str]) -> bool>;

pub struct CommanderBuilder<Context> {
    case_sensitive: bool,
    command_builders: HashMap<&'static str, CommandFuncs<Context>>,
}

impl<Context> CommanderBuilder<Context> {
    pub fn with_case_sensitive(mut self, b: bool) -> Self {
        self.case_sensitive = b;
        self
    }

    pub fn with_command<C>(mut self) -> Self
    where
        C: Command<Context> + Send + Sync + 'static,
        Context: 'static,
    {
        fn command_builder<C, Context>(input: &str) -> Result<CommandBox<Context>, CommanderError>
        where
            C: Command<Context> + Send + Sync + 'static,
            Context: 'static,
        {
            C::build_from_command_name_str(input)
                .map(|c| CommandBox(Box::new(c) as Box<dyn Command<Context>>))
                .map_err(CommanderError::Command)
        }

        fn arg_validator<C, Context>(args: &[&str]) -> bool
        where
            C: Command<Context> + Send + Sync + 'static,
            Context: 'static,
        {
            C::args_are_valid(args)
        }

        self.command_builders.insert(
            C::name(),
            CommandFuncs {
                builder: Box::new(command_builder::<C, Context>),
                arg_validator: Box::new(arg_validator::<C, Context>),
            },
        );
        self
    }

    pub fn build(mut self) -> Commander<Context> {
        self.command_builders.shrink_to_fit();
        let search_engine = nucleo_matcher::Matcher::new({
            let mut config = nucleo_matcher::Config::DEFAULT;
            config.ignore_case = !self.case_sensitive;
            config.prefer_prefix = true;
            config
        });

        Commander {
            command_builders: self.command_builders,

            active: false,
            search_engine,
            command_str: String::new(),
        }
    }
}