1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use eframe::{
    egui::{
        plot::{Bar, BarChart, Line, Plot as EguiPlot, PlotPoints},
        CentralPanel, Context, Direction, Layout, RichText, ScrollArea, Ui,
    },
    emath::Align,
    epaint::{Color32, Stroke},
    run_native, NativeOptions,
};
use egui_extras::{Column, StripBuilder, TableBuilder};

use crate::{
    eval_with_context, Error, Result, Table, Tool, ToolInfo, Value, ValueType, VariableMap,
};

pub struct BarGraph;

impl Tool for BarGraph {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "bar_graph",
            description: "Render a list of values as a bar graph.",
            group: "gui",
            inputs: vec![ValueType::ListOf(Box::new(ValueType::List))],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = argument.as_list()?;
        let mut bars = Vec::new();

        for (index, value) in argument.iter().enumerate() {
            let list = value.as_list()?;
            let mut name = None;
            let mut height = None;

            for value in list {
                match value {
                    Value::Float(float) => {
                        if height.is_none() {
                            height = Some(float);
                        }
                    }
                    Value::Integer(_integer) => {}
                    Value::String(string) => name = Some(string),
                    Value::Boolean(_)
                    | Value::List(_)
                    | Value::Map(_)
                    | Value::Table(_)
                    | Value::Time(_)
                    | Value::Function(_)
                    | Value::Empty => continue,
                }
            }

            let height =
                match height {
                    Some(height) => *height,
                    None => return Err(Error::CustomMessage(
                        "Could not create bar graph. No float value was found to use as a height."
                            .to_string(),
                    )),
                };

            let bar = Bar::new(index as f64, height).name(name.unwrap_or(&"".to_string()));

            bars.push(bar);
        }

        run_native(
            "bar_graph",
            NativeOptions::default(),
            Box::new(|_cc| Box::new(BarGraphGui::new(bars))),
        )
        .unwrap();

        Ok(Value::Empty)
    }
}

struct BarGraphGui {
    bars: Vec<Bar>,
}

impl BarGraphGui {
    fn new(data: Vec<Bar>) -> Self {
        Self { bars: data }
    }
}

impl eframe::App for BarGraphGui {
    fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
        CentralPanel::default().show(ctx, |ui| {
            EguiPlot::new("bar_graph").show(ui, |plot_ui| {
                plot_ui.bar_chart(BarChart::new(self.bars.clone()).color(Color32::RED));
            });
        });
    }
}

pub struct Plot;

impl Tool for Plot {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "plot",
            description: "Render a list of numbers as a scatter plot graph.",
            group: "gui",
            inputs: vec![
                ValueType::ListOf(Box::new(ValueType::Float)),
                ValueType::ListOf(Box::new(ValueType::Integer)),
            ],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = argument.as_list()?;
        let mut floats = Vec::new();

        for value in argument {
            if let Ok(float) = value.as_float() {
                floats.push(float);
            } else if let Ok(integer) = value.as_int() {
                floats.push(integer as f64);
            } else {
                return Err(Error::expected_number(value.clone()));
            }
        }

        run_native(
            "plot",
            NativeOptions {
                resizable: true,
                centered: true,
                ..Default::default()
            },
            Box::new(|_cc| Box::new(PlotGui::new(floats))),
        )
        .unwrap();

        Ok(Value::Empty)
    }
}

struct PlotGui {
    data: Vec<f64>,
}

impl PlotGui {
    fn new(data: Vec<f64>) -> Self {
        Self { data }
    }
}

impl eframe::App for PlotGui {
    fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
        CentralPanel::default().show(ctx, |ui| {
            EguiPlot::new("plot").show(ui, |plot_ui| {
                let points = self
                    .data
                    .iter()
                    .enumerate()
                    .map(|(index, value)| [index as f64, *value])
                    .collect::<PlotPoints>();
                let line = Line::new(points);
                plot_ui.line(line);
            })
        });
    }
}

pub struct GuiApp {
    text_edit_buffer: String,
    whale_context: VariableMap,
    eval_result: Result<Value>,
}

