145 lines
4.2 KiB
Rust
145 lines
4.2 KiB
Rust
#![windows_subsystem = "windows"]
|
|
use bevy::{prelude::*, winit::WinitSettings};
|
|
use bevy_egui::{
|
|
egui::{self},
|
|
EguiContexts, EguiPlugin,
|
|
};
|
|
|
|
#[cfg(windows)]
|
|
const LINE_ENDING: &str = "\r\n";
|
|
#[cfg(not(windows))]
|
|
const LINE_ENDING: &str = "\n";
|
|
|
|
#[derive(Resource, Debug)]
|
|
struct InputText {
|
|
value: String,
|
|
}
|
|
|
|
#[derive(Resource, Debug)]
|
|
struct OutputText {
|
|
value: String,
|
|
}
|
|
|
|
#[derive(Resource, Debug)]
|
|
struct NeedsSort {
|
|
value: bool,
|
|
}
|
|
|
|
#[derive(Resource, Debug)]
|
|
struct NeedsStrip {
|
|
value: bool,
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.insert_resource(InputText {
|
|
value: "".to_string(),
|
|
})
|
|
.insert_resource(OutputText {
|
|
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(),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}))
|
|
.add_plugins(EguiPlugin)
|
|
.add_systems(Update, ui_system)
|
|
.add_systems(Update, process_text)
|
|
.run();
|
|
}
|
|
|
|
fn process_text(
|
|
mut output_text: ResMut<OutputText>,
|
|
mut needs_sort: ResMut<NeedsSort>,
|
|
mut needs_strip: ResMut<NeedsStrip>,
|
|
input_text: Res<InputText>,
|
|
) {
|
|
let needs_processing = needs_sort.value || needs_strip.value;
|
|
|
|
if needs_processing {
|
|
let mut arr = input_text.value.lines().collect::<Vec<_>>();
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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());
|
|
|
|
egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| {
|
|
let window_size = ui.available_size();
|
|
let height = window_size[1];
|
|
|
|
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");
|
|
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());
|
|
}
|
|
|
|
let clear_button = ui.button("Clear");
|
|
if clear_button.clicked() {
|
|
input_text.value = "".to_string();
|
|
needs_sort.value = true;
|
|
}
|
|
});
|
|
|
|
ui.with_layout(
|
|
egui::Layout::left_to_right(egui::Align::Center).with_cross_justify(true),
|
|
|ui| {
|
|
egui::ScrollArea::vertical()
|
|
.id_source("left")
|
|
.show(ui, |ui| {
|
|
ui.add_sized(
|
|
[ui.available_width() / 2., height],
|
|
egui::TextEdit::multiline(&mut input_text.value),
|
|
);
|
|
});
|
|
|
|
egui::ScrollArea::vertical()
|
|
.id_source("right")
|
|
.show(ui, |ui| {
|
|
ui.with_layout(
|
|
egui::Layout::top_down_justified(egui::Align::LEFT),
|
|
|ui| {
|
|
ui.add(egui::Label::new(&output_text.value).selectable(true));
|
|
},
|
|
)
|
|
});
|
|
},
|
|
);
|
|
});
|
|
});
|
|
}
|