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
//! Provides the `Function` trait for implementing custom `Prompter` commands

use std::io;

use command::Category;
use prompter::Prompter;
use terminal::Terminal;

/// Implements custom functionality for a `Prompter` command
pub trait Function<Term: Terminal>: Send + Sync {
    /// Executes the function.
    ///
    /// `count` is the numerical argument supplied by the user; `1` by default.
    /// `prompter.explicit_arg()` may be called to determine whether this value
    /// was explicitly supplied by the user.
    ///
    /// `ch` is the final character of the sequence that triggered the command.
    /// `prompter.sequence()` may be called to determine the full sequence that
    /// triggered the command.
    fn execute(&self, prompter: &mut Prompter<Term>, count: i32, ch: char) -> io::Result<()>;

    /// Returns the command category.
    fn category(&self) -> Category { Category::Other }
}

impl<F, Term: Terminal> Function<Term> for F where
        F: Send + Sync,
        F: Fn(&mut Prompter<Term>, i32, char) -> io::Result<()> {
    fn execute(&self, prompter: &mut Prompter<Term>, count: i32, ch: char) -> io::Result<()> {
        self(prompter, count, ch)
    }
}