From 508d4f476157384b0d454bee3dd6e9256560561b Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 22 Apr 2022 21:05:02 +0100 Subject: History filter (#329) * Add history filters, and hotkey toggle Switch between different search modes to narrow down the history you want - global search for all history, host for all history from your current machine, session for the current shell session, and directory for the current directory The default can be configured via `filter_mode` * Update docs * Add context --- src/command/client/search.rs | 74 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 8 deletions(-) (limited to 'src/command/client/search.rs') diff --git a/src/command/client/search.rs b/src/command/client/search.rs index a1dc5aa9..a913e161 100644 --- a/src/command/client/search.rs +++ b/src/command/client/search.rs @@ -1,6 +1,7 @@ use chrono::Utc; use clap::Parser; use eyre::Result; +use std::env; use std::{io::stdout, ops::Sub, time::Duration}; use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen}; use tui::{ @@ -14,9 +15,11 @@ use tui::{ use unicode_width::UnicodeWidthStr; use atuin_client::{ + database::current_context, + database::Context, database::Database, history::History, - settings::{SearchMode, Settings}, + settings::{FilterMode, SearchMode, Settings}, }; use super::event::{Event, Events}; @@ -91,9 +94,13 @@ impl Cmd { struct State { input: String, + filter_mode: FilterMode, + results: Vec, results_state: ListState, + + context: Context, } impl State { @@ -233,8 +240,14 @@ async fn query_results( db: &mut (impl Database + Send + Sync), ) -> Result<()> { let results = match app.input.as_str() { - "" => db.list(Some(200), true).await?, - i => db.search(Some(200), search_mode, i).await?, + "" => { + db.list(app.filter_mode, &app.context, Some(200), true) + .await? + } + i => { + db.search(Some(200), search_mode, app.filter_mode, &app.context, i) + .await? + } }; app.results = results; @@ -300,6 +313,16 @@ async fn key_handler( app.input = String::from(""); query_results(app, search_mode, db).await.unwrap(); } + Key::Ctrl('r') => { + app.filter_mode = match app.filter_mode { + FilterMode::Global => FilterMode::Host, + FilterMode::Host => FilterMode::Session, + FilterMode::Session => FilterMode::Directory, + FilterMode::Directory => FilterMode::Global, + }; + + query_results(app, search_mode, db).await.unwrap(); + } Key::Down | Key::Ctrl('n') => { let i = match app.results_state.selected() { Some(i) => { @@ -376,8 +399,15 @@ fn draw(f: &mut Frame<'_, T>, history_count: i64, app: &mut State) { let help = Text::from(Spans::from(help)); let help = Paragraph::new(help); + let filter_mode = match app.filter_mode { + FilterMode::Global => "GLOBAL", + FilterMode::Host => "HOST", + FilterMode::Session => "SESSION", + FilterMode::Directory => "DIRECTORY", + }; + let input = Paragraph::new(app.input.clone()) - .block(Block::default().borders(Borders::ALL).title("Query")); + .block(Block::default().borders(Borders::ALL).title(filter_mode)); let stats = Paragraph::new(Text::from(Span::raw(format!( "history count: {}", @@ -451,7 +481,15 @@ fn draw_compact(f: &mut Frame<'_, T>, history_count: i64, app: &mut .style(Style::default().fg(Color::DarkGray)) .alignment(Alignment::Right); - let input = Paragraph::new(format!("] {}", app.input.clone())).block(Block::default()); + let filter_mode = match app.filter_mode { + FilterMode::Global => "GLOBAL", + FilterMode::Host => "HOST", + FilterMode::Session => "SESSION", + FilterMode::Directory => "DIRECTORY", + }; + + let input = + Paragraph::new(format!("{}] {}", filter_mode, app.input.clone())).block(Block::default()); f.render_widget(title, header_chunks[0]); f.render_widget(help, header_chunks[1]); @@ -460,9 +498,11 @@ fn draw_compact(f: &mut Frame<'_, T>, history_count: i64, app: &mut app.render_results(f, chunks[1], Block::default()); f.render_widget(input, chunks[2]); + let extra_width = app.input.width() + filter_mode.len(); + f.set_cursor( // Put cursor past the end of the input text - chunks[2].x + app.input.width() as u16 + 2, + chunks[2].x + extra_width as u16 + 2, // Move one line down, from the border to the input line chunks[2].y + 1, ); @@ -475,6 +515,7 @@ fn draw_compact(f: &mut Frame<'_, T>, history_count: i64, app: &mut async fn select_history( query: &[String], search_mode: SearchMode, + filter_mode: FilterMode, style: atuin_client::settings::Style, db: &mut (impl Database + Send + Sync), ) -> Result { @@ -491,6 +532,8 @@ async fn select_history( input: query.join(" "), results: Vec::new(), results_state: ListState::default(), + context: current_context(), + filter_mode, }; query_results(&mut app, search_mode, db).await?; @@ -551,11 +594,26 @@ pub async fn run( }; if interactive { - let item = select_history(query, settings.search_mode, settings.style, db).await?; + let item = select_history( + query, + settings.search_mode, + settings.filter_mode, + settings.style, + db, + ) + .await?; eprintln!("{}", item); } else { + let context = current_context(); + let results = db - .search(None, settings.search_mode, query.join(" ").as_str()) + .search( + None, + settings.search_mode, + settings.filter_mode, + &context, + query.join(" ").as_str(), + ) .await?; // TODO: This filtering would be better done in the SQL query, I just -- cgit v1.3.1