mirror of
https://github.com/sigoden/aichat
synced 2024-11-18 09:28:27 +00:00
parent
e78b5f7c58
commit
a7f2da156c
@ -1,16 +1,23 @@
|
|||||||
use super::Repl;
|
|
||||||
use super::REPL_COMMANDS;
|
use super::REPL_COMMANDS;
|
||||||
|
|
||||||
use crate::config::{Config, SharedConfig};
|
use crate::config::{Config, SharedConfig};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use reedline::{
|
use reedline::{
|
||||||
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultPrompt, DefaultPromptSegment,
|
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, Emacs, FileBackedHistory, KeyCode,
|
||||||
Emacs, FileBackedHistory, KeyCode, KeyModifiers, Keybindings, Reedline, ReedlineEvent,
|
KeyModifiers, Keybindings, Prompt, PromptHistorySearch, PromptHistorySearchStatus, Reedline,
|
||||||
ReedlineMenu, ValidationResult, Validator,
|
ReedlineEvent, ReedlineMenu, ValidationResult, Validator,
|
||||||
};
|
};
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
const MENU_NAME: &str = "completion_menu";
|
const MENU_NAME: &str = "completion_menu";
|
||||||
|
const DEFAULT_PROMPT_INDICATOR: &str = "〉";
|
||||||
|
const DEFAULT_MULTILINE_INDICATOR: &str = "::: ";
|
||||||
|
|
||||||
|
pub struct Repl {
|
||||||
|
pub editor: Reedline,
|
||||||
|
pub prompt: ReplPrompt,
|
||||||
|
}
|
||||||
|
|
||||||
impl Repl {
|
impl Repl {
|
||||||
pub fn init(config: SharedConfig) -> Result<Self> {
|
pub fn init(config: SharedConfig) -> Result<Self> {
|
||||||
@ -19,7 +26,7 @@ impl Repl {
|
|||||||
.filter(|(_, _, v)| *v)
|
.filter(|(_, _, v)| *v)
|
||||||
.map(|(v, _, _)| *v)
|
.map(|(v, _, _)| *v)
|
||||||
.collect();
|
.collect();
|
||||||
let completer = Self::create_completer(config);
|
let completer = Self::create_completer(config.clone());
|
||||||
let keybindings = Self::create_keybindings();
|
let keybindings = Self::create_keybindings();
|
||||||
let history = Self::create_history()?;
|
let history = Self::create_history()?;
|
||||||
let menu = Self::create_menu();
|
let menu = Self::create_menu();
|
||||||
@ -33,14 +40,10 @@ impl Repl {
|
|||||||
.with_partial_completions(true)
|
.with_partial_completions(true)
|
||||||
.with_validator(Box::new(ReplValidator { multiline_commands }))
|
.with_validator(Box::new(ReplValidator { multiline_commands }))
|
||||||
.with_ansi_colors(true);
|
.with_ansi_colors(true);
|
||||||
let prompt = Self::create_prompt();
|
let prompt = ReplPrompt(config);
|
||||||
Ok(Self { editor, prompt })
|
Ok(Self { editor, prompt })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_prompt() -> DefaultPrompt {
|
|
||||||
DefaultPrompt::new(DefaultPromptSegment::Empty, DefaultPromptSegment::Empty)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_completer(config: SharedConfig) -> DefaultCompleter {
|
fn create_completer(config: SharedConfig) -> DefaultCompleter {
|
||||||
let mut completion: Vec<String> = REPL_COMMANDS
|
let mut completion: Vec<String> = REPL_COMMANDS
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -118,3 +121,45 @@ fn incomplete_brackets(line: &str, multiline_commands: &[&str]) -> bool {
|
|||||||
|
|
||||||
!balance.is_empty()
|
!balance.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ReplPrompt(SharedConfig);
|
||||||
|
|
||||||
|
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> {
|
||||||
|
Cow::Borrowed("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_prompt_indicator(&self, _prompt_mode: reedline::PromptEditMode) -> Cow<str> {
|
||||||
|
Cow::Borrowed(DEFAULT_PROMPT_INDICATOR)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ mod init;
|
|||||||
|
|
||||||
pub use self::abort::*;
|
pub use self::abort::*;
|
||||||
pub use self::handler::*;
|
pub use self::handler::*;
|
||||||
|
pub use self::init::Repl;
|
||||||
|
|
||||||
use crate::client::ChatGptClient;
|
use crate::client::ChatGptClient;
|
||||||
use crate::config::SharedConfig;
|
use crate::config::SharedConfig;
|
||||||
@ -11,7 +12,7 @@ use crate::print_now;
|
|||||||
use crate::term;
|
use crate::term;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use reedline::{DefaultPrompt, Reedline, Signal};
|
use reedline::Signal;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub const REPL_COMMANDS: [(&str, &str, bool); 10] = [
|
pub const REPL_COMMANDS: [(&str, &str, bool); 10] = [
|
||||||
@ -27,11 +28,6 @@ pub const REPL_COMMANDS: [(&str, &str, bool); 10] = [
|
|||||||
(".exit", "Exit the REPL", false),
|
(".exit", "Exit the REPL", false),
|
||||||
];
|
];
|
||||||
|
|
||||||
pub struct Repl {
|
|
||||||
editor: Reedline,
|
|
||||||
prompt: DefaultPrompt,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Repl {
|
impl Repl {
|
||||||
pub fn run(&mut self, client: ChatGptClient, config: SharedConfig) -> Result<()> {
|
pub fn run(&mut self, client: ChatGptClient, config: SharedConfig) -> Result<()> {
|
||||||
let abort = AbortSignal::new();
|
let abort = AbortSignal::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user