summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/config.rs16
-rw-r--r--src/main.rs6
-rw-r--r--src/repl.rs7
-rw-r--r--src/utils.rs6
6 files changed, 33 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 549539e..59b257f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -23,6 +23,7 @@ version = "0.2.0"
dependencies = [
"anyhow",
"bytes",
+ "chrono",
"clap",
"copypasta",
"crossbeam",
diff --git a/Cargo.toml b/Cargo.toml
index 92c63eb..d70b5ea 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,6 +32,7 @@ pulldown-cmark = { version = "0.9.2", default-features = false, features = ['sim
crossbeam = "0.8.2"
crossterm = "0.26.1"
copypasta = "0.8.2"
+chrono = "0.4.23"
[dependencies.syntect]
version = "5.0.0"
diff --git a/src/config.rs b/src/config.rs
index b5220dd..3a85066 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -10,6 +10,8 @@ use anyhow::{anyhow, Result};
use inquire::{Confirm, Text};
use serde::Deserialize;
+use crate::utils::now;
+
const CONFIG_FILE_NAME: &str = "config.yaml";
const ROLES_FILE_NAME: &str = "roles.yaml";
const HISTORY_FILE_NAME: &str = "history.txt";
@@ -97,11 +99,21 @@ impl Config {
Ok(file)
}
- pub fn save_message(file: Option<&mut File>, input: &str, output: &str) {
+ pub fn save_message(
+ file: Option<&mut File>,
+ input: &str,
+ output: &str,
+ role_name: &Option<String>,
+ ) {
+ let role_name = match role_name {
+ Some(v) => format!("({v})"),
+ None => String::new(),
+ };
+ let timestamp = format!("[{}]", now());
if let (false, Some(file)) = (output.is_empty(), file) {
let _ = file.write_all(
format!(
- "AICHAT: {}\n\n--------\n{}\n--------\n\n",
+ "# CHAT:{timestamp} {role_name}\n{}\n\n--------\n{}\n--------\n\n",
input.trim(),
output.trim(),
)
diff --git a/src/main.rs b/src/main.rs
index 9cb360d..55c09a6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -56,7 +56,9 @@ fn start_directive(
input: &str,
) -> Result<()> {
let mut file = config.open_message_file()?;
- let output = client.acquire(input, role.map(|v| v.prompt))?;
+ let prompt = role.as_ref().map(|v| v.prompt.to_string());
+ let role_name = role.as_ref().map(|v| v.name.to_string());
+ let output = client.acquire(input, prompt)?;
let output = output.trim();
if !config.no_highlight && stdout().is_terminal() {
let markdown_render = MarkdownRender::init()?;
@@ -65,7 +67,7 @@ fn start_directive(
println!("{output}");
}
- Config::save_message(file.as_mut(), input, output);
+ Config::save_message(file.as_mut(), input, output, &role_name);
Ok(())
}
diff --git a/src/repl.rs b/src/repl.rs
index 2a3a48f..164d746 100644
--- a/src/repl.rs
+++ b/src/repl.rs
@@ -278,10 +278,17 @@ impl ReplCmdHandler {
};
self.client
.acquire_stream(&input, prompt, &mut receiver, self.ctrlc.clone())?;
+ let role = self
+ .state
+ .borrow_mut()
+ .role
+ .as_ref()
+ .map(|v| v.name.to_string());
Config::save_message(
self.state.borrow_mut().save_file.as_mut(),
&input,
&receiver.output,
+ &role,
);
wg.wait();
self.state.borrow_mut().reply = receiver.output;
diff --git a/src/utils.rs b/src/utils.rs
index cb5d1f9..b248d92 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
+use chrono::prelude::*;
use copypasta::{ClipboardContext, ClipboardProvider};
use std::io::{stdout, Write};
@@ -17,3 +18,8 @@ pub fn paste() -> Result<String> {
let mut ctx = ClipboardContext::new().map_err(|err| anyhow!("{err}"))?;
ctx.get_contents().map_err(|err| anyhow!("{err}"))
}
+
+pub fn now() -> String {
+ let now = Local::now();
+ now.to_rfc3339_opts(SecondsFormat::Secs, false)
+}