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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! The `cline` crate provides an API that allows users to register CLI commands with an execute
//! and dynamic suggest callback to help implementing command line clients that support auto
//! completion of commands
//!
//! ## Getting started
//! The cline API works by creating an instance of the struct [`Cli`](struct.Cli.html) and calling
//! [`register`](struct.Cli.html#method.register), [`execute`](struct.Cli.html#method.execute) or
//! [`complete`](struct.Cli.html#method.complete) on it
//!
//! Create a new [`Cli`](struct.Cli.html) object:
//!
//! ```
//! # use cline::*;
//! let mut cli = Cli::new();
//! ```
//!
//! Register a function:
//!
//! ```
//! # use cline::*;
//! # let mut cli = Cli::new();
//! cli.register(vec!["list", "files"], | args | { println!("called with: {:?}", args); });
//! ```
//!
//! Get suggestions for autocompletion:
//!
//! ```
//! # use cline::*;
//! # let mut cli = Cli::new();
//! cli.complete("l"); //returns vec!["list"]
//! cli.complete("list"); //returns vec!["files"]
//! ```
//!
//! Execute command aka call registered exec closure:
//!
//! ```
//! # use cline::*;
//! # let mut cli = Cli::new();
//! cli.exec("list files"); //calls the registered closure
//! ```

use std::collections::HashMap;
use std::rc::Rc;
use std::str::SplitWhitespace;
use std::slice::Iter;
use std::cell::RefCell;

struct Command<'a> {
    command: Vec<&'a str>,
    exec: Box<RefCell<FnMut(Vec<&str>) + 'a>>,
    complete: Option<Box<RefCell<for <'b> FnMut(Vec<&'b str>) -> Vec<&'a str> + 'a>>>
}

impl<'a> Command<'a> {
    fn new<T: FnMut(Vec<&str>) + 'a, U: for <'b> FnMut(Vec<&'b str>) -> Vec<&'a str> + 'a>(cmd: Vec<&'a str>, exec_handler: T, complete_handler: Option<U>) -> Command<'a> {
        let mut complete_cb:Option<Box<RefCell<for <'b> FnMut(Vec<&'b str>) -> Vec<&'a str>>>> = None;
        if let Some(cb) = complete_handler {
            complete_cb = Some(Box::new(RefCell::new(cb)));
        }

        Command {
            command: cmd,
            exec: Box::new(RefCell::new(exec_handler)),
            complete: complete_cb
        }
    }
}

/// Opaque struct holding the registered Commands
pub struct Cli<'a> {
    commands: HashMap<&'a str, Cli<'a>>,
    handler: Option<Rc<Command<'a>>>
}

impl<'a> Cli<'a>{

