Skip to content

Commit

Permalink
fix: refresh replacement hashmap (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood authored Jun 19, 2022
1 parent a0b36f7 commit 14dcaa4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zeditor"
version = "0.1.6-d"
version = "0.1.6-e"
edition = "2021"

[dependencies]
Expand Down
16 changes: 10 additions & 6 deletions src/config_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use crate::{
db::Db, msg::Msg, quit::quit_button, replace::ReplaceHits, screens::ZeditorScreens,
db::Db, msg::Msg, quit::quit_button, replace::ReplaceCommand, screens::ZeditorScreens,
search::SearchCommand,
};
use cursive::{
Expand All @@ -27,8 +27,8 @@ pub fn render(
siv: &mut Cursive,
screens: ZeditorScreens,
db: Arc<Mutex<Db>>,
replace_s: Sender<Msg<ReplaceHits>>,
search_command_s: Sender<Msg<SearchCommand>>,
replace_s: Sender<Msg<ReplaceCommand>>,
search_s: Sender<Msg<SearchCommand>>,
) {
siv.set_screen(screens.config);

Expand All @@ -38,7 +38,8 @@ pub fn render(
let existing_replace_inputs = ListView::new().with_name(EXISTING_REPLACE_INPUTS);
let new_search_input = TextArea::new().with_name(NEW_SEARCH_INPUT);
let db2 = db.clone();
let scs2 = search_command_s.clone();
let scs2 = search_s.clone();
let rs2 = replace_s.clone();
let new_replace_input = OnEventView::new(TextArea::new().with_name(NEW_REPLACE_INPUT))
.on_event(Event::FocusLost, move |s| {
let mut nri = s.find_name::<TextArea>(NEW_SEARCH_INPUT).unwrap();
Expand Down Expand Up @@ -67,9 +68,12 @@ pub fn render(

nri.set_content("");
nsi.set_content("");
search_command_s
search_s
.send(SearchCommand::RecompileSearch(search_2).into())
.expect("send search command");
replace_s
.send(ReplaceCommand::RefreshSearchReplace.into())
.expect("send replace command");
}
}
},
Expand Down Expand Up @@ -109,7 +113,7 @@ pub fn render(
s.set_screen(screens.home);
}))
.child(DummyView)
.child(quit_button(replace_s, scs2)),
.child(quit_button(rs2, scs2)),
),
)
.title("zeditor"),
Expand Down
26 changes: 13 additions & 13 deletions src/home_screen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::msg::Msg;
use crate::quit::quit_button;
use crate::replace::ReplaceHits;
use crate::replace::ReplaceCommand;
use crate::screens::ZeditorScreens;
use crate::search::{Hit, SearchCommand};
use crate::skip::SkipRepo;
Expand Down Expand Up @@ -31,8 +31,8 @@ const PERM_BUTTONS_SIZE: (usize, usize) = (30, 11);

