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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * config.rs is part of slaps.
 * Copyright (C) 2020 rd <slaps@megane.space>
 * License: WTFPL
 */

use ansi_term::{Color, Style};

/// Configuration for the internal sub-components of slaps.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Config<'a> {
    /// Colorization mode.
    pub color_mode: ColorMode,
    /// Tab-completion type.
    pub completion_type: CompletionType,
    /// Prompt for input.
    pub prompt: &'a str,
    /// Prompt for passwords.
    pub password_prompt: &'a str,
    /// Template for the root help command.
    pub help_template: &'a str,
    /// Help template for subcommands with arguments.
    pub subcommand_help_template: &'a str,
    /// Help template for subcommands without arguments.
    pub subcommand_help_template_no_args: &'a str,
    /// Style to use to highlight commands.
    pub highlighter_style_command: Style,
    /// Style to use to highlight arguments.
    pub highlighter_style_argument: Style,
    /// Style to use to highlight values.
    pub highlighter_style_value: Style,
    /// Style to use to highlight mismatched quotes.
    pub highlighter_style_mismatched_quotes: Style,
    /// Whether to underline the word currently under cursor while editing.
    pub highlighter_underline_word_under_cursor: bool,
}

impl<'a> Config<'a> {
    /// Forces colorization on or off.
    ///
    /// By default, colorization is on except if stdout is not a TTY.
    ///
    /// # Examples
    /// ```
    /// use slaps::{Config, ColorMode};
    ///
    /// let config = Config::default().color_mode(ColorMode::Disabled);
    /// # assert_eq!(config.color_mode, ColorMode::Disabled);
    /// ```
    #[must_use]
    pub fn color_mode(mut self, color_mode: ColorMode) -> Self {
        self.color_mode = color_mode;
        self
    }

    /// Sets the tab completion type.
    ///
    /// # Examples
    /// ```
    /// use slaps::{Config, CompletionType};
    ///
    /// let config = Config::default().completion_type(CompletionType::List);
    /// # assert_eq!(config.completion_type, CompletionType::List);
    /// ```
    #[must_use]
    pub fn completion_type(mut self, completion_mode: CompletionType) -> Self {
        self.completion_type = completion_mode;
        self
    }

    /// Sets the string used to prompt for input.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    ///
    /// let config = Config::default().set_prompt(" >>> ");
    /// # assert_eq!(config.prompt, " >>> ");
    /// ```
    #[must_use]
    pub fn set_prompt(mut self, prompt: &'a str) -> Self {
        self.prompt = prompt;
        self
    }

    /// Sets the string used to prompt for passwords.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    ///
    /// let config = Config::default().set_password_prompt("Password? ");
    /// # assert_eq!(config.password_prompt, "Password? ");
    /// ```
    #[must_use]
    pub fn set_password_prompt(mut self, prompt: &'a str) -> Self {
        self.password_prompt = prompt;
        self
    }

    /// Sets the [template] for the root help command.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    ///
    /// let config = Config::default().help_template("Subcommands: {subcommands}");
    /// # assert_eq!(config.help_template, "Subcommands: {subcommands}");
    /// ```
    ///
    /// [template]: clap::App::template
    #[must_use]
    pub fn help_template(mut self, template: &'a str) -> Self {
        self.help_template = template;
        self
    }

    /// Sets the help [template] for subcommands with arguments.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    ///
    /// let config = Config::default().subcommand_help_template("Usage: {usage}");
    /// # assert_eq!(config.subcommand_help_template, "Usage: {usage}");
    /// ```
    ///
    /// [template]: clap::App::template
    #[must_use]
    pub fn subcommand_help_template(mut self, template: &'a str) -> Self {
        self.subcommand_help_template = template;
        self
    }

    /// Sets the help [template] for subcommands without arguments.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    ///
    /// let config = Config::default().subcommand_help_template_no_args("About: {about}");
    /// # assert_eq!(config.subcommand_help_template_no_args, "About: {about}");
    /// ```
    ///
    /// [template]: clap::App::template
    #[must_use]
    pub fn subcommand_help_template_no_args(mut self, template: &'a str) -> Self {
        self.subcommand_help_template_no_args = template;
        self
    }