    /// Constructs a new `Cli<'a>`
    ///
    /// This object holds all commands and callbacks registered with it
    pub fn new() -> Cli<'a> {
        Cli {
            commands: HashMap::new(),
            handler: None
        }
    }

    /// Registers a command with an exec callback
    /// When multiple commands get registered with matching prefixes, then a call to `complete`
    /// will return all "subcommands" available for the given input
    ///
    /// ```
    /// # use cline::*;
    /// # let mut cli = Cli::new();
    /// cli.register(vec!["foo", "bar"], | args | { println!("called with: {:?}", args); });
    /// // calling complete with "foo" would now return a Vec containing "bar"
    ///
    /// cli.register(vec!["foo", "baz"], | args | { println!("called with: {:?}", args); });
    /// // calling complete with "foo" would now return a Vec containing "bar" and "baz"
    /// ```
    ///
    pub fn register<T: FnMut(Vec<&str>) + 'a>(&mut self, cmd: Vec<&'a str>, exec: T) -> Result<(), ()> {
        let tmp = Rc::new(Command::new(cmd.clone(), exec, None::<for <'b> fn(Vec<&'b str>) -> Vec<&'a str>>));

        match self._register(cmd.iter(), tmp.clone()) {
            Ok(_) => Ok(()),
            Err(_) => {
                //TODO: this can only happen if we register "Vec::new()" ... probably disallow that at all
                self.handler = Some(tmp.clone());
                Err(())
            }
        }
    }

    /// Registers a command with an exec callback and a dynamic complete callback
    /// This comes in handy if the completion is based on data that can change at runtime like a
    /// list of active sessions
    ///
    /// ```
    /// # use cline::*;
    /// # let mut cli = Cli::new();
    /// cli.register_dyn_complete(vec!["foo"],
    ///     | args | { println!("called with: {:?}", args); },
    ///     | args | { vec!["bar", "baz"] });
    ///
    /// cli.complete("foo"); // --> vec!["bar", "baz"]
    /// ```
    pub fn register_dyn_complete<T: FnMut(Vec<&str>) + 'a, U: for <'b> FnMut(Vec<&'b str>) -> Vec<&'a str> + 'a>(&mut self, cmd: Vec<&'a str>, exec: T, complete: U) -> Result<(), ()> {
        let tmp = Rc::new(Command::new(cmd.clone(), exec, Some(complete)));

        match self._register(cmd.iter(), tmp.clone()) {
            Ok(_) => Ok(()),
            Err(_) => {
                //TODO: this can only happen if we register "Vec::new()" ... probably disallow that at all
                self.handler = Some(tmp.clone());
                Err(())
            }
        }
    }

    fn _register(&mut self, mut it: Iter<&'a str>, command: Rc<Command<'a>>) -> Result<(), ()> {
        if let Some(portion) = it.next() {
            if !self.commands.contains_key(portion) {
                let mut cli = Cli::new();
                if let Err(_) = cli._register(it, command.clone()) {
                    cli.handler = Some(command.clone())
                }
                self.commands.insert(portion, cli);
            } else {
                if let Some(cmd) = self.commands.get_mut(portion) {
                    if let Err(_) = cmd._register(it, command.clone()) {
                        cmd.handler = Some(command.clone())
                    }
                }
            };
            Ok(())
        } else {
            Err(())
        }
    }

    /// Returns a Vector with "subcommands" that can either complete the current work or follow the given input
    /// for registered commands
    ///
    /// ```
    /// # use cline::*;
    /// # let mut cli = Cli::new();
    /// cli.register(vec!["foo","bar"], | _ | {});
    /// assert!(vec!["foo"] == cli.complete("f"));
    /// assert!(vec!["bar"] == cli.complete("foo"));
    ///
    /// cli.register(vec!["foo","baz"], | _ | {}); // -> vec!["bar", "baz"]
    /// ```
    pub fn complete<'b>(&mut self, argv: &'b str) -> Vec<&'a str> {
        let portions = argv.trim().split_whitespace();

        match self._complete(portions) {
            Ok(ret) => ret,
            Err(_) => Vec::new()
        }
    }

    fn _complete<'b>(&mut self, mut portions: SplitWhitespace<'b>) -> Result<Vec<&'a str>, ()> {
        if let Some(ref portion) = portions.next() {
            if let Some(cmd) = self.commands.get_mut(*portion) {
                return cmd._complete(portions);
            }

            let mut ret:Vec<&str> = Vec::new();
            if let Some(ref mut handler) = self.handler {
                if let Some(ref cb) = handler.complete {
                    let mut args:Vec<&str> = Vec::new();
                    args.push(portion);
                    args.extend(portions);
                    ret.extend((&mut *cb.borrow_mut())(args.clone()));
                }
            }
            ret.extend(self.commands.keys()
                .filter(|cmd| cmd.starts_with(portion)));
            return Ok(ret);
        } else {
            let mut ret:Vec<&str> = Vec::new();
            if let Some(ref mut handler) = self.handler {
                if let Some(ref cb) = handler.complete {
                    ret.extend((&mut *cb.borrow_mut())(vec![""]).iter());
                }
            }
            ret.extend(self.commands.keys());
            return Ok(ret)
        }
    }

    /// Calls the execute callback registered with a command specified by `cmd`
    ///
    /// ```
    /// # use cline::*;
    /// # let mut cli = Cli::new();
    /// cli.register(vec!["foo"], | args | { println!("called with: {:?}", args); });
    /// cli.exec("foo");
    /// ```
    pub fn exec<'b>(&mut self, cmd: &'b str) {
        let argv:Vec<&str> = cmd.clone().split_whitespace().collect();
        let portions = cmd.trim().split_whitespace();
        self._exec(portions, argv);
    }


    //TODO: don't wanna pass through argv - do it like complete
    fn _exec<'b>(&mut self, mut portions: SplitWhitespace<'b>, argv: Vec<&str>) {
        if let Some(ref portion) = portions.next() {
            if let Some(cmd) = self.commands.get_mut(*portion) {
                cmd._exec(portions, argv);
            } else {
                if let Some(ref mut cb) = self.handler {
                    (&mut *cb.exec.borrow_mut())(argv);
                }
            }
        } else {
            if let Some(ref mut cb) = self.handler {
                (&mut *cb.exec.borrow_mut())(argv);
            }
        }
    }
}

