2023-12-30 12:02:58 -05:00
|
|
|
use std::{fs::read_to_string, path::PathBuf};
|
|
|
|
|
|
|
|
use dust_lang::{Interpreter, Map, Result, Value};
|
2023-12-31 16:46:21 -05:00
|
|
|
use egui::{Align, Color32, Layout, RichText};
|
2023-12-30 02:04:39 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct App {
|
2023-12-30 12:02:58 -05:00
|
|
|
path: String,
|
2023-12-30 02:04:39 -05:00
|
|
|
source: String,
|
2023-12-30 12:02:58 -05:00
|
|
|
context: Map,
|
|
|
|
#[serde(skip)]
|
|
|
|
interpreter: Interpreter,
|
2023-12-30 02:04:39 -05:00
|
|
|
output: Result<Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
2023-12-30 12:02:58 -05:00
|
|
|
pub fn new(cc: &eframe::CreationContext<'_>, path: PathBuf) -> Self {
|
|
|
|
fn create_app(path: PathBuf) -> App {
|
|
|
|
let context = Map::new();
|
|
|
|
let mut interpreter = Interpreter::new(context.clone());
|
|
|
|
let read_source = read_to_string(&path);
|
|
|
|
let source = if let Ok(source) = read_source {
|
|
|
|
source
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
let output = interpreter.run(&source);
|
2023-12-30 02:04:39 -05:00
|
|
|
|
2023-12-30 12:02:58 -05:00
|
|
|
App {
|
|
|
|
path: path.to_string_lossy().to_string(),
|
|
|
|
source,
|
|
|
|
context,
|
|
|
|
interpreter,
|
|
|
|
output,
|
|
|
|
}
|
|
|
|
}
|
2023-12-30 02:04:39 -05:00
|
|
|
|
2023-12-31 16:46:21 -05:00
|
|
|
cc.egui_ctx.set_zoom_factor(1.2);
|
|
|
|
|
2023-12-30 12:02:58 -05:00
|
|
|
if path.is_file() {
|
|
|
|
create_app(path)
|
2023-12-30 02:04:39 -05:00
|
|
|
} else {
|
2023-12-30 12:02:58 -05:00
|
|
|
if let Some(storage) = cc.storage {
|
|
|
|
return eframe::get_value(storage, eframe::APP_KEY)
|
|
|
|
.unwrap_or_else(|| create_app(path));
|
|
|
|
} else {
|
|
|
|
create_app(path)
|
|
|
|
}
|
2023-12-30 02:04:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl eframe::App for App {
|
|
|
|
/// Called by the frame work to save state before shutdown.
|
|
|
|
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
|
|
|
eframe::set_value(storage, eframe::APP_KEY, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called each time the UI needs repainting, which may be many times per second.
|
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
|
|
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
|
|
|
egui::menu::bar(ui, |ui| {
|
|
|
|
if ui.button("Quit").clicked() {
|
|
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
|
|
|
}
|
2023-12-31 16:46:21 -05:00
|
|
|
|
2023-12-30 02:04:39 -05:00
|
|
|
ui.add_space(16.0);
|
|
|
|
|
|
|
|
egui::widgets::global_dark_light_mode_buttons(ui);
|
2023-12-30 12:02:58 -05:00
|
|
|
|
|
|
|
ui.with_layout(Layout::right_to_left(Align::Max), |ui| {
|
|
|
|
egui::warn_if_debug_build(ui);
|
|
|
|
ui.hyperlink_to("source code", "https://git.jeffa.io/jeff/dust");
|
|
|
|
});
|
2023-12-30 02:04:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2023-12-30 12:02:58 -05:00
|
|
|
ui.with_layout(Layout::left_to_right(Align::Min), |ui| {
|
|
|
|
ui.with_layout(Layout::top_down(Align::Min).with_main_justify(true), |ui| {
|
|
|
|
ui.with_layout(Layout::left_to_right(Align::Min), |ui| {
|
|
|
|
ui.text_edit_singleline(&mut self.path);
|
2023-12-30 02:04:39 -05:00
|
|
|
|
2023-12-30 12:02:58 -05:00
|
|
|
if ui.button("read").clicked() {
|
|
|
|
self.source = read_to_string(&self.path).unwrap();
|
|
|
|
}
|
2023-12-30 02:04:39 -05:00
|
|
|
|
2023-12-30 12:02:58 -05:00
|
|
|
if ui.button("run").clicked() {
|
|
|
|
self.output = self.interpreter.run(&self.source);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ui.code_editor(&mut self.source);
|
|
|
|
});
|
2023-12-30 02:04:39 -05:00
|
|
|
|
2023-12-31 16:46:21 -05:00
|
|
|
match &self.output {
|
|
|
|
Ok(value) => {
|
|
|
|
display_value(value, ui);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
ui.label(error.to_string());
|
|
|
|
}
|
|
|
|
}
|
2023-12-30 02:04:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-12-31 16:46:21 -05:00
|
|
|
|
|
|
|
fn display_value(value: &Value, ui: &mut egui::Ui) {
|
|
|
|
match value {
|
|
|
|
Value::List(list) => {
|
|
|
|
ui.collapsing("list", |ui| {
|
|
|
|
for value in list.items().iter() {
|
|
|
|
display_value(value, ui);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Value::Map(_) => todo!(),
|
|
|
|
Value::Function(function) => {
|
|
|
|
ui.label(function.to_string());
|
|
|
|
}
|
|
|
|
Value::String(string) => {
|
|
|
|
ui.label(RichText::new(string).color(Color32::GREEN));
|
|
|
|
}
|
|
|
|
Value::Float(float) => {
|
|
|
|
ui.label(float.to_string());
|
|
|
|
}
|
|
|
|
Value::Integer(integer) => {
|
|
|
|
ui.label(RichText::new(integer.to_string()).color(Color32::BLUE));
|
|
|
|
}
|
|
|
|
Value::Boolean(boolean) => {
|
|
|
|
ui.label(RichText::new(boolean.to_string()).color(Color32::RED));
|
|
|
|
}
|
|
|
|
Value::Option(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|