summaryrefslogtreecommitdiffstats
path: root/src/repl/init.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-11-02 09:53:54 +0800
committerGitHub <noreply@github.com>2023-11-02 09:53:54 +0800
commit444f4ebe9de8aa68afdf8d39b734a8543b9b5c4b (patch)
treed8b0e16faa0270fda2a6c7afa5fe18c1970b9a93 /src/repl/init.rs
parent5c7bfd92ff3e557477969be9db0638ef0d3d3659 (diff)
downloadaichat-444f4ebe9de8aa68afdf8d39b734a8543b9b5c4b.tar.gz
refactor: improve code quanity (#196)
- rewrite Repl, remove ReplHandler - move ReplyStreamHandler to repl/ and rename it to ReplyHandler - deprecate utils::print_now - refactor session info
Diffstat (limited to 'src/repl/init.rs')
-rw-r--r--src/repl/init.rs85
1 files changed, 0 insertions, 85 deletions
diff --git a/src/repl/init.rs b/src/repl/init.rs
deleted file mode 100644
index fbbcf22..0000000
--- a/src/repl/init.rs
+++ /dev/null
@@ -1,85 +0,0 @@
-use super::{
- highlighter::ReplHighlighter, prompt::ReplPrompt, validator::ReplValidator, REPL_COMMANDS,
-};
-
-use crate::config::SharedConfig;
-
-use anyhow::Result;
-use reedline::{
- default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
- ColumnarMenu, DefaultCompleter, EditMode, Emacs, KeyCode, KeyModifiers, Keybindings, Reedline,
- ReedlineEvent, ReedlineMenu, Vi,
-};
-
-const MENU_NAME: &str = "completion_menu";
-
-pub struct Repl {
- pub(crate) editor: Reedline,
- pub(crate) prompt: ReplPrompt,
-}
-
-impl Repl {
- pub fn init(config: SharedConfig) -> Result<Self> {
- let commands: Vec<String> = REPL_COMMANDS
- .into_iter()
- .map(|(v, _)| v.to_string())
- .collect();
-
- let completer = Self::create_completer(&config, &commands);
- let highlighter = ReplHighlighter::new(commands, config.clone());
- let menu = Self::create_menu();
- let edit_mode: Box<dyn EditMode> = if config.read().keybindings.is_vi() {
- let mut normal_keybindings = default_vi_normal_keybindings();
- let mut insert_keybindings = default_vi_insert_keybindings();
- Self::extra_keybindings(&mut normal_keybindings);
- Self::extra_keybindings(&mut insert_keybindings);
- Box::new(Vi::new(insert_keybindings, normal_keybindings))
- } else {
- let mut keybindings = default_emacs_keybindings();
- Self::extra_keybindings(&mut keybindings);
- Box::new(Emacs::new(keybindings))
- };
- let mut editor = Reedline::create()
- .with_completer(Box::new(completer))
- .with_highlighter(Box::new(highlighter))
- .with_menu(menu)
- .with_edit_mode(edit_mode)
- .with_quick_completions(true)
- .with_partial_completions(true)
- .with_validator(Box::new(ReplValidator))
- .with_ansi_colors(true);
- editor.enable_bracketed_paste()?;
- let prompt = ReplPrompt::new(config);
- Ok(Self { editor, prompt })
- }
-
- fn create_completer(config: &SharedConfig, commands: &[String]) -> DefaultCompleter {
- let mut completion = commands.to_vec();
- completion.extend(config.read().repl_completions());
- let mut completer =
- DefaultCompleter::with_inclusions(&['.', '-', '_', ':']).set_min_word_len(2);
- completer.insert(completion.clone());
- completer
- }
-
- fn extra_keybindings(keybindings: &mut Keybindings) {
- keybindings.add_binding(
- KeyModifiers::NONE,
- KeyCode::Tab,
- ReedlineEvent::UntilFound(vec![
- ReedlineEvent::Menu(MENU_NAME.to_string()),
- ReedlineEvent::MenuNext,
- ]),
- );
- keybindings.add_binding(
- KeyModifiers::CONTROL,
- KeyCode::Char('s'),
- ReedlineEvent::Submit,
- );
- }
-
- fn create_menu() -> ReedlineMenu {
- let completion_menu = ColumnarMenu::default().with_name(MENU_NAME);
- ReedlineMenu::EngineCompleter(Box::new(completion_menu))
- }
-}