completest/
lib.rs

1//! Core types for running completions for your program
2//!
3//! See
4//! - completest-pty
5//! - completest-nu
6
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8#![warn(clippy::print_stderr)]
9#![warn(clippy::print_stdout)]
10
11/// Terminal that shell's will run completions in
12#[derive(Debug)]
13pub struct Term {
14    width: u16,
15    height: u16,
16}
17
18#[allow(missing_docs)]
19impl Term {
20    pub fn new() -> Self {
21        Self {
22            width: 120,
23            height: 60,
24        }
25    }
26
27    pub fn width(mut self, width: u16) -> Self {
28        self.width = width;
29        self
30    }
31
32    pub fn height(mut self, height: u16) -> Self {
33        self.height = height;
34        self
35    }
36
37    pub fn get_width(&self) -> u16 {
38        self.width
39    }
40
41    pub fn get_height(&self) -> u16 {
42        self.height
43    }
44}
45
46impl Default for Term {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52/// Abstract factory for [`Runtime`]
53pub trait RuntimeBuilder: std::fmt::Debug {
54    /// The [`Runtime`] being built
55    type Runtime: Runtime;
56
57    /// Name for the runtime (useful for defining a `home`)
58    fn name() -> &'static str;
59
60    /// Initialize a new runtime's home
61    fn new(
62        bin_root: std::path::PathBuf,
63        home: std::path::PathBuf,
64    ) -> std::io::Result<Self::Runtime>;
65    /// Reuse an existing runtime's home
66    fn with_home(
67        bin_root: std::path::PathBuf,
68        home: std::path::PathBuf,
69    ) -> std::io::Result<Self::Runtime>;
70}
71
72/// Run completions for a shell
73pub trait Runtime: std::fmt::Debug {
74    /// Location of the runtime's home directory
75    fn home(&self) -> &std::path::Path;
76
77    /// Register a completion script
78    fn register(&mut self, name: &str, content: &str) -> std::io::Result<()>;
79
80    /// Get the output from typing `input` into the shell
81    fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String>;
82}