add line stripping, but the workflow is weird

This commit is contained in:
Matthew Ryan Dillon 2024-08-24 17:29:46 -04:00
parent 16e5802cba
commit 854ba3f83e

View file

@ -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<OutputText>,
mut needs_sort: ResMut<NeedsSort>,
mut needs_strip: ResMut<NeedsStrip>,
input_text: Res<InputText>,
) {
if needs_sort.value {
let needs_processing = needs_sort.value || needs_strip.value;
if needs_processing {
let mut arr = input_text.value.lines().collect::<Vec<_>>();
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<InputText>,
mut needs_sort: ResMut<NeedsSort>,
mut needs_strip: ResMut<NeedsStrip>,
output_text: Res<OutputText>,
) {
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());