Skip to content

Commit 3e2dd16

Browse files
committed
ensure data is written to disk before continuing, fix spinner delay
1 parent 8655c5e commit 3e2dd16

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

Cargo.lock

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ regex = "1.7.3"
2222
rand = "0.8.5"
2323
serde_with = {version = "2.3.1", features = ["macros"]}
2424
chrono = "0.4.24"
25+
os_info = "3.7.0"
26+
sys-info = "0.9.1"

src/main.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ mod spinners;
33

44
use std::{
55
collections::HashMap,
6-
io::{stdout, Write},
6+
fs::File,
7+
io::{stdout, BufRead, BufReader, Write},
78
panic,
89
path::{self, Path},
910
sync::{Arc, Mutex},
@@ -223,7 +224,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
223224
}
224225

225226
println!("To clear the conversation history, type /clear");
226-
227227
loop {
228228
let readline = rl.readline(">> ");
229229
match readline {
@@ -318,7 +318,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
318318

319319
config::save_config(&config_dir.display().to_string().as_str(), &config).await?;
320320

321-
if config.app.notify_save {
321+
println!("Saved config!");
322+
323+
if config.app.save_conversation && !messages.is_empty() {
322324
let logs_dir = data_dir.join("logs");
323325
if !logs_dir.exists() {
324326
tokio::fs::create_dir_all(&logs_dir).await?;
@@ -333,7 +335,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
333335
let mut log_file = tokio::io::BufWriter::new(log_file);
334336

335337
let messages_json = serde_json::to_string(&messages)?;
338+
336339
log_file.write_all(messages_json.as_bytes()).await?;
340+
// Ensure the data is written to disk
341+
log_file.flush().await?;
337342

338343
let mut log_file_content = String::new();
339344
for message in messages {
@@ -356,6 +361,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
356361
let mut log_file = tokio::io::BufWriter::new(log_file);
357362

358363
log_file.write_all(log_file_content.as_bytes()).await?;
364+
log_file.flush().await?;
359365
}
360366

361367
Ok(())
@@ -514,14 +520,7 @@ async fn chat_completion(
514520
);
515521
execute!(stdout(), Clear(ClearType::UntilNewLine)).unwrap();
516522

517-
tokio::time::sleep(std::time::Duration::from_millis(
518-
if u64::from(spinner_interval) < rainbow_delay {
519-
rainbow_delay
520-
} else {
521-
spinner_interval.into()
522-
},
523-
))
524-
.await;
523+
tokio::time::sleep(std::time::Duration::from_millis(rainbow_delay)).await;
525524

526525
i = i + 1;
527526
}

0 commit comments

Comments
 (0)