From 854ba3f83e56ca082d5e80fc1a39693bef4f5372 Mon Sep 17 00:00:00 2001 From: Matthew Ryan Dillon Date: Sat, 24 Aug 2024 17:29:46 -0400 Subject: [PATCH] add line stripping, but the workflow is weird --- src/main.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index a475f8f..d32becc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,11 @@ struct NeedsSort { value: bool, } +#[derive(Resource, Debug)] +struct NeedsStrip { + value: bool, +} + fn main() { App::new() .insert_resource(WinitSettings::desktop_app()) @@ -35,6 +40,7 @@ fn main() { value: "".to_string(), }) .insert_resource(NeedsSort { value: false }) + .insert_resource(NeedsStrip { value: false }) .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: "dsort".to_string(), @@ -51,14 +57,25 @@ fn main() { fn process_text( mut output_text: ResMut, mut needs_sort: ResMut, + mut needs_strip: ResMut, input_text: Res, ) { - if needs_sort.value { + let needs_processing = needs_sort.value || needs_strip.value; + + if needs_processing { let mut arr = input_text.value.lines().collect::>(); - arr.retain(|&x| !x.is_empty()); - arr.sort_by(|&a, &b| vsort::compare(a, b)); + + if needs_strip.value { + arr.retain(|&x| !x.is_empty()); + needs_strip.value = false; + } + + if needs_sort.value { + arr.sort_by(|&a, &b| vsort::compare(a, b)); + needs_sort.value = false; + } + output_text.value = arr.join(LINE_ENDING); - needs_sort.value = false; } } @@ -66,6 +83,7 @@ fn ui_system( mut contexts: EguiContexts, mut input_text: ResMut, mut needs_sort: ResMut, + mut needs_strip: ResMut, output_text: Res, ) { contexts.ctx_mut().set_visuals(egui::Visuals::light()); @@ -76,11 +94,16 @@ fn ui_system( ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| { ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| { - let sort_button = ui.button("Sort & Remove Blanks"); + let sort_button = ui.button("Sort"); if sort_button.clicked() { needs_sort.value = true; } + let strip_button = ui.button("Remove Blanks"); + if strip_button.clicked() { + needs_strip.value = true; + } + let copy_button = ui.button("Copy"); if copy_button.clicked() { ui.output_mut(|o| o.copied_text = output_text.value.clone());