Skip to content

feat: highlight matches in interactive search #2653

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/atuin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ tracing-subscriber = { workspace = true }
uuid = { workspace = true }
sysinfo = "0.30.7"
regex = "1.10.5"
norm = { version = "0.1.1", features = ["fzf-v2"] }

[target.'cfg(any(target_os = "windows", target_os = "macos"))'.dependencies]
arboard = { version = "3.4", optional = true }
Expand Down
1 change: 1 addition & 0 deletions crates/atuin/src/command/client/search/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ pub trait SearchEngine: Send + Sync + 'static {
self.full_query(state, db).await
}
}
fn get_highlight_indices(&self, command: &str, search_input: &str) -> Vec<usize>;
}
20 changes: 18 additions & 2 deletions crates/atuin/src/command/client/search/engines/db.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::{SearchEngine, SearchState};
use async_trait::async_trait;
use atuin_client::{
database::Database, database::OptFilters, history::History, settings::SearchMode,
};
use eyre::Result;

use super::{SearchEngine, SearchState};
use norm::Metric;
use norm::fzf::{FzfParser, FzfV2};
use std::ops::Range;

pub struct Search(pub SearchMode);

Expand All @@ -30,4 +32,18 @@ impl SearchEngine for Search {
// ignore errors as it may be caused by incomplete regex
.map_or(Vec::new(), |r| r.into_iter().collect()))
}

fn get_highlight_indices(&self, command: &str, search_input: &str) -> Vec<usize> {
if self.0 == SearchMode::Prefix {
return vec![];
}
let mut fzf = FzfV2::new();
let mut parser = FzfParser::new();
let query = parser.parse(search_input);
let mut ranges: Vec<Range<usize>> = Vec::new();
let _ = fzf.distance_and_ranges(query, command, &mut ranges);

// convert ranges to all indices
ranges.into_iter().flatten().collect()
}
}
8 changes: 8 additions & 0 deletions crates/atuin/src/command/client/search/engines/skim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ impl SearchEngine for Search {

Ok(fuzzy_search(&self.engine, state, &self.all_history).await)
}

fn get_highlight_indices(&self, command: &str, search_input: &str) -> Vec<usize> {
let (_, indices) = self
.engine
.fuzzy_indices(command, search_input)
.unwrap_or_default();
indices
}
}

async fn fuzzy_search(
Expand Down
54 changes: 48 additions & 6 deletions crates/atuin/src/command/client/search/history_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::time::Duration;

use super::duration::format_duration;
use super::engines::SearchEngine;
use atuin_client::{
history::History,
theme::{Meaning, Theme},
};
use atuin_common::utils::Escapable as _;
use itertools::Itertools;
use ratatui::{
buffer::Buffer,
crossterm::style,
Expand All @@ -14,7 +17,17 @@ use ratatui::{
};
use time::OffsetDateTime;

use super::duration::format_duration;
pub struct HistoryHighlighter<'a> {
pub engine: &'a dyn SearchEngine,
pub search_input: &'a str,
}

impl HistoryHighlighter<'_> {
pub fn get_highlight_indices(&self, command: &str) -> Vec<usize> {
self.engine
.get_highlight_indices(command, self.search_input)
}
}

pub struct HistoryList<'a> {
history: &'a [History],
Expand All @@ -25,6 +38,7 @@ pub struct HistoryList<'a> {
now: &'a dyn Fn() -> OffsetDateTime,
indicator: &'a str,
theme: &'a Theme,
history_highlighter: HistoryHighlighter<'a>,
}

#[derive(Default)]
Expand Down Expand Up @@ -78,6 +92,7 @@ impl StatefulWidget for HistoryList<'_> {
now: &self.now,
indicator: self.indicator,
theme: self.theme,
history_highlighter: self.history_highlighter,
};