/// Helper function that emulates linux terminal behaviour for command 
/// completion based on the commands registered with the [`Cli`](struct.Cli.html) 
/// struct passed to the function.
/// Can be exited with Ctrl + c
///
/// ```ignore
/// # use cline::{cli, cline_run};
/// # let mut cli = Cli::new();
/// cli.register(vec!["foo", "bar"], | _ | { println!("running foo bar") });
/// cli.register(vec!["foo", "baz"], | _ | { println!("running foo baz") });
///
/// cline_run(&mut cli);
/// ```
/// # Note
/// Current implementation only works on linux (`termios` based)
#[cfg(unix)]
pub fn cline_run(cli: &mut Cli) {
    unix::unix_cline_run(cli);
}
#[cfg(windows)]
pub fn cline_run(cli: &mut Cli) {
    panic!("Not yet implemented");
}

#[derive(Debug)]
pub enum Direction {
    Up,
    Down,
    Left,
    Right
}

#[derive(Debug)]
pub enum Key {
    Char(char),
    Symbol(char),
    Digit(u8),
    Arrow(Direction),
    Whitespace,
    Backspace,
    Del,
    Tab,
    Newline,
    Etx
}

#[cfg(unix)]
mod unix {
    extern crate termios;

    use std::io::{stdout, stdin, Bytes};
    use std::io::prelude::*;
    use self::termios::*;
    use super::*;

    fn read_key<T: Read>(bytes: &mut Bytes<T>) -> Option<Key> {
        match bytes.next() {
            Some(Ok(0x1B)) => { //Esc
                match bytes.next() {
                    Some(Ok(0x5b)) => { // [
                        match bytes.next() {
                            Some(Ok(0x41)) => { // A
                                Some(Key::Arrow(Direction::Up))
                            },
                            Some(Ok(0x42)) => { // B
                                Some(Key::Arrow(Direction::Down))
                            },
                            Some(Ok(0x43)) => { // C
                                Some(Key::Arrow(Direction::Right))
                            },
                            Some(Ok(0x44)) => { // D
                                Some(Key::Arrow(Direction::Left))
                            },
                            c @ _ => {
                                println!("{} {} {}", 0x1B, 0x5b, c.unwrap().unwrap());
                                None
                            }
                        }
                    },
                    c @ _ => {
                        println!("{} {}", 0x1B, c.unwrap().unwrap());
                        None
                    }
                }
            },
            Some(Ok(c)) if c >= 0x30 && c <= 0x39 => {
                Some(Key::Digit(c - 0x30))
            },
            Some(Ok(c)) if (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) => {
                Some(Key::Char(c as char))
            },
            Some(Ok(0x20)) => {
                Some(Key::Whitespace)
            },
            Some(Ok(0x7f)) => {
                Some(Key::Del)
            },
            Some(Ok(0x09)) => {
                Some(Key::Tab)
            },
            Some(Ok(0x08)) => {
                Some(Key::Backspace)
            },
            Some(Ok(0xA)) => {
                Some(Key::Newline)
            },
            Some(Ok(0x3)) => {
                Some(Key::Etx)
            },
            Some(Ok(c)) => {
                println!("{:?}", c);
                Some(Key::Symbol(c as char))
            },
            _ => {
                None
            }
        }
    }

