fix: repl set save true not work if not rerun(#34)

This commit is contained in:
sigoden 2023-03-08 11:27:51 +08:00 committed by GitHub
parent b05fce7bca
commit b7cb6f89f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 43 deletions

View File

@ -103,29 +103,14 @@ impl Config {
Ok(path)
}
pub fn open_message_file(&self) -> Result<Option<File>> {
pub fn save_message(&self, input: &str, output: &str) -> Result<()> {
if !self.save {
return Ok(None);
}
let path = Config::messages_file()?;
let file: Option<File> = if self.save {
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.with_context(|| format!("Failed to create/append {}", path.display()))?;
Some(file)
} else {
None
};
Ok(file)
}
pub fn save_message(&self, file: Option<&mut File>, input: &str, output: &str) -> Result<()> {
if output.is_empty() || !self.save || file.is_none() {
return Ok(());
}
let file = file.unwrap();
let mut file = self.open_message_file()?;
if output.is_empty() || !self.save {
return Ok(());
}
let timestamp = now();
let output = match self.role.as_ref() {
None => {
@ -261,7 +246,7 @@ impl Config {
match key {
"api_key" => {
if unset {
return Ok("Not allowed".into());
return Ok("Error: Not allowed".into());
} else {
self.api_key = value.to_string();
}
@ -298,6 +283,15 @@ impl Config {
Ok("".into())
}
fn open_message_file(&self) -> Result<File> {
let path = Config::messages_file()?;
OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.with_context(|| format!("Failed to create/append {}", path.display()))
}
fn load_roles(&mut self) -> Result<()> {
let path = Self::roles_file()?;
if !path.exists() {

View File

@ -70,7 +70,6 @@ fn start_directive(
input: &str,
no_stream: bool,
) -> Result<()> {
let mut file = config.borrow().open_message_file()?;
let prompt = config.borrow().get_prompt();
let highlight = config.borrow().highlight && stdout().is_terminal();
let output = if no_stream {
@ -94,7 +93,7 @@ fn start_directive(
wg.wait();
output
};
config.borrow().save_message(file.as_mut(), input, &output)
config.borrow().save_message(input, &output)
}
fn start_interactive(client: ChatGptClient, config: SharedConfig) -> Result<()> {

View File

@ -7,7 +7,6 @@ use anyhow::Result;
use crossbeam::channel::Sender;
use crossbeam::sync::WaitGroup;
use std::cell::RefCell;
use std::fs::File;
use super::abort::SharedAbortSignal;
@ -23,30 +22,21 @@ pub enum ReplCmd {
pub struct ReplCmdHandler {
client: ChatGptClient,
config: SharedConfig,
state: RefCell<ReplCmdHandlerState>,
reply: RefCell<String>,
abort: SharedAbortSignal,
}
pub struct ReplCmdHandlerState {
reply: String,
save_file: Option<File>,
}
impl ReplCmdHandler {
pub fn init(
client: ChatGptClient,
config: SharedConfig,
abort: SharedAbortSignal,
) -> Result<Self> {
let save_file = config.as_ref().borrow().open_message_file()?;
let state = RefCell::new(ReplCmdHandlerState {
save_file,
reply: String::new(),
});
let reply = RefCell::new(String::new());
Ok(Self {
client,
config,
state,
reply,
abort,
})
}
@ -55,7 +45,7 @@ impl ReplCmdHandler {
match cmd {
ReplCmd::Submit(input) => {
if input.is_empty() {
self.state.borrow_mut().reply.clear();
self.reply.borrow_mut().clear();
return Ok(());
}
let highlight = self.config.borrow().highlight;
@ -72,12 +62,8 @@ impl ReplCmdHandler {
);
wg.wait();
let buffer = ret?;
self.config.borrow().save_message(
self.state.borrow_mut().save_file.as_mut(),
&input,
&buffer,
)?;
self.state.borrow_mut().reply = buffer;
self.config.borrow().save_message(&input, &buffer)?;
*self.reply.borrow_mut() = buffer;
}
ReplCmd::SetRole(name) => {
let output = self.config.borrow_mut().change_role(&name);