summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-03 09:38:53 +0800
committersigoden <sigoden@gmail.com>2023-03-03 10:11:23 +0800
commit627a2d08ce0932241a21a281712e58427c15d478 (patch)
treebf7059a6c9c8e0df939888ba37dc4759640f30b7 /src/main.rs
parent9e8a5481cf9aa4dfd75d5c55c003b3813ccbca1e (diff)
downloadaichat-627a2d08ce0932241a21a281712e58427c15d478.tar.gz
refactor: config path and config data
use yaml other than toml for configuration. use $AICHAT_CONFIG_DIR other than $HOME for config dir. split roles to seperate config file
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs28
1 files changed, 6 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index 18be063..0f19adf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,11 +3,10 @@ mod config;
use std::fs::{File, OpenOptions};
use std::io::{stdout, Write};
use std::path::Path;
-use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
-use config::{Config, Role};
+use config::{Config, Role, CONFIG_FILE_NAME, HISTORY_FILE_NAME, MESSAGE_FILE_NAME};
use anyhow::{anyhow, Result};
use clap::{Arg, ArgAction, Command};
@@ -77,7 +76,7 @@ fn start() -> Result<()> {
.collect::<Vec<String>>()
.join(" ")
});
- let config_path = get_config_path()?;
+ let config_path = Config::local_file(CONFIG_FILE_NAME)?;
if !config_path.exists() && text.is_none() {
create_config_file(&config_path)?;
}
@@ -137,7 +136,7 @@ fn run_repl(
]),
);
let history = Box::new(
- FileBackedHistory::with_file(1000, get_history_path()?)
+ FileBackedHistory::with_file(1000, Config::local_file(HISTORY_FILE_NAME)?)
.map_err(|err| anyhow!("Failed to setup history file, {err}"))?,
);
let edit_mode = Box::new(Emacs::new(keybindings));
@@ -150,17 +149,12 @@ fn run_repl(
let mut trigged_ctrlc = false;
let mut output = String::new();
let mut role: Option<Role> = None;
- let mut save_file: Option<File> = if let Some(path) = &config.save_path {
+ let mut save_file: Option<File> = if config.save {
let file = OpenOptions::new()
.create(true)
.append(true)
- .open(path)
- .map_err(|err| {
- anyhow!(
- "Failed to create/append save_file at {}, {err}",
- path.display()
- )
- })?;
+ .open(Config::local_file(MESSAGE_FILE_NAME)?)
+ .map_err(|err| anyhow!("Failed to create/append save_file, {err}"))?;
Some(file)
} else {
None
@@ -440,16 +434,6 @@ fn dump<T: ToString>(text: T, newlines: usize) {
stdout().flush().unwrap();
}
-fn get_config_path() -> Result<PathBuf> {
- let config_dir = dirs::home_dir().ok_or_else(|| anyhow!("No home dir"))?;
- Ok(config_dir.join(format!(".{}.toml", env!("CARGO_CRATE_NAME"))))
-}
-
-fn get_history_path() -> Result<PathBuf> {
- let config_dir = dirs::home_dir().ok_or_else(|| anyhow!("No home dir"))?;
- Ok(config_dir.join(format!(".{}_history", env!("CARGO_CRATE_NAME"))))
-}
-
fn print_repl_title() {
println!("Welcome to aichat {}", env!("CARGO_PKG_VERSION"));
println!("Type \".help\" for more information.");