diff --git a/src/commands/history/mod.rs b/src/commands/history/mod.rs index 56c0b20..35ebf2c 100644 --- a/src/commands/history/mod.rs +++ b/src/commands/history/mod.rs @@ -1,6 +1,9 @@ use clap::Args; use sqlite::{Connection, State, Value}; -use tabled::{settings::{peaker::PriorityMax, Width}, Table, Tabled}; +use tabled::{ + settings::{peaker::PriorityMax, Width}, + Table, Tabled, +}; #[derive(Debug, Args)] #[clap(name = "history", about = "Display the previous search history")] @@ -21,36 +24,37 @@ pub struct HistoryRow { } pub async fn connect_history() -> Connection { - let home_dir = dirs::home_dir().unwrap(); let save_dir = home_dir.join(".cllm"); let db_path = save_dir.join(":memory:"); - + let connection = sqlite::open(db_path).unwrap(); - connection.execute( - " + connection + .execute( + " CREATE TABLE IF NOT EXISTS history ( id INTEGER PRIMARY KEY, input TEXT NOT NULL, output TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) - " - ).unwrap(); + ", + ) + .unwrap(); connection } -pub async fn insert_history(input: String, output: String) -> Result<(), Box> { +pub async fn insert_history( + input: String, + output: String, +) -> Result<(), Box> { let connection = connect_history().await; let query = "INSERT INTO history (input, output) VALUES (:input, :output)"; let mut statement = connection.prepare(query).unwrap(); - statement.bind_iter::<_, (_, Value)>([ - (":input", input.into()), - (":output", output.into()), - ])?; + statement.bind_iter::<_, (_, Value)>([(":input", input.into()), (":output", output.into())])?; statement.next().unwrap(); @@ -58,18 +62,18 @@ pub async fn insert_history(input: String, output: String) -> Result<(), Box Result<(), Box> { - let limit = history.limit; let condition = format!("%{}%", history.query.unwrap_or("".to_string())).to_string(); - + let connection = connect_history().await; let mut rows: Vec = Vec::new(); - let query = "SELECT * FROM history WHERE input LIKE :query ORDER BY created_at DESC LIMIT :limit"; + let query = + "SELECT * FROM history WHERE input LIKE :query ORDER BY created_at DESC LIMIT :limit"; let mut statement = connection.prepare(query).unwrap(); statement.bind_iter::<_, (_, Value)>([ (":query", condition.into()), - (":limit", limit.to_string().into()) + (":limit", limit.to_string().into()), ])?; while let Ok(State::Row) = statement.next() { @@ -85,14 +89,12 @@ pub async fn handle_history(history: History) -> Result<(), Box() - ); + table.with(Width::wrap(width as usize).priority::()); println!("{}", table); Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7e9338f..06f98cd 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,6 @@ +mod history; mod search; mod set; -mod history; use clap::Subcommand; diff --git a/src/commands/search/mod.rs b/src/commands/search/mod.rs index d593f53..0f43d00 100644 --- a/src/commands/search/mod.rs +++ b/src/commands/search/mod.rs @@ -1,3 +1,4 @@ +use super::history::insert_history; use clap::Parser; use cli_clipboard::{ClipboardContext, ClipboardProvider}; use llm_chain::{ @@ -11,7 +12,6 @@ use llm_chain::{ use llm_chain_openai::chatgpt::Model; use spinners::{Spinner, Spinners}; use std::env; -use super::history::insert_history; #[derive(Debug, Parser)] #[clap(name = "search", about = "Search a command from the LLM model")]