Skip to main content

reedline/prompt/
base.rs

1use {
2    crossterm::style::Color,
3    serde::{Deserialize, Serialize},
4    std::{
5        borrow::Cow,
6        fmt::{Display, Formatter},
7    },
8    strum::EnumIter,
9};
10
11/// The default color for the prompt, indicator, and right prompt
12pub static DEFAULT_PROMPT_COLOR: Color = Color::Green;
13pub static DEFAULT_PROMPT_MULTILINE_COLOR: nu_ansi_term::Color = nu_ansi_term::Color::LightBlue;
14pub static DEFAULT_INDICATOR_COLOR: Color = Color::Cyan;
15pub static DEFAULT_PROMPT_RIGHT_COLOR: Color = Color::AnsiValue(5);
16
17/// The current success/failure of the history search
18pub enum PromptHistorySearchStatus {
19    /// Success for the search
20    Passing,
21
22    /// Failure to find the search
23    Failing,
24}
25
26/// A representation of the history search
27pub struct PromptHistorySearch {
28    /// The status of the search
29    pub status: PromptHistorySearchStatus,
30
31    /// The search term used during the search
32    pub term: String,
33}
34
35impl PromptHistorySearch {
36    /// A constructor to create a history search
37    pub const fn new(status: PromptHistorySearchStatus, search_term: String) -> Self {
38        PromptHistorySearch {
39            status,
40            term: search_term,
41        }
42    }
43}
44
45/// Modes that the prompt can be in
46#[derive(Serialize, Deserialize, Clone, Debug, EnumIter, Default)]
47pub enum PromptEditMode {
48    /// The default mode
49    #[default]
50    Default,
51
52    /// Emacs normal mode
53    Emacs,
54
55    /// A vi-specific mode
56    Vi(PromptViMode),
57
58    /// A custom mode
59    Custom(String),
60}
61
62/// The vi-specific modes that the prompt can be in
63#[derive(Serialize, Deserialize, Clone, Debug, EnumIter, Default)]
64pub enum PromptViMode {
65    /// The default mode
66    #[default]
67    Normal,
68
69    /// Insertion mode
70    Insert,
71}
72
73impl Display for PromptEditMode {
74    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
75        match self {
76            PromptEditMode::Default => write!(f, "Default"),
77            PromptEditMode::Emacs => write!(f, "Emacs"),
78            PromptEditMode::Vi(_) => write!(f, "Vi_Normal\nVi_Insert"),
79            PromptEditMode::Custom(s) => write!(f, "Custom_{s}"),
80        }
81    }
82}
83/// API to provide a custom prompt.
84///
85/// Implementors have to provide [`str`]-based content which will be
86/// displayed before the `LineBuffer` is drawn.
87pub trait Prompt: Send {
88    /// Provide content of the left full prompt
89    fn render_prompt_left(&self) -> Cow<'_, str>;
90    /// Provide content of the right full prompt
91    fn render_prompt_right(&self) -> Cow<'_, str>;
92    /// Render the prompt indicator (Last part of the prompt that changes based on the editor mode)
93    fn render_prompt_indicator(&self, prompt_mode: PromptEditMode) -> Cow<'_, str>;
94    /// Indicator to show before explicit new lines
95    fn render_prompt_multiline_indicator(&self) -> Cow<'_, str>;
96    /// Render the prompt indicator for `Ctrl-R` history search
97    fn render_prompt_history_search_indicator(
98        &self,
99        history_search: PromptHistorySearch,
100    ) -> Cow<'_, str>;
101    /// Get the default prompt color
102    fn get_prompt_color(&self) -> Color {
103        DEFAULT_PROMPT_COLOR
104    }
105    /// Get the default multiline prompt color
106    fn get_prompt_multiline_color(&self) -> nu_ansi_term::Color {
107        DEFAULT_PROMPT_MULTILINE_COLOR
108    }
109    /// Get the default indicator color
110    fn get_indicator_color(&self) -> Color {
111        DEFAULT_INDICATOR_COLOR
112    }
113    /// Get the default right prompt color
114    fn get_prompt_right_color(&self) -> Color {
115        DEFAULT_PROMPT_RIGHT_COLOR
116    }
117
118    /// Whether to render right prompt on the last line
119    fn right_prompt_on_last_line(&self) -> bool {
120        false
121    }
122}