summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-03 12:43:34 +0800
committersigoden <sigoden@gmail.com>2023-03-03 12:43:34 +0800
commit2e511c13276b7109e6774263d37eafce8df0a434 (patch)
treeaa2066db5a9c7b30d96ba84cc95c1f6411823cf7 /src
parent1cde32160bbc594e8ed5d209a429ba57fd541876 (diff)
downloadaichat-2e511c13276b7109e6774263d37eafce8df0a434.tar.gz
chore: optimize config path
Diffstat (limited to 'src')
-rw-r--r--src/config.rs28
-rw-r--r--src/main.rs16
2 files changed, 32 insertions, 12 deletions
diff --git a/src/config.rs b/src/config.rs
index 3c2c633..7f21027 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -7,10 +7,10 @@ use std::{
use anyhow::{anyhow, Result};
use serde::Deserialize;
-pub const CONFIG_FILE_NAME: &str = "config.yaml";
-pub const ROLES_FILE_NAME: &str = "roles.yaml";
-pub const HISTORY_FILE_NAME: &str = "history.txt";
-pub const MESSAGE_FILE_NAME: &str = "messages.md";
+const CONFIG_FILE_NAME: &str = "config.yaml";
+const ROLES_FILE_NAME: &str = "roles.yaml";
+const HISTORY_FILE_NAME: &str = "history.txt";
+const MESSAGE_FILE_NAME: &str = "messages.md";
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
@@ -40,6 +40,7 @@ impl Config {
config.load_roles()?;
Ok(config)
}
+
pub fn local_file(name: &str) -> Result<PathBuf> {
let env_name = format!(
"{}_CONFIG_DIR",
@@ -58,8 +59,25 @@ impl Config {
path.push(name);
Ok(path)
}
+
+ pub fn config_file() -> Result<PathBuf> {
+ Self::local_file(CONFIG_FILE_NAME)
+ }
+
+ pub fn roles_file() -> Result<PathBuf> {
+ Self::local_file(ROLES_FILE_NAME)
+ }
+
+ pub fn history_file() -> Result<PathBuf> {
+ Self::local_file(HISTORY_FILE_NAME)
+ }
+
+ pub fn messages_file() -> Result<PathBuf> {
+ Self::local_file(MESSAGE_FILE_NAME)
+ }
+
fn load_roles(&mut self) -> Result<()> {
- let path = Self::local_file(ROLES_FILE_NAME)?;
+ let path = Self::roles_file()?;
if !path.exists() {
return Ok(());
}
diff --git a/src/main.rs b/src/main.rs
index 5d28b73..92d15bf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,7 +6,7 @@ use std::path::Path;
use std::process::exit;
use std::time::Duration;
-use config::{Config, Role, CONFIG_FILE_NAME, HISTORY_FILE_NAME, MESSAGE_FILE_NAME};
+use config::{Config, Role};
use anyhow::{anyhow, Result};
use clap::{Arg, ArgAction, Command};
@@ -76,7 +76,7 @@ fn start() -> Result<()> {
.collect::<Vec<String>>()
.join(" ")
});
- let config_path = Config::local_file(CONFIG_FILE_NAME)?;
+ let config_path = Config::config_file()?;
if !config_path.exists() && text.is_none() {
create_config_file(&config_path)?;
}
@@ -136,7 +136,7 @@ fn run_repl(
]),
);
let history = Box::new(
- FileBackedHistory::with_file(1000, Config::local_file(HISTORY_FILE_NAME)?)
+ FileBackedHistory::with_file(1000, Config::history_file()?)
.map_err(|err| anyhow!("Failed to setup history file, {err}"))?,
);
let edit_mode = Box::new(Emacs::new(keybindings));
@@ -153,7 +153,7 @@ fn run_repl(
let file = OpenOptions::new()
.create(true)
.append(true)
- .open(Config::local_file(MESSAGE_FILE_NAME)?)
+ .open(Config::messages_file()?)
.map_err(|err| anyhow!("Failed to create/append save_file, {err}"))?;
Some(file)
} else {
@@ -211,9 +211,7 @@ fn run_repl(
},
None => dump("Usage: .role <name>.", 2),
},
- _ => {
- dump("Unknown command. Type \".help\" for more information.", 2);
- }
+ _ => unknown_command(),
}
} else {
let input = if let Some(role) = role.take() {
@@ -450,6 +448,10 @@ async fn acquire_stream(
Ok(stream)
}
+fn unknown_command() {
+ dump("Unknown command. Type \".help\" for more information.", 2);
+}
+
fn dump<T: ToString>(text: T, newlines: usize) {
print!("{}{}", text.to_string(), "\n".repeat(newlines));
stdout().flush().unwrap();