Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 14 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(),
)
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -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(())
}

Expand Down
7 changes: 7 additions & 0 deletions src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
use chrono::prelude::*;
use copypasta::{ClipboardContext, ClipboardProvider};
use std::io::{stdout, Write};

Expand All @@ -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)
}