summaryrefslogtreecommitdiffstats
path: root/src/repl/prompt.rs
blob: 8c670f401909beb1a1be1c60094276f87bb8e1c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::config::SharedConfig;

use crossterm::style::Color;
use reedline::{Prompt, PromptHistorySearch, PromptHistorySearchStatus};
use std::borrow::Cow;

const PROMPT_COLOR: Color = Color::Green;
const PROMPT_MULTILINE_COLOR: nu_ansi_term::Color = nu_ansi_term::Color::LightBlue;
const INDICATOR_COLOR: Color = Color::Cyan;
const PROMPT_RIGHT_COLOR: Color = Color::AnsiValue(5);

#[derive(Clone)]
pub struct ReplPrompt {
    config: SharedConfig,
    prompt_color: Color,
    prompt_multiline_color: nu_ansi_term::Color,
    indicator_color: Color,
    prompt_right_color: Color,
}

impl ReplPrompt {
    pub fn new(config: SharedConfig) -> Self {
        let (prompt_color, prompt_multiline_color, indicator_color, prompt_right_color) =
            Self::get_colors(config.clone());
        Self {
            config,
            prompt_color,
            prompt_multiline_color,
            indicator_color,
            prompt_right_color,
        }
    }
    pub fn sync_config(&mut self) {
        let (prompt_color, prompt_multiline_color, indicator_color, prompt_right_color) =
            Self::get_colors(self.config.clone());
        self.prompt_color = prompt_color;
        self.prompt_multiline_color = prompt_multiline_color;
        self.indicator_color = indicator_color;
        self.prompt_right_color = prompt_right_color;
    }

    pub fn get_colors(config: SharedConfig) -> (Color, nu_ansi_term::Color, Color, Color) {
        let (highlight, light_theme) = config.read().get_render_options();
        if highlight {
            (
                PROMPT_COLOR,
                PROMPT_MULTILINE_COLOR,
                INDICATOR_COLOR,
                PROMPT_RIGHT_COLOR,
            )
        } else if light_theme {
            (
                Color::Black,
                nu_ansi_term::Color::Black,
                Color::Black,
                Color::Black,
            )
        } else {
            (
                Color::White,
                nu_ansi_term::Color::White,
                Color::White,
                Color::White,
            )
        }
    }
}

impl Prompt for ReplPrompt {
    fn render_prompt_left(&self) -> Cow<str> {
        if let Some(role) = self.config.read().role.as_ref() {
            role.name.to_string().into()
        } else {
            Cow::Borrowed("")
        }
    }

    fn render_prompt_right(&self) -> Cow<str> {
        if self.config.read().conversation.is_none() {
            Cow::Borrowed("")
        } else {
            self.config.read().get_reamind_tokens().to_string().into()
        }
    }

    fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> {
        if self.config.read().conversation.is_some() {
            Cow::Borrowed("$")
        } else {
            Cow::Borrowed("〉")
        }
    }

    fn render_prompt_multiline_indicator(&self) -> Cow<str> {
        Cow::Borrowed("")
    }

    fn render_prompt_history_search_indicator(
        &self,
        history_search: PromptHistorySearch,
    ) -> Cow<str> {
        let prefix = match history_search.status {
            PromptHistorySearchStatus::Passing => "",
            PromptHistorySearchStatus::Failing => "failing ",
        };
        // NOTE: magic strings, given there is logic on how these compose I am not sure if it
        // is worth extracting in to static constant
        Cow::Owned(format!(
            "({}reverse-search: {}) ",
            prefix, history_search.term
        ))
    }

    fn get_prompt_color(&self) -> Color {
        self.prompt_color
    }
    /// Get the default multilince prompt color
    fn get_prompt_multiline_color(&self) -> nu_ansi_term::Color {
        self.prompt_multiline_color
    }
    /// Get the default indicator color
    fn get_indicator_color(&self) -> Color {
        self.indicator_color
    }
    /// Get the default right prompt color
    fn get_prompt_right_color(&self) -> Color {
        self.prompt_right_color
    }
}