Skip to content

Commit

Permalink
hook up new search popup
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst committed Aug 16, 2023
1 parent 1e928ce commit 3a82b93
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 42 deletions.
52 changes: 29 additions & 23 deletions asyncgit/src/sync/logwalker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,33 @@ bitflags! {

impl Default for FilterSearchOptions {
fn default() -> Self {
Self::all()
Self::MESSAGE
}
}

///
#[derive(Default, Debug)]
pub struct LogFilterSearchOptions {
///
pub search_pattern: String,
///
pub filters: FilterSearchOptions,
}

///
#[derive(Default)]
pub struct LogFilterSearch {
///
pub matcher: fuzzy_matcher::skim::SkimMatcherV2,
///
pub search_pattern: String,
///
pub options: FilterSearchOptions,
pub options: LogFilterSearchOptions,
}

impl LogFilterSearch {
///
pub fn new(
search_pattern: String,
options: FilterSearchOptions,
) -> Self {
pub fn new(options: LogFilterSearchOptions) -> Self {
Self {
matcher: fuzzy_matcher::skim::SkimMatcherV2::default(),
search_pattern,
options,
}
}
Expand All @@ -114,7 +117,7 @@ impl LogFilterSearch {
self.matcher
.fuzzy_match(
file,
self.search_pattern.as_str(),
self.options.search_pattern.as_str(),
)
.is_some()
})
Expand All @@ -131,7 +134,7 @@ impl LogFilterSearch {
self.matcher
.fuzzy_match(
file,
self.search_pattern.as_str(),
self.options.search_pattern.as_str(),
)
.is_some()
})
Expand All @@ -152,12 +155,13 @@ pub fn filter_commit_by_search(

let msg_match = filter
.options
.filters
.contains(FilterSearchOptions::MESSAGE)
.then(|| {
commit.message().and_then(|msg| {
filter.matcher.fuzzy_match(
msg,
filter.search_pattern.as_str(),
filter.options.search_pattern.as_str(),
)
})
})
Expand All @@ -166,6 +170,7 @@ pub fn filter_commit_by_search(

let file_match = filter
.options
.filters
.contains(FilterSearchOptions::FILENAMES)
.then(|| {
get_commit_diff(
Expand Down Expand Up @@ -263,7 +268,6 @@ mod tests {
commit, get_commits_info, stage_add_file,
tests::repo_init_empty,
};
use fuzzy_matcher::skim::SkimMatcherV2;
use pretty_assertions::assert_eq;
use std::{fs::File, io::Write, path::Path};

Expand Down Expand Up @@ -389,11 +393,12 @@ mod tests {
);
write_commit_file(&repo, "foo", "b", "commit3");

let log_filter = filter_commit_by_search(LogFilterSearch {
options: FilterSearchOptions::MESSAGE,
matcher: SkimMatcherV2::default(),
search_pattern: String::from("my msg"),
});
let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
filters: FilterSearchOptions::MESSAGE,
search_pattern: String::from("my msg"),
}),
);

let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
Expand All @@ -404,11 +409,12 @@ mod tests {
assert_eq!(items.len(), 1);
assert_eq!(items[0], second_commit_id);

let log_filter = filter_commit_by_search(LogFilterSearch {
options: FilterSearchOptions::FILENAMES,
matcher: SkimMatcherV2::default(),
search_pattern: String::from("fo"),
});
let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
filters: FilterSearchOptions::FILENAMES,
search_pattern: String::from("fo"),
}),
);

let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
Expand Down
3 changes: 2 additions & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ pub use hunks::{reset_hunk, stage_hunk, unstage_hunk};
pub use ignore::add_to_ignore;
pub use logwalker::{
diff_contains_file, filter_commit_by_search, FilterSearchOptions,
LogFilterSearch, LogWalker, LogWalkerFilter,
LogFilterSearch, LogFilterSearchOptions, LogWalker,
LogWalkerFilter,
};
pub use merge::{
abort_pending_rebase, abort_pending_state,
Expand Down
3 changes: 3 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ impl App {
InternalEvent::OpenResetPopup(id) => {
self.reset_popup.open(id)?;
}
InternalEvent::CommitSearch(options) => {
self.revlog.search(options);
}
};

Ok(flags)
Expand Down
47 changes: 39 additions & 8 deletions src/components/log_search_popup.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#![allow(dead_code)]

use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventState, TextInputComponent,
};
use crate::{
keys::{key_match, SharedKeyConfig},
queue::Queue,
queue::{InternalEvent, Queue},
strings::{self},
ui::{self, style::SharedTheme},
};
use anyhow::Result;
use asyncgit::sync::FilterSearchOptions;
use asyncgit::sync::{FilterSearchOptions, LogFilterSearchOptions};
use crossterm::event::Event;
use ratatui::{
backend::Backend,
layout::{
Alignment, Constraint, Direction, Layout, Margin, Rect,
},
style::Style,
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph},
Frame,
Expand Down Expand Up @@ -69,6 +66,13 @@ impl LogSearchPopupComponent {

fn execute_search(&mut self) {
self.hide();

self.queue.push(InternalEvent::CommitSearch(
LogFilterSearchOptions {
filters: self.options,
search_pattern: self.find_text.get_text().to_string(),
},
));
}

fn get_text(&self) -> Vec<Line> {
Expand All @@ -81,10 +85,37 @@ impl LogSearchPopupComponent {
" "
};

let x_files = if self
.options
.contains(FilterSearchOptions::FILENAMES)
{
"X"
} else {
" "
};

let theme = self.theme.text(false, false);
txt.push(Line::from(vec![Span::styled(
"[X] fuzzy search",
theme,
)]));
txt.push(Line::from(vec![Span::styled(
format!("[{x_message}] messages",),
theme,
)]));
txt.push(Line::from(vec![Span::styled(
format!("[{x_files}] commited files",),
theme,
)]));
txt.push(Line::from(vec![Span::styled(
format!("[{x_message}] message",),
Style::default(),
"[ ] changes",
theme,
)]));
txt.push(Line::from(vec![Span::styled(
"[ ] authors",
theme,
)]));
txt.push(Line::from(vec![Span::styled("[ ] hashes", theme)]));

txt
}
Expand All @@ -97,7 +128,7 @@ impl DrawableComponent for LogSearchPopupComponent {
area: Rect,
) -> Result<()> {
if self.is_visible() {
const SIZE: (u16, u16) = (60, 8);
const SIZE: (u16, u16) = (60, 10);
let area =
ui::centered_rect_absolute(SIZE.0, SIZE.1, area);

Expand Down
6 changes: 5 additions & 1 deletion src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::{
tabs::StashingOptions,
};
use asyncgit::{
sync::{diff::DiffLinePosition, CommitId},
sync::{
diff::DiffLinePosition, CommitId, LogFilterSearchOptions,
},
PushType,
};
use bitflags::bitflags;
Expand Down Expand Up @@ -132,6 +134,8 @@ pub enum InternalEvent {
OpenResetPopup(CommitId),
///
RewordCommit(CommitId),
///
CommitSearch(LogFilterSearchOptions),
}

/// single threaded simple queue for components to communicate with each other
Expand Down
16 changes: 7 additions & 9 deletions src/tabs/revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use anyhow::Result;
use asyncgit::{
asyncjob::AsyncSingleJob,
sync::{
self, filter_commit_by_search, CommitId, FilterSearchOptions,
LogFilterSearch, RepoPathRef,
self, filter_commit_by_search, CommitId, LogFilterSearch,
LogFilterSearchOptions, RepoPathRef,
},
AsyncBranchesJob, AsyncGitNotification, AsyncLog, AsyncTags,
CommitFilesParams, FetchStatus,
Expand Down Expand Up @@ -273,15 +273,13 @@ impl Revlog {
}
}

fn find(&mut self) {
pub fn search(&mut self, options: LogFilterSearchOptions) {
if self.git_log_find.is_none() {
log::info!("start search");
log::info!("start search: {:?}", options);

let filter =
filter_commit_by_search(LogFilterSearch::new(
String::from("README.md"),
FilterSearchOptions::all(),
));
let filter = filter_commit_by_search(
LogFilterSearch::new(options),
);

let async_find = AsyncLog::new(
self.repo.borrow().clone(),
Expand Down

0 comments on commit 3a82b93

Please sign in to comment.