diff options
| author | Anthony Rubick <68485672+AnthonyMichaelTDM@users.noreply.github.com> | 2023-07-09 20:07:09 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-10 11:07:09 +0800 |
| commit | 97fc7de6754c8b5c383d7d5e3f9e26b3bbfb08fb (patch) | |
| tree | c19390877425fa50049761e8ee6bc93f21c46f56 /src/repl | |
| parent | ec51b84290215d11d5045de8baa9b89f2447b1e9 (diff) | |
| download | aichat-97fc7de6754c8b5c383d7d5e3f9e26b3bbfb08fb.tar.gz | |
fix: minor code cleanup and pedantic lints (#134)
Diffstat (limited to 'src/repl')
| -rw-r--r-- | src/repl/abort.rs | 1 | ||||
| -rw-r--r-- | src/repl/handler.rs | 30 | ||||
| -rw-r--r-- | src/repl/highlighter.rs | 14 | ||||
| -rw-r--r-- | src/repl/init.rs | 4 | ||||
| -rw-r--r-- | src/repl/mod.rs | 17 | ||||
| -rw-r--r-- | src/repl/prompt.rs | 17 | ||||
| -rw-r--r-- | src/repl/validator.rs | 3 |
7 files changed, 45 insertions, 41 deletions
diff --git a/src/repl/abort.rs b/src/repl/abort.rs index f76abb6..5377f43 100644 --- a/src/repl/abort.rs +++ b/src/repl/abort.rs @@ -5,6 +5,7 @@ use std::sync::{ pub type SharedAbortSignal = Arc<AbortSignal>; +#[allow(clippy::module_name_repetitions)] pub struct AbortSignal { ctrlc: AtomicBool, ctrld: AtomicBool, diff --git a/src/repl/handler.rs b/src/repl/handler.rs index 495413c..2057b8b 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -24,6 +24,7 @@ pub enum ReplCmd { Copy, } +#[allow(clippy::module_name_repetitions)] pub struct ReplCmdHandler { client: ChatGptClient, config: SharedConfig, @@ -32,6 +33,7 @@ pub struct ReplCmdHandler { } impl ReplCmdHandler { + #[allow(clippy::unnecessary_wraps)] pub fn init( client: ChatGptClient, config: SharedConfig, @@ -58,7 +60,7 @@ impl ReplCmdHandler { let ret = render_stream( &input, &self.client, - self.config.clone(), + &self.config, true, self.abort.clone(), wg.clone(), @@ -113,6 +115,7 @@ impl ReplCmdHandler { } } +#[allow(clippy::module_name_repetitions)] pub struct ReplyStreamHandler { sender: Option<Sender<ReplyStreamEvent>>, buffer: String, @@ -154,22 +157,19 @@ impl ReplyStreamHandler { } pub fn done(&mut self) -> Result<()> { - match self.sender.as_ref() { - Some(tx) => { - let ret = tx - .send(ReplyStreamEvent::Done) - .with_context(|| "Failed to send StreamEvent:Done"); - self.safe_ret(ret)?; + if let Some(tx) = self.sender.as_ref() { + let ret = tx + .send(ReplyStreamEvent::Done) + .with_context(|| "Failed to send StreamEvent:Done"); + self.safe_ret(ret)?; + } else { + if !self.buffer.ends_with('\n') { + print_now!("\n"); } - None => { - if !self.buffer.ends_with('\n') { - print_now!("\n") - } - if self.repl { + if self.repl { + print_now!("\n"); + if cfg!(macos) { print_now!("\n"); - if cfg!(macos) { - print_now!("\n") - } } } } diff --git a/src/repl/highlighter.rs b/src/repl/highlighter.rs index 593c307..b1ec023 100644 --- a/src/repl/highlighter.rs +++ b/src/repl/highlighter.rs @@ -5,6 +5,7 @@ use reedline::{Highlighter, StyledText}; const MATCH_COLOR: Color = Color::Green; +#[allow(clippy::module_name_repetitions)] pub struct ReplHighlighter { external_commands: Vec<String>, config: SharedConfig, @@ -12,10 +13,10 @@ pub struct ReplHighlighter { impl ReplHighlighter { /// Construct the default highlighter with a given set of extern commands/keywords to detect and highlight - pub fn new(config: SharedConfig, external_commands: Vec<String>) -> ReplHighlighter { + pub fn new(config: SharedConfig, external_commands: Vec<String>) -> Self { Self { - config, external_commands, + config, } } } @@ -28,9 +29,10 @@ impl Highlighter for ReplHighlighter { } else { Color::White }; - let match_color = match self.config.read().highlight { - true => MATCH_COLOR, - false => color, + let match_color = if self.config.read().highlight { + MATCH_COLOR + } else { + color }; if self @@ -45,7 +47,7 @@ impl Highlighter for ReplHighlighter { .filter(|c| line.contains(*c)) .map(std::ops::Deref::deref) .collect(); - let longest_match = matches.iter().fold("".to_string(), |acc, &item| { + let longest_match = matches.iter().fold(String::new(), |acc, &item| { if item.len() > acc.len() { item.to_string() } else { diff --git a/src/repl/init.rs b/src/repl/init.rs index 2ec30bf..089e855 100644 --- a/src/repl/init.rs +++ b/src/repl/init.rs @@ -24,7 +24,7 @@ impl Repl { .map(|(v, _)| v.to_string()) .collect(); - let completer = Self::create_completer(config.clone(), &commands); + let completer = Self::create_completer(&config, &commands); let highlighter = ReplHighlighter::new(config.clone(), commands); let keybindings = Self::create_keybindings(); let history = Self::create_history()?; @@ -45,7 +45,7 @@ impl Repl { Ok(Self { editor, prompt }) } - fn create_completer(config: SharedConfig, commands: &[String]) -> DefaultCompleter { + fn create_completer(config: &SharedConfig, commands: &[String]) -> DefaultCompleter { let mut completion = commands.to_vec(); completion.extend(config.read().repl_completions()); let mut completer = diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 8f3d184..a728e2e 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -55,7 +55,7 @@ impl Repl { Ok(Signal::Success(line)) => { already_ctrlc = false; abort.reset(); - match self.handle_line(handler.clone(), line) { + match self.handle_line(&handler, &line) { Ok(quit) => { if quit { break; @@ -69,12 +69,11 @@ impl Repl { } Ok(Signal::CtrlC) => { abort.set_ctrlc(); - if !already_ctrlc { - already_ctrlc = true; - print_now!("(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n\n"); - } else { + if already_ctrlc { break; } + already_ctrlc = true; + print_now!("(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n\n"); } Ok(Signal::CtrlD) => { abort.set_ctrld(); @@ -86,9 +85,9 @@ impl Repl { Ok(()) } - fn handle_line(&mut self, handler: Arc<ReplCmdHandler>, line: String) -> Result<bool> { - let line = clean_multiline_symbols(&line); - match parse_command(&line) { + fn handle_line(&mut self, handler: &Arc<ReplCmdHandler>, line: &str) -> Result<bool> { + let line = clean_multiline_symbols(line); + match parse_command(line.as_ref()) { Some((cmd, args)) => match cmd { ".exit" => { return Ok(true); @@ -176,7 +175,7 @@ Press Ctrl+C to abort readline, Ctrl+D to exit the REPL fn clean_multiline_symbols(line: &str) -> Cow<str> { let trimed_line = line.trim(); match trimed_line.chars().next() { - Some('{') | Some('[') | Some('(') => trimed_line[1..trimed_line.len() - 1].into(), + Some('{' | '[' | '(') => trimed_line[1..trimed_line.len() - 1].into(), _ => Cow::Borrowed(line), } } diff --git a/src/repl/prompt.rs b/src/repl/prompt.rs index 8c670f4..c9fa005 100644 --- a/src/repl/prompt.rs +++ b/src/repl/prompt.rs @@ -9,6 +9,7 @@ const PROMPT_MULTILINE_COLOR: nu_ansi_term::Color = nu_ansi_term::Color::LightBl const INDICATOR_COLOR: Color = Color::Cyan; const PROMPT_RIGHT_COLOR: Color = Color::AnsiValue(5); +#[allow(clippy::module_name_repetitions)] #[derive(Clone)] pub struct ReplPrompt { config: SharedConfig, @@ -21,7 +22,7 @@ pub struct ReplPrompt { 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::get_colors(&config); Self { config, prompt_color, @@ -32,14 +33,14 @@ impl ReplPrompt { } pub fn sync_config(&mut self) { let (prompt_color, prompt_multiline_color, indicator_color, prompt_right_color) = - Self::get_colors(self.config.clone()); + Self::get_colors(&self.config); 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) { + pub fn get_colors(config: &SharedConfig) -> (Color, nu_ansi_term::Color, Color, Color) { let (highlight, light_theme) = config.read().get_render_options(); if highlight { ( @@ -68,11 +69,11 @@ impl ReplPrompt { 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("") - } + self.config + .read() + .role + .as_ref() + .map_or(Cow::Borrowed(""), |role| Cow::Owned(role.name.clone())) } fn render_prompt_right(&self) -> Cow<str> { diff --git a/src/repl/validator.rs b/src/repl/validator.rs index e1f6e99..d829911 100644 --- a/src/repl/validator.rs +++ b/src/repl/validator.rs @@ -1,6 +1,7 @@ use reedline::{ValidationResult, Validator}; /// A default validator which checks for mismatched quotes and brackets +#[allow(clippy::module_name_repetitions)] pub struct ReplValidator; impl Validator for ReplValidator { @@ -30,7 +31,7 @@ fn incomplete_brackets(line: &str) -> bool { None => match c { '{' | '(' => { balance.push(c); - symbol = Some(c) + symbol = Some(c); } _ => {} }, |
