summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/config.rs28
-rw-r--r--src/main.rs16
3 files changed, 33 insertions, 13 deletions
diff --git a/README.md b/README.md
index 2f5833a..07a3a4b 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,7 @@ aichat may generate the following files in the config dir:
We can let ChatGPT play a certain role through `prompt` to make it better generate what we want. See [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts) for details.
-We can predefine a batch of roles in `rules.yaml`. For example, we define a javascript-console role as follows.
+We can predefine a batch of roles in `roles.yaml`. For example, we define a javascript-console role as follows.
```yaml
- name: javascript-console
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();