added search function
This commit is contained in:
parent
5769965c6c
commit
af1f57df36
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "calcifer"
|
name = "calcifer"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -18,3 +18,4 @@ image = "0.24.8"
|
||||||
serde = { version = "1.0.195", features = ["derive"] }
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
serde_json = "1.0.111"
|
serde_json = "1.0.111"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"tabs":["/home/penwing/Documents/projects/rust/calcifer/src/main.rs","/home/penwing/Documents/notes/victory2.txt"],"theme":6}
|
{"tabs":["/home/penwing/Documents/projects/rust/calcifer/src/main.rs","/home/penwing/Documents/notes/victory2.txt","/home/penwing/Documents/projects/rust/calcifer/src/tools/search.rs"],"theme":6}
|
|
@ -8,7 +8,7 @@ pub mod search;
|
||||||
|
|
||||||
|
|
||||||
pub trait View {
|
pub trait View {
|
||||||
fn ui(&mut self, ui: &mut egui::Ui);
|
fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Something to view
|
/// Something to view
|
||||||
|
@ -26,7 +26,7 @@ pub trait Demo {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum TabNumber {
|
pub enum TabNumber {
|
||||||
None,
|
None,
|
||||||
Open,
|
Open,
|
||||||
|
|
|
@ -2,7 +2,17 @@ use eframe::egui;
|
||||||
use crate::tools::{View, Demo, Tab, TabNumber};
|
use crate::tools::{View, Demo, Tab, TabNumber};
|
||||||
|
|
||||||
|
|
||||||
|
enum Action {
|
||||||
|
Next,
|
||||||
|
Previous,
|
||||||
|
Replace,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Selection {
|
pub struct Selection {
|
||||||
|
pub tab: TabNumber,
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
}
|
}
|
||||||
|
@ -11,6 +21,7 @@ pub struct Selection {
|
||||||
impl Default for Selection {
|
impl Default for Selection {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
tab: TabNumber::Zero,
|
||||||
start: 0,
|
start: 0,
|
||||||
end: 0,
|
end: 0,
|
||||||
}
|
}
|
||||||
|
@ -20,7 +31,9 @@ impl Default for Selection {
|
||||||
|
|
||||||
pub struct SearchWindow {
|
pub struct SearchWindow {
|
||||||
search_text: String,
|
search_text: String,
|
||||||
result: Selection,
|
searched_text: String,
|
||||||
|
results: Vec<Selection>,
|
||||||
|
current_result: Selection,
|
||||||
across_documents: bool,
|
across_documents: bool,
|
||||||
replace_text: String,
|
replace_text: String,
|
||||||
}
|
}
|
||||||
|
@ -30,7 +43,9 @@ impl Default for SearchWindow {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
search_text: "".into(),
|
search_text: "".into(),
|
||||||
result: Selection::default(),
|
searched_text: "".into(),
|
||||||
|
results: vec![],
|
||||||
|
current_result: Selection::default(),
|
||||||
across_documents: false,
|
across_documents: false,
|
||||||
replace_text: "".into(),
|
replace_text: "".into(),
|
||||||
}
|
}
|
||||||
|
@ -48,29 +63,35 @@ impl Demo for SearchWindow {
|
||||||
.open(open)
|
.open(open)
|
||||||
.vscroll(true)
|
.vscroll(true)
|
||||||
.hscroll(true)
|
.hscroll(true)
|
||||||
.show(ctx, |ui| self.ui(ui));
|
.show(ctx, |ui| self.ui(ui, tabs, selected_tab));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl View for SearchWindow {
|
impl View for SearchWindow {
|
||||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber) {
|
||||||
ui.set_min_width(250.0);
|
ui.set_min_width(250.0);
|
||||||
|
|
||||||
|
let mut action : Action = Action::None;
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
let Self { search_text, .. } = self;
|
let Self { search_text, .. } = self;
|
||||||
ui.add(egui::TextEdit::singleline(search_text).desired_width(120.0).lock_focus(true));
|
ui.add(egui::TextEdit::singleline(search_text).desired_width(120.0).lock_focus(true));
|
||||||
|
|
||||||
if ui.add(egui::Button::new("<")).clicked() {
|
ui.label(format!("{} ", self.results.len()));
|
||||||
self.find_previous();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ui.add(egui::Button::new(">")).clicked() {
|
if ui.add(egui::Button::new("<")).clicked() {
|
||||||
self.find_next();
|
action = Action::Previous;
|
||||||
|
} else if ui.add(egui::Button::new(">")).clicked() {
|
||||||
|
action = Action::Next;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let previous_bool_state = self.across_documents.clone();
|
||||||
ui.checkbox(&mut self.across_documents, "Across documents");
|
ui.checkbox(&mut self.across_documents, "Across documents");
|
||||||
|
if previous_bool_state != self.across_documents {
|
||||||
|
self.searched_text = "".into();
|
||||||
|
}
|
||||||
|
|
||||||
egui::CollapsingHeader::new("Replace")
|
egui::CollapsingHeader::new("Replace")
|
||||||
.default_open(false)
|
.default_open(false)
|
||||||
|
@ -79,23 +100,68 @@ impl View for SearchWindow {
|
||||||
let Self { replace_text, .. } = self;
|
let Self { replace_text, .. } = self;
|
||||||
ui.add(egui::TextEdit::singleline(replace_text).desired_width(120.0).lock_focus(true));
|
ui.add(egui::TextEdit::singleline(replace_text).desired_width(120.0).lock_focus(true));
|
||||||
if ui.add(egui::Button::new("Replace")).clicked() {
|
if ui.add(egui::Button::new("Replace")).clicked() {
|
||||||
self.replace_text();
|
action = Action::Replace;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
match action {
|
||||||
|
Action::Next => self.find_next(tabs, selected_tab),
|
||||||
|
Action::Previous => self.find_previous(tabs, selected_tab),
|
||||||
|
Action::Replace => self.replace(tabs, selected_tab),
|
||||||
|
Action::None => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SearchWindow {
|
impl SearchWindow {
|
||||||
fn find_previous(&self) {
|
fn search(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
|
||||||
println!("Searched for previous");
|
let mut search_results: Vec<Selection> = vec![];
|
||||||
|
|
||||||
|
if self.across_documents {
|
||||||
|
for (index, tab) in tabs.iter().enumerate() {
|
||||||
|
search_results.extend(self.match_text(tab.code.clone(), TabNumber::from_index(index)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
search_results.extend(self.match_text(tabs[TabNumber::to_index(&selected_tab.clone())].code.clone(), selected_tab.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Found {} results", search_results.len());
|
||||||
|
self.searched_text = self.search_text.clone();
|
||||||
|
self.results = search_results.clone();
|
||||||
|
|
||||||
|
if self.results.len() > 0 {
|
||||||
|
self.current_result = self.results[0].clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_next(&self) {
|
fn match_text(&self, tab_text: String, tab_number: TabNumber) -> Vec<Selection> {
|
||||||
println!("Searched for next");
|
let matches = tab_text.match_indices(&self.search_text.clone()).map(|(i, _)| Selection {
|
||||||
|
tab : tab_number.clone(),
|
||||||
|
start: i,
|
||||||
|
end: i + self.search_text.len(),
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
matches
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_text(&self) {
|
fn find_next(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
|
||||||
|
if self.searched_text != self.search_text && self.search_text.len() > 1 {
|
||||||
|
self.search(tabs, selected_tab);
|
||||||
|
} else {
|
||||||
|
println!("just need to get next result");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_previous(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
|
||||||
|
if self.searched_text != self.search_text && self.search_text.len() > 1 {
|
||||||
|
self.search(tabs, selected_tab);
|
||||||
|
} else {
|
||||||
|
println!("just need to get next result");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace(&mut self, tabs: &Vec<Tab>, selected_tab: &TabNumber) {
|
||||||
println!("Searched to replace {} with {}", &self.search_text, &self.replace_text);
|
println!("Searched to replace {} with {}", &self.search_text, &self.replace_text);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue