summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-02-24 19:13:48 +0800
committerGitHub <noreply@github.com>2024-02-24 19:13:48 +0800
commit21d1be5bedb13d08462e9800a1c3cbf93dd585bc (patch)
treef21874bd7fefa25fccf443df1249cdf3c74e0763 /src
parent16b7ac071f3feed52939c2244ede2f0abe5230a2 (diff)
downloadaichat-21d1be5bedb13d08462e9800a1c3cbf93dd585bc.tar.gz
refactor: improve prompt error handling (#319)
Diffstat (limited to 'src')
-rw-r--r--src/config/mod.rs22
-rw-r--r--src/main.rs5
-rw-r--r--src/utils/prompt_input.rs10
3 files changed, 12 insertions, 25 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index d38cbf3..f8d97c2 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -11,7 +11,7 @@ use crate::client::{
Model, OpenAIClient, SendData,
};
use crate::render::{MarkdownRender, RenderOptions};
-use crate::utils::{get_env_name, light_theme_from_colorfgbg, now, prompt_op_err, render_prompt};
+use crate::utils::{get_env_name, light_theme_from_colorfgbg, now, render_prompt};
use anyhow::{anyhow, bail, Context, Result};
use inquire::{Confirm, Select, Text};
@@ -577,8 +577,7 @@ impl Config {
"Start a session that incorporates the last question and answer?",
)
.with_default(false)
- .prompt()
- .map_err(prompt_op_err)?;
+ .prompt()?;
if ans {
session.add_message(input, output)?;
}
@@ -593,19 +592,13 @@ impl Config {
self.last_message = None;
self.temperature = self.default_temperature;
if session.should_save() {
- let ans = Confirm::new("Save session?")
- .with_default(false)
- .prompt()
- .map_err(prompt_op_err)?;
+ let ans = Confirm::new("Save session?").with_default(false).prompt()?;
if !ans {
return Ok(());
}
let mut name = session.name().to_string();
if session.is_temp() {
- name = Text::new("Session name:")
- .with_default(&name)
- .prompt()
- .map_err(prompt_op_err)?;
+ name = Text::new("Session name:").with_default(&name).prompt()?;
}
let session_path = Self::session_file(&name)?;
let sessions_dir = session_path.parent().ok_or_else(|| {
@@ -917,15 +910,12 @@ pub enum State {
fn create_config_file(config_path: &Path) -> Result<()> {
let ans = Confirm::new("No config file, create a new one?")
.with_default(true)
- .prompt()
- .map_err(prompt_op_err)?;
+ .prompt()?;
if !ans {
exit(0);
}
- let client = Select::new("Platform:", list_client_types())
- .prompt()
- .map_err(prompt_op_err)?;
+ let client = Select::new("Platform:", list_client_types()).prompt()?;
let mut config = serde_json::json!({});
config["model"] = client.into();
diff --git a/src/main.rs b/src/main.rs
index 8946985..29e034f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,7 +11,7 @@ mod utils;
use crate::cli::Cli;
use crate::config::{Config, GlobalConfig};
-use crate::utils::{prompt_op_err, run_command};
+use crate::utils::run_command;
use anyhow::{bail, Result};
use clap::Parser;
@@ -165,8 +165,7 @@ fn execute(config: &GlobalConfig, text: &str) -> Result<()> {
)),
}
})
- .prompt()
- .map_err(prompt_op_err)?;
+ .prompt()?;
match anwser.as_str() {
"E" | "e" => {
diff --git a/src/utils/prompt_input.rs b/src/utils/prompt_input.rs
index f63823a..e887b9f 100644
--- a/src/utils/prompt_input.rs
+++ b/src/utils/prompt_input.rs
@@ -10,7 +10,8 @@ pub fn prompt_input_string(desc: &str, required: bool) -> anyhow::Result<String>
} else {
text = text.with_help_message(MSG_OPTIONAL)
}
- text.prompt().map_err(prompt_op_err)
+ let text = text.prompt()?;
+ Ok(text)
}
pub fn prompt_input_integer(desc: &str, required: bool) -> anyhow::Result<String> {
@@ -36,11 +37,8 @@ pub fn prompt_input_integer(desc: &str, required: bool) -> anyhow::Result<String
})
.with_help_message(MSG_OPTIONAL)
}
- text.prompt().map_err(prompt_op_err)
-}
-
-pub fn prompt_op_err<T>(_: T) -> anyhow::Error {
- anyhow::anyhow!("Not finish questionnaire, try again later!")
+ let text = text.prompt()?;
+ Ok(text)
}
#[derive(Debug, Clone, Copy)]