    /// Sets the [`Style`] used to highlight commands and subcommands.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    /// use ansi_term::Color;
    ///
    /// let config = Config::default().highlighter_command_style(Color::Blue.normal());
    /// # assert_eq!(config.highlighter_style_command, Color::Blue.normal());
    /// ```
    #[must_use]
    pub fn highlighter_command_style(mut self, style: Style) -> Self {
        self.highlighter_style_command = style;
        self
    }

    /// Sets the [`Style`] used to highlight arguments.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    /// use ansi_term::Color;
    ///
    /// let config = Config::default().highlighter_argument_style(Color::Blue.normal());
    /// # assert_eq!(config.highlighter_style_argument, Color::Blue.normal());
    /// ```
    #[must_use]
    pub fn highlighter_argument_style(mut self, style: Style) -> Self {
        self.highlighter_style_argument = style;
        self
    }

    /// Sets the [`Style`] used to highlight values for positional and named arguments.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    /// use ansi_term::Color;
    ///
    /// let config = Config::default().highlighter_value_style(Color::Blue.normal());
    /// # assert_eq!(config.highlighter_style_value, Color::Blue.normal());
    /// ```
    #[must_use]
    pub fn highlighter_value_style(mut self, style: Style) -> Self {
        self.highlighter_style_value = style;
        self
    }

    /// Sets the [`Style`] used to highlight unclosed quotation marks.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    /// use ansi_term::Color;
    ///
    /// let config = Config::default().highlighter_mismatched_quote_style(Color::Blue.normal());
    /// # assert_eq!(config.highlighter_style_mismatched_quotes, Color::Blue.normal());
    /// ```
    #[must_use]
    pub fn highlighter_mismatched_quote_style(mut self, style: Style) -> Self {
        self.highlighter_style_mismatched_quotes = style;
        self
    }

    /// Enables or disables underlining the word currently under cursor by the input highlighter.
    ///
    /// # Examples
    /// ```
    /// use slaps::Config;
    /// use ansi_term::Color;
    ///
    /// let config = Config::default().highlighter_underline_word_under_cursor(false);
    /// # assert_eq!(config.highlighter_underline_word_under_cursor, false);
    /// ```
    #[must_use]
    pub fn highlighter_underline_word_under_cursor(mut self, underline: bool) -> Self {
        self.highlighter_underline_word_under_cursor = underline;
        self
    }
}

impl Default for Config<'_> {
    fn default() -> Self {
        Config {
            color_mode: ColorMode::default(),
            completion_type: CompletionType::default(),
            prompt: "> ",
            password_prompt: "Password: ",
            help_template: "{subcommands}",
            subcommand_help_template: "{usage}\n{about}\n\n{all-args}",
            subcommand_help_template_no_args: "{about}",
            highlighter_style_command: Color::Yellow.bold(),
            highlighter_style_argument: Color::Purple.bold(),
            highlighter_style_mismatched_quotes: Color::Red.normal(),
            highlighter_style_value: Color::Green.normal(),
            highlighter_underline_word_under_cursor: true,
        }
    }
}

/// Colorization mode.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ColorMode {
    /// Activate highlighting even if platform is not supported (windows < 10).
    Forced,
    /// Activate highlighting if platform/terminal is supported.
    Enabled,
    /// Deactivate highlighting even if platform/terminal is supported.
    Disabled,
}

impl Default for ColorMode {
    fn default() -> Self {
        ColorMode::Enabled
    }
}

/// Tab completion style.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CompletionType {
    /// Complete the next full match (like in Vim by default).
    Circular,
    /// Complete till longest match.
    /// When more than one match, list all matches
    /// (like in Bash/Readline).
    List,
}

impl Default for CompletionType {
    fn default() -> Self {
        CompletionType::Circular
    }
}

#[cfg(test)]
mod tests {
    use crate::{ColorMode, CompletionType, Config};
    use ansi_term::Color;

    #[test]
    fn test_default_config() {
        assert_eq!(
            Config::default(),
            Config {
                color_mode: ColorMode::Enabled,
                completion_type: CompletionType::Circular,
                prompt: "> ",
                password_prompt: "Password: ",
                help_template: "{subcommands}",
                subcommand_help_template: "{usage}\n{about}\n\n{all-args}",
                subcommand_help_template_no_args: "{about}",
                highlighter_style_command: Color::Yellow.bold(),
                highlighter_style_argument: Color::Purple.bold(),
                highlighter_style_mismatched_quotes: Color::Red.normal(),
                highlighter_style_value: Color::Green.normal(),
                highlighter_underline_word_under_cursor: true,
            }
        );
    }
}