nvim_api/opts/
open_term.rs

1use derive_builder::Builder;
2use nvim_types::{self as nvim, Dictionary, Object};
3
4use crate::Buffer;
5use crate::ToFunction;
6
7/// Arguments passed to the callback registered to
8/// [`on_input`](OpenTermOptsBuilder::on_input). The `(a, b, c, d)` tuple
9/// represents:
10///
11/// - `a`: the string literal `"input"`;
12/// - `b`: channel id;
13/// - `c`: the [`Buffer`] associated to the terminal instance;
14/// - `d`: data input.
15pub type OnInputArgs = (
16    String,       // the string literal `"input"`
17    u32,          // channel_id
18    Buffer,       // buffer
19    nvim::String, // data input
20);
21
22#[derive(Clone, Debug, Default, Builder)]
23#[builder(default, build_fn(private, name = "fallible_build"))]
24pub struct OpenTermOpts {
25    #[builder(setter(custom))]
26    on_input: Object,
27}
28
29impl OpenTermOpts {
30    #[inline(always)]
31    /// Creates a new [`OpenTermOptsBuilder`].
32    pub fn builder() -> OpenTermOptsBuilder {
33        OpenTermOptsBuilder::default()
34    }
35}
36
37impl OpenTermOptsBuilder {
38    /// Callback invoked on data input (like keypresses in terminal mode).
39    pub fn on_input<F>(&mut self, fun: F) -> &mut Self
40    where
41        F: ToFunction<OnInputArgs, ()>,
42    {
43        self.on_input = Some(fun.to_object());
44        self
45    }
46
47    pub fn build(&mut self) -> OpenTermOpts {
48        self.fallible_build().expect("never fails, all fields have defaults")
49    }
50}
51
52impl From<&OpenTermOpts> for Dictionary {
53    fn from(opts: &OpenTermOpts) -> Self {
54        Self::from_iter([("on_input", opts.on_input.clone())])
55    }
56}