pub fn render(
siv: &mut Cursive,
replace_hits_s: Sender<Msg<ReplaceHits>>,
search_command_s: Sender<Msg<SearchCommand>>,
replace_s: Sender<Msg<ReplaceCommand>>,
search_s: Sender<Msg<SearchCommand>>,
skip_repo: Arc<Mutex<SkipRepo>>,
screens: ZeditorScreens,
) {
Expand All @@ -45,10 +45,10 @@ pub fn render(
let found_lastsize = LastSizeView::new(found).with_name(FOUND_LASTSIZE);

let perm_buttons = {
let search_s = search_command_s.clone();
let search_s2 = search_command_s.clone();
let replace_s = replace_hits_s.clone();
let replace_s2 = replace_hits_s.clone();
let search_s = search_s.clone();
let search_s2 = search_s.clone();
let replace_s = replace_s.clone();
let replace_s2 = replace_s.clone();

Panel::new(
LinearLayout::vertical()
Expand All @@ -58,7 +58,7 @@ pub fn render(
let visible_lines = count_visible_lines(s).unwrap_or_default();
let visible_hits = take_found_user_data(s, visible_lines);

let msg = ReplaceHits(visible_hits.clone()).into();
let msg = ReplaceCommand::ReplaceHits(visible_hits.clone()).into();

replace_s.send(msg).expect("send")
}))
Expand Down Expand Up @@ -86,12 +86,12 @@ pub fn render(
.title("zeditor"),
);

refresh_found_widget(siv, &replace_hits_s, skip_repo.clone());
refresh_found_widget(siv, &replace_s, skip_repo.clone());
}

fn refresh_found_widget(
siv: &mut Cursive,
replace_hits_s: &Sender<Msg<ReplaceHits>>,
replace_hits_s: &Sender<Msg<ReplaceCommand>>,
skip_repo: Arc<Mutex<SkipRepo>>,
) {
if let Some(mut search_widget) = siv.find_name::<ListView>(FOUND) {
Expand Down Expand Up @@ -138,7 +138,7 @@ fn refresh_found_widget(
.child(DummyView)
.child(Button::new("OK", move |_| {
replace_hits_chan
.send(ReplaceHits(vec![hitc.clone()]).into())
.send(ReplaceCommand::ReplaceHits(vec![hitc.clone()]).into())
.expect("send")
}))
.child(DummyView)
Expand All @@ -156,7 +156,7 @@ fn refresh_found_widget(
pub fn update_found_user_data(
siv: &mut Cursive,
results: Vec<Hit>,
replace_hits_s: &Sender<Msg<ReplaceHits>>,
replace_hits_s: &Sender<Msg<ReplaceCommand>>,
skip_repo: Arc<Mutex<SkipRepo>>,
) {
siv.with_user_data(|state: &mut STATE| {
Expand Down Expand Up @@ -196,7 +196,7 @@ fn take_found_user_data(siv: &mut Cursive, until_lines: usize) -> Vec<Hit> {
fn skip_candidate(
siv: &mut Cursive,
user_data_pos: usize,
replace_hits_s: &Sender<Msg<ReplaceHits>>,
replace_hits_s: &Sender<Msg<ReplaceCommand>>,
skip_repo: Arc<Mutex<SkipRepo>>,
) {
siv.with_user_data(|state: &mut STATE| {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cursive::Cursive;
use std::sync::{Arc, Mutex};
use zeditor::db::Db;
use zeditor::msg::Msg;
use zeditor::replace::{HitsReplaced, ReplaceHits};
use zeditor::replace::{HitsReplaced, ReplaceCommand};
use zeditor::screens::ZeditorScreens;
use zeditor::search::{Hit, SearchCommand};
use zeditor::skip::SkipRepo;
Expand All @@ -23,7 +23,7 @@ async fn main() {

tokio::spawn(async move { zeditor::search::run(db, files_searched_s, search_files_r).await });

let (replace_hits_s, replace_hits_r) = unbounded::<Msg<ReplaceHits>>();
let (replace_hits_s, replace_hits_r) = unbounded::<Msg<ReplaceCommand>>();
let (hits_replaced_s, hits_replaced_r) = unbounded::<HitsReplaced>();

tokio::spawn(async move { zeditor::replace::run(db2, hits_replaced_s, replace_hits_r).await });
Expand Down
4 changes: 2 additions & 2 deletions src/quit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use cursive::{reexports::crossbeam_channel::Sender, views::Button, Cursive};

use crate::{msg::Msg, replace::ReplaceHits, search::SearchCommand};
use crate::{msg::Msg, replace::ReplaceCommand, search::SearchCommand};

pub fn quit_button(
replace: Sender<Msg<ReplaceHits>>,
replace: Sender<Msg<ReplaceCommand>>,
search: Sender<Msg<SearchCommand>>,
) -> Button {
Button::new("Quit", move |s| {
Expand Down
19 changes: 15 additions & 4 deletions src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use tokio::fs::File;
use tokio::io::AsyncWriteExt;

#[derive(Clone)]
pub struct ReplaceHits(pub Vec<Hit>);
pub enum ReplaceCommand {
ReplaceHits(Vec<Hit>),
RefreshSearchReplace,
}

#[derive(Copy, Clone)]
pub enum HitsReplaced {
Expand All @@ -20,9 +23,9 @@ pub enum HitsReplaced {
pub async fn run(
db: Arc<Mutex<Db>>,
hits_replaced_s: Sender<HitsReplaced>,
replace_hits_r: Receiver<Msg<ReplaceHits>>,
replace_hits_r: Receiver<Msg<ReplaceCommand>>,
) {
let search_replace = db
let mut search_replace = db
.lock()
.expect("replace db arc lock")
.get_search_replace()
Expand All @@ -32,7 +35,7 @@ pub async fn run(
select! {
recv(replace_hits_r) -> msg => {
match msg {
Ok(Msg::Event(ReplaceHits(hits))) =>{
Ok(Msg::Event(ReplaceCommand::ReplaceHits(hits))) =>{
let result = if let Ok(_) = replace(&hits, &search_replace).await {
HitsReplaced::Success
} else {
Expand All @@ -42,6 +45,14 @@ pub async fn run(
hits_replaced_s.send(result).expect("send");
}

Ok(Msg::Event(ReplaceCommand::RefreshSearchReplace)) => {
search_replace = db
.lock()
.expect("replace db arc lock")
.get_search_replace()
.expect("replace db fetch");
}

Ok(Msg::Quit) => {
break;
}
Expand Down

0 comments on commit 14dcaa4

Please sign in to comment.