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_macros::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)]
47pub enum PromptEditMode {
48    /// The default mode
49    Default,
50
51    /// Emacs normal mode
52    Emacs,
53
54    /// A vi-specific mode
55    Vi(PromptViMode),
56
57    /// A custom mode
58    Custom(String),
59}
60
61/// The vi-specific modes that the prompt can be in
62#[derive(Serialize, Deserialize, Clone, Debug, EnumIter, Default)]
63pub enum PromptViMode {
64    /// The default mode
65    #[default]
66    Normal,
67
68    /// Insertion mode
69    Insert,
70}
71
72impl Display for PromptEditMode {
73    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
74        match self {
75            PromptEditMode::Default => write!(f, "Default"),
76            PromptEditMode::Emacs => write!(f, "Emacs"),
77            PromptEditMode::Vi(_) => write!(f, "Vi_Normal\nVi_Insert"),
78            PromptEditMode::Custom(s) => write!(f, "Custom_{s}"),
79        }
80    }
81}
82/// API to provide a custom prompt.
83///
84/// Implementors have to provide [`str`]-based content which will be
85/// displayed before the `LineBuffer` is drawn.
86pub trait Prompt: Send {
87    /// Provide content of the left full prompt
88    fn render_prompt_left(&self) -> Cow<str>;
89    /// Provide content of the right full prompt
90    fn render_prompt_right(&self) -> Cow<str>;
91    /// Render the prompt indicator (Last part of the prompt that changes based on the editor mode)
92    fn render_prompt_indicator(&self, prompt_mode: PromptEditMode) -> Cow<str>;
93    /// Indicator to show before explicit new lines
94    fn render_prompt_multiline_indicator(&self) -> Cow<str>;
95    /// Render the prompt indicator for `Ctrl-R` history search
96    fn render_prompt_history_search_indicator(
97        &self,
98        history_search: PromptHistorySearch,
99    ) -> Cow<str>;
100    /// Get the default prompt color
101    fn get_prompt_color(&self) -> Color {
102        DEFAULT_PROMPT_COLOR
103    }
104    /// Get the default multiline prompt color
105    fn get_prompt_multiline_color(&self) -> nu_ansi_term::Color {
106        DEFAULT_PROMPT_MULTILINE_COLOR
107    }
108    /// Get the default indicator color
109    fn get_indicator_color(&self) -> Color {
110        DEFAULT_INDICATOR_COLOR
111    }
112    /// Get the default right prompt color
113    fn get_prompt_right_color(&self) -> Color {
114        DEFAULT_PROMPT_RIGHT_COLOR
115    }
116
117    /// Whether to render right prompt on the last line
118    fn right_prompt_on_last_line(&self) -> bool {
119        false
120    }
121}