impl GuiApp {
    pub fn new(result: Result<Value>) -> Self {
        GuiApp {
            text_edit_buffer: String::new(),
            whale_context: VariableMap::new(),
            eval_result: result,
        }
    }

    fn _table_ui(&mut self, table: &Table, ui: &mut Ui) {
        TableBuilder::new(ui)
            .resizable(true)
            .striped(true)
            .columns(Column::remainder(), table.column_names().len())
            .header(30.0, |mut row| {
                for name in table.column_names() {
                    row.col(|ui| {
                        ui.label(name);
                    });
                }
            })
            .body(|body| {
                body.rows(20.0, table.rows().len(), |index, mut row| {
                    let row_data = table.rows().get(index).unwrap();

                    for cell_data in row_data {
                        row.col(|ui| {
                            ui.label(cell_data.to_string());
                        });
                    }
                });
            });
    }
}

impl eframe::App for GuiApp {
    fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
        CentralPanel::default().show(ctx, |ui| {
            ui.with_layout(
                Layout {
                    main_dir: Direction::TopDown,
                    main_wrap: false,
                    main_align: Align::Center,
                    main_justify: false,
                    cross_align: Align::Center,
                    cross_justify: true,
                },
                |ui| {
                    ui.text_edit_multiline(&mut self.text_edit_buffer);
                    ui.horizontal(|ui| {
                        let clear = ui.button("clear");
                        let submit = ui.button("submit");

                        if clear.clicked() {
                            self.text_edit_buffer.clear();
                        }

                        if submit.clicked() {
                            self.eval_result =
                                eval_with_context(&self.text_edit_buffer, &mut self.whale_context);
                        }
                    });
                },
            );
            ui.separator();

            StripBuilder::new(ui)
                .sizes(egui_extras::Size::remainder(), 1)
                .vertical(|mut strip| {
                    strip.cell(|ui| {
                        let rectangle = ui.available_rect_before_wrap();
                        let corner = 5.0;
                        let border = Stroke::new(1.0, Color32::DARK_GREEN);
                        let item_size = 20.0;

                        ui.painter().rect_stroke(rectangle, corner, border);

                        match &self.eval_result {
                            Ok(value) => match value {
                                Value::String(string) => {
                                    ui.label(RichText::new(string).size(item_size));
                                }
                                Value::Float(float) => {
                                    ui.label(RichText::new(float.to_string()).size(item_size));
                                }
                                Value::Integer(integer) => {
                                    ui.label(RichText::new(integer.to_string()).size(item_size));
                                }
                                Value::Boolean(boolean) => {
                                    ui.label(RichText::new(boolean.to_string()).size(item_size));
                                }
                                Value::List(list) => {
                                    for value in list {
                                        ui.label(RichText::new(value.to_string()).size(item_size));
                                    }
                                }
                                Value::Map(_) => todo!(),
                                Value::Table(table) => {
                                    ScrollArea::both().show(ui, |ui| {
                                        TableBuilder::new(ui)
                                            .resizable(true)
                                            .striped(true)
                                            .vscroll(true)
                                            .columns(
                                                Column::remainder(),
                                                table.column_names().len(),
                                            )
                                            .header(20.0, |mut row| {
                                                for name in table.column_names() {
                                                    row.col(|ui| {
                                                        ui.label(name);
                                                    });
                                                }
                                            })
                                            .body(|body| {
                                                body.rows(
                                                    20.0,
                                                    table.rows().len(),
                                                    |index, mut row| {
                                                        let row_data =
                                                            table.rows().get(index).unwrap();

                                                        for value in row_data {
                                                            row.col(|ui| {
                                                                ui.label(value.to_string());
                                                            });
                                                        }
                                                    },
                                                );
                                            });
                                    });
                                }
                                Value::Function(_) => todo!(),
                                Value::Empty => {}
                                Value::Time(_) => todo!(),
                            },
                            Err(error) => {
                                let rectangle = ui.available_rect_before_wrap();
                                let corner = 5.0;
                                let border = Stroke::new(1.0, Color32::DARK_RED);

                                ui.painter().rect_stroke(rectangle, corner, border);
                                ui.label(error.to_string());
                            }
                        }
                    });
                });
        });
    }
}