    pub fn unix_cline_run(cli: &mut Cli) {
        let mut termios = Termios::from_fd(0).unwrap();
        let term_orig = termios;
        let mut input_iter = stdin().bytes();
        let mut command = String::new();

        termios.c_lflag &= !(ICANON | IEXTEN | ISIG | ECHO);
        tcsetattr(0, TCSANOW, &termios).unwrap();
        tcflush(0, TCIOFLUSH).unwrap();

        print!(">> ");
        stdout().flush().unwrap();

        loop {
            match read_key(&mut input_iter) {
                Some(key) => {
                    match key {
                        Key::Char(c) => {
                            command.push(c);
                            print!("{}", c);
                            stdout().flush().unwrap();
                        },
                        Key::Whitespace => {
                            command.push(' ');
                            print!(" ");
                            stdout().flush().unwrap();
                        }
                        Key::Del | Key::Backspace => {
                            command.pop();
                            print!("{} {}", 0x08 as char, 0x08 as char);
                            stdout().flush().unwrap();
                        },
                        Key::Tab => {
                            let suggestions = cli.complete(&command);
                            print!("{}", '\n');
                            for cmd in suggestions.iter() {
                                print!("{} ", cmd);
                            }
                            print!("\n>> {}", command);
                            stdout().flush().unwrap();
                        },
                        Key::Newline => {
                            println!("");
                            cli.exec(&command);
                            command.clear();
                            print!(">> ");
                            stdout().flush().unwrap();
                        },
                        Key::Etx => { //Ctrl + C
                            println!("");
                            break;
                        }
                        x @ _ => {
                            println!("unhandled: {:?}", x);
                        }
                    }
                },
                None => {
                    break;
                }
            }
        }
        tcsetattr(0, termios::TCSANOW, &term_orig).unwrap();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_register_and_execute() {
        let mut called = false;
        {
            let mut cli = Cli::new();
            cli.register(vec!["foo", "bar"], | _ | { called=true }).ok();
            cli.exec("foo bar");
        }
        assert!(called == true)
    }

    #[test]
    fn test_register_and_execute_multiple_times() {
        let mut called = 0u8;
        {
            let mut cli = Cli::new();
            cli.register(vec!["foo", "bar"], | _ | { called = called + 1} ).ok();
            cli.exec("foo bar");
            cli.exec("foo bar");
        }
        assert!(called == 2)
    }

    #[test]
    fn test_complete_empty_single_cmd() {
        let mut cli = Cli::new();
        cli.register(vec!["foo"], | _ | { } ).ok();
        assert!(vec!["foo"] == cli.complete(""));
        assert!(vec!["foo"] == cli.complete("f"));
        assert!(vec!["foo"] == cli.complete("  f"));
        //assert!(vec![""] == cli.complete("f   "));  //FIXME: this test should pass
        assert!(cli.complete("foo").is_empty());
    }

    #[test]
    fn test_complete_with_dynamic() {
        let mut cli = Cli::new();
        cli.register_dyn_complete(vec!["foo"], | _ | { }, | _ | {
            vec!["bar", "baz"]
        }).ok();
        assert!(vec!["foo"] == cli.complete("f"));
        assert!(vec!["bar", "baz"] == cli.complete("foo a b"));
    }

    #[test]
    fn test_complete_multi_cmd() {
        let mut cli = Cli::new();
        cli.register(vec!["foo", "bar"], | _| { } ).ok();
        assert!(vec!["foo"] == cli.complete("f"));
        assert!(vec!["bar"] == cli.complete("foo"));
        assert!(vec!["bar"] == cli.complete("foo b"));
    }

    #[test]
    fn test_register_and_execute_with_arguments() {
        let mut called = false;
        {
            let mut cli = Cli::new();
            cli.register(vec!["foo"], | args | {
                called=true;
                assert!(vec!["foo", "bar", "baz"] == args);
            } ).ok();
            cli.exec("foo bar baz");
        }
        assert!(called == true);
    }
}