-
Notifications
You must be signed in to change notification settings - Fork 0
Add command to store and display previous search history in the database #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ykdy3951
merged 5 commits into
main
from
17-add-command-to-store-and-display-previous-search-history-in-database
Mar 6, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ab9d81
Add SQLite, tabled, and terminal_size dependencies, and implement his…
ykdy3951 99d0534
Add query parameter to history command
ykdy3951 1bf9697
Fix binding values in handle_history function
ykdy3951 1a988ff
Refactor history module and fix import order
ykdy3951 9c844b4
Add offset parameter to history command
ykdy3951 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| use clap::Args; | ||
| use sqlite::{Connection, State, Value}; | ||
| use tabled::{ | ||
| settings::{peaker::PriorityMax, Width}, | ||
| Table, Tabled, | ||
| }; | ||
|
|
||
| #[derive(Debug, Args)] | ||
| #[clap(name = "history", about = "Display the previous search history")] | ||
| pub struct History { | ||
| #[clap(short, long, default_value = "10")] | ||
| limit: i64, | ||
|
|
||
| #[clap(short, long, default_value = "0")] | ||
| offset: i64, | ||
|
|
||
| #[clap(short, long)] | ||
| query: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Tabled)] | ||
| pub struct HistoryRow { | ||
| id: i64, | ||
| input: String, | ||
| output: String, | ||
| created_at: String, | ||
| } | ||
|
|
||
| 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( | ||
| " | ||
| 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(); | ||
|
|
||
| connection | ||
| } | ||
|
|
||
| pub async fn insert_history( | ||
| input: String, | ||
| output: String, | ||
| ) -> Result<(), Box<dyn std::error::Error>> { | ||
| 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.next().unwrap(); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn handle_history(history: History) -> Result<(), Box<dyn std::error::Error>> { | ||
| let limit = history.limit; | ||
| let condition = format!("%{}%", history.query.unwrap_or("".to_string())).to_string(); | ||
|
|
||
| let connection = connect_history().await; | ||
| let mut rows: Vec<HistoryRow> = Vec::new(); | ||
|
|
||
| let query = | ||
| "SELECT * FROM history WHERE input LIKE :query ORDER BY created_at DESC LIMIT :limit OFFSET :offset"; | ||
| let mut statement = connection.prepare(query).unwrap(); | ||
| statement.bind_iter::<_, (_, Value)>([ | ||
| (":query", condition.into()), | ||
| (":limit", limit.to_string().into()), | ||
| (":offset", history.offset.to_string().into()), | ||
| ])?; | ||
|
|
||
| while let Ok(State::Row) = statement.next() { | ||
| let id: i64 = statement.read(0)?; | ||
| let input: String = statement.read(1)?; | ||
| let output: String = statement.read(2)?; | ||
| let created_at: String = statement.read(3)?; | ||
|
|
||
| rows.push(HistoryRow { | ||
| id, | ||
| input, | ||
| output, | ||
| created_at, | ||
| }); | ||
| } | ||
|
|
||
| let terminal_size::Width(width) = terminal_size::terminal_size().unwrap().0; | ||
| let mut table = Table::new(rows); | ||
| table.with(Width::wrap(width as usize).priority::<PriorityMax>()); | ||
|
|
||
| println!("{}", table); | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.