linefeed/function.rs
1//! Provides the `Function` trait for implementing custom `Prompter` commands
2
3use std::io;
4
5use crate::command::Category;
6use crate::prompter::Prompter;
7use crate::terminal::Terminal;
8
9/// Implements custom functionality for a `Prompter` command
10pub trait Function<Term: Terminal>: Send + Sync {
11 /// Executes the function.
12 ///
13 /// `count` is the numerical argument supplied by the user; `1` by default.
14 /// `prompter.explicit_arg()` may be called to determine whether this value
15 /// was explicitly supplied by the user.
16 ///
17 /// `ch` is the final character of the sequence that triggered the command.
18 /// `prompter.sequence()` may be called to determine the full sequence that
19 /// triggered the command.
20 fn execute(&self, prompter: &mut Prompter<Term>, count: i32, ch: char) -> io::Result<()>;
21
22 /// Returns the command category.
23 fn category(&self) -> Category { Category::Other }
24}
25
26impl<F, Term: Terminal> Function<Term> for F where
27 F: Send + Sync,
28 F: Fn(&mut Prompter<Term>, i32, char) -> io::Result<()> {
29 fn execute(&self, prompter: &mut Prompter<Term>, count: i32, ch: char) -> io::Result<()> {
30 self(prompter, count, ch)
31 }
32}