diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-10 07:08:40 +0800 |
|---|---|---|
| committer | sigoden <sigoden@gmail.com> | 2023-03-10 07:08:40 +0800 |
| commit | ff00426c2c9d7d16de9221644e4f276f7e24d74a (patch) | |
| tree | d6bc1d31a1aba4430e7d3b07adab5625290e417a /src/repl/prompt.rs | |
| parent | 4161eaa6c3ea0b6a10bd70d36f47bbbae0ae209c (diff) | |
| download | aichat-ff00426c2c9d7d16de9221644e4f276f7e24d74a.tar.gz | |
chore: improve code quality, split rep/prompt.rs from repl/init.rs
Diffstat (limited to 'src/repl/prompt.rs')
| -rw-r--r-- | src/repl/prompt.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/repl/prompt.rs b/src/repl/prompt.rs new file mode 100644 index 0000000..4c56626 --- /dev/null +++ b/src/repl/prompt.rs @@ -0,0 +1,64 @@ +use crate::config::SharedConfig; + +use reedline::{Prompt, PromptHistorySearch, PromptHistorySearchStatus}; +use std::borrow::Cow; + +const DEFAULT_MULTILINE_INDICATOR: &str = "::: "; + +#[derive(Clone)] +pub struct ReplPrompt(SharedConfig); + +impl ReplPrompt { + pub fn new(config: SharedConfig) -> Self { + Self(config) + } +} + +impl Prompt for ReplPrompt { + fn render_prompt_left(&self) -> Cow<str> { + let config = self.0.lock(); + if let Some(role) = config.role.as_ref() { + role.name.to_string().into() + } else { + Cow::Borrowed("") + } + } + + fn render_prompt_right(&self) -> Cow<str> { + let config = self.0.lock(); + if let Some(conversation) = config.conversation.as_ref() { + conversation.reamind_tokens().to_string().into() + } else { + Cow::Borrowed("") + } + } + + fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> { + let config = self.0.lock(); + if config.conversation.is_some() { + Cow::Borrowed("$") + } else { + Cow::Borrowed("〉") + } + } + + fn render_prompt_multiline_indicator(&self) -> Cow<str> { + Cow::Borrowed(DEFAULT_MULTILINE_INDICATOR) + } + + 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 + )) + } +} |