for item in self.history.iter().skip(state.offset).take(end - start) {
Expand All @@ -101,6 +116,7 @@ impl<'a> HistoryList<'a> {
now: &'a dyn Fn() -> OffsetDateTime,
indicator: &'a str,
theme: &'a Theme,
history_highlighter: HistoryHighlighter<'a>,
) -> Self {
Self {
history,
Expand All @@ -110,6 +126,7 @@ impl<'a> HistoryList<'a> {
now,
indicator,
theme,
history_highlighter,
}
}

Expand Down Expand Up @@ -144,6 +161,7 @@ struct DrawState<'a> {
now: &'a dyn Fn() -> OffsetDateTime,
indicator: &'a str,
theme: &'a Theme,
history_highlighter: HistoryHighlighter<'a>,
}

// longest line prefix I could come up with
Expand Down Expand Up @@ -203,21 +221,45 @@ impl DrawState<'_> {

fn command(&mut self, h: &History) {
let mut style = self.theme.as_style(Meaning::Base);
let mut row_highlighted = false;
if !self.alternate_highlight && (self.y as usize + self.state.offset == self.state.selected)
{
row_highlighted = true;
// if not applying alternative highlighting to the whole row, color the command
style = self.theme.as_style(Meaning::AlertError);
style.attributes.set(style::Attribute::Bold);
}

let highlight_indices = self.history_highlighter.get_highlight_indices(
h.command
.escape_control()
.split_ascii_whitespace()
.join(" ")
.as_str(),
);

let mut pos = 0;
for section in h.command.escape_control().split_ascii_whitespace() {
self.draw(" ", style.into());
if self.x > self.list_area.width {
// Avoid attempting to draw a command section beyond the width
// of the list
return;
for ch in section.chars() {
if self.x > self.list_area.width {
// Avoid attempting to draw a command section beyond the width
// of the list
return;
}
let mut style = style;
if highlight_indices.contains(&pos) {
if row_highlighted {
// if the row is highlighted bold is not enough as the whole row is bold
// change the color too
style = self.theme.as_style(Meaning::AlertWarn);
}
style.attributes.set(style::Attribute::Bold);
}
self.draw(&ch.to_string(), style.into());
pos += 1;
}
self.draw(section, style.into());
pos += 1;
}
}

Expand Down
19 changes: 13 additions & 6 deletions crates/atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use semver::Version;
use time::OffsetDateTime;
use unicode_width::UnicodeWidthStr;

use super::{
cursor::Cursor,
engines::{SearchEngine, SearchState},
history_list::{HistoryList, ListState, PREFIX_LENGTH},
};
use atuin_client::{
database::{Database, current_context},
history::{History, HistoryStats, store::HistoryStore},
Expand All @@ -18,12 +23,7 @@ use atuin_client::{
},
};

use super::{
cursor::Cursor,
engines::{SearchEngine, SearchState},
history_list::{HistoryList, ListState, PREFIX_LENGTH},
};

use crate::command::client::search::history_list::HistoryHighlighter;
use crate::command::client::theme::{Meaning, Theme};
use crate::{VERSION, command::client::search::engines};

Expand Down Expand Up @@ -728,13 +728,18 @@ impl State {

match self.tab_index {
0 => {
let history_highlighter = HistoryHighlighter {
engine: self.engine.as_ref(),
search_input: self.search.input.as_str(),
};
let results_list = Self::build_results_list(
style,
results,
self.keymap_mode,
&self.now,
indicator.as_str(),
theme,
history_highlighter,
);
f.render_stateful_widget(results_list, results_list_chunk, &mut self.results_state);
}
Expand Down Expand Up @@ -878,6 +883,7 @@ impl State {
now: &'a dyn Fn() -> OffsetDateTime,
indicator: &'a str,
theme: &'a Theme,
history_highlighter: HistoryHighlighter<'a>,
) -> HistoryList<'a> {
let results_list = HistoryList::new(
results,
Expand All @@ -886,6 +892,7 @@ impl State {
now,
indicator,
theme,
history_highlighter,
);

if style.compact {
Expand Down
Loading