gerb 0.0.1-alpha+2023-04-27

Font editor for UFO 3 fonts.
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * gerb
 *
 * Copyright 2022 - Manos Pitsidianakis
 *
 * This file is part of gerb.
 *
 * gerb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * gerb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gerb. If not, see <http://www.gnu.org/licenses/>.
 */

//! # Python shell
//!
//! A shell window is created, and communication channels are set up in order to make data
//! transfer through threads safe.
//!
//! Python's stdout is replaced with a `StringIO` object so we can read from it and update the
//! gtk window.
//!
//! User input is sent to the python thread via a regular [`std::sync::mpsc::channel`] channel.
//!
//! The python thread sends API requests to a [`glib::MainContext`] channel. The [`Runtime`]
//! object handles it, and replies to the python thread through another
//! [`std::sync::mpsc::channel`].

use super::*;

pub const SYS_PS1: &str = ">>> ";
pub const SYS_PS2: &str = "... ";
pub const BANNER: &str = "Exported objects: 'gerb'. Use 'help(gerb)' for more information.";

// [ref:needs_user_doc]
// [ref:needs_dev_doc]

// [ref:FIXME]: Ctrl-C not working when issuing `help(gerb)`?
// [ref:TODO]: How do we export typing hints?

#[derive(Clone, Debug, Hash, PartialEq, Eq, Copy)]
#[repr(u8)]
pub enum LinePrefix {
    Output,
    Ps1,
    Ps2,
}

impl LinePrefix {
    #[inline]
    pub const fn as_str(self) -> &'static str {
        match self {
            LinePrefix::Output => "",
            LinePrefix::Ps1 => SYS_PS1,
            LinePrefix::Ps2 => SYS_PS2,
        }
    }
}

/// Python shell history
pub struct ShellHistory {
    cursor: Cell<usize>,
    history: Vec<(LinePrefix, String)>,
}

impl ShellHistory {
    pub fn history(&self) -> &[(LinePrefix, String)] {
        &self.history
    }

    pub fn prev(&self) -> Option<&str> {
        let cursor = self.cursor.get();
        if cursor == 0 {
            return None;
        }
        let (i, ret) = self
            .history
            .get(..cursor)?
            .iter()
            .enumerate()
            .rev()
            .find_map(|(i, (lp, l))| {
                if !matches!(lp, LinePrefix::Output) && !l.is_empty() {
                    Some((i, l.as_str()))
                } else {
                    None
                }
            })?;
        self.cursor.set(i);
        Some(ret)
    }

    pub fn next(&self) -> Option<&str> {
        let cursor = self.cursor.get();
        if cursor + 1 == self.history.len() {
            return None;
        }
        let (i, ret) = self
            .history
            .get(cursor + 1..)?
            .iter()
            .enumerate()
            .find_map(|(i, (lp, l))| {
                if !matches!(lp, LinePrefix::Output) && !l.is_empty() {
                    Some((i, l.as_str()))
                } else {
                    None
                }
            })?;
        self.cursor.set(cursor + 1 + i);
        Some(ret)
    }

    pub fn push(&mut self, (prefix, line): (LinePrefix, String)) {
        match self.history.last_mut() {
            Some((LinePrefix::Ps1 | LinePrefix::Ps2, llast))
                if matches!(prefix, LinePrefix::Ps2) =>
            {
                llast.push('\n');
                llast.push_str(&line);
                return;
            }
            _ => {}
        }
        self.cursor.set(self.history.len());
        self.history.push((prefix, line));
    }

    pub fn clear(&mut self) {
        self.history.clear();
        self.cursor.set(0);
    }
}

/// Setup python shell window
pub fn new_shell_window(app: Application) -> gtk::Window {
    let w = gtk::Window::builder()
        .deletable(true)
        .transient_for(&app.window)
        .attached_to(&app.window)
        .destroy_with_parent(true)
        .application(&app)
        .focus_on_map(true)
        .resizable(true)
        .title("python shell")
        .visible(true)
        .expand(true)
        .type_hint(gtk::gdk::WindowTypeHint::Utility)
        .window_position(gtk::WindowPosition::Center)
        .build();
    w.set_default_size(640, 480);
    w.set_size_request(640, 480);
    let scrolled_window = gtk::ScrolledWindow::builder()
        .expand(true)
        .visible(true)
        .can_focus(true)
        .halign(gtk::Align::Fill)
        .valign(gtk::Align::Fill)
        .build();
    let adj = scrolled_window.vadjustment();
    adj.set_value(adj.upper());
    let list = gtk::ListBox::builder()
        .halign(gtk::Align::Fill)
        .valign(gtk::Align::Fill)
        .visible(true)
        .build();
    {
        let label = gtk::Label::new(Some(BANNER));
        label.set_wrap(true);
        label.set_selectable(true);
        label.set_visible(true);
        label.set_halign(gtk::Align::Start);
        label.set_valign(gtk::Align::End);
        list.add(&label);
        list.queue_draw();
    }
    list.style_context().add_class("monospace");
    {
        let container = gtk::Box::builder()
            .orientation(gtk::Orientation::Vertical)
            .margin(0)
            .margin_bottom(0)
            .visible(true)
            .halign(gtk::Align::Fill)
            .valign(gtk::Align::Fill)
            .expand(true)
            .build();
        container.style_context().add_class("terminal-box");
        container.pack_end(&list, false, false, 0);
        scrolled_window.set_child(Some(&container));
    }
    let b = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .margin(5)
        .spacing(6)
        .margin_bottom(10)
        .visible(true)
        .build();
    let entry = gtk::Entry::new();
    entry.style_context().add_class("terminal-entry");
    entry.style_context().add_class("monospace");
    entry.set_visible(true);
    let shell = ShellInstance::new(
        app.runtime.clone(),
        // [ref:python_api_main_loop_channel]
        clone!(@weak app => @default-return Continue(false), move |tx: &mpsc::Sender<String>, msg: String| {
            let response = process_api_request(&app.runtime, msg);
            if let Err(ref err) = response {
                let dialog = crate::utils::widgets::new_simple_error_dialog(
                    None,
                    &err.to_string(),
                    None,
                    app.window.upcast_ref(),
                );
                dialog.run();
                dialog.emit_close();
            }
            let (Err(json) | Ok(json)) = response;
            // [ref:python_api_response_channel]
            tx.send(json.to_string()).unwrap();
            Continue(true)
        }),
        clone!(@weak list => @default-return Continue(false), move |hist, (prefix, mut msg)| {
            if !msg.is_empty() {
                while msg.ends_with('\n') {
                    msg.pop();
                }
                let label = gtk::Label::new(Some(&format!("{}{msg}", match prefix {
                    LinePrefix::Output => "",
                    LinePrefix::Ps1 => SYS_PS1,
                    LinePrefix::Ps2 => SYS_PS2,
                })));
                hist.borrow_mut().push((prefix, msg));
                label.set_wrap(true);
                label.set_selectable(true);
                label.set_visible(true);
                label.set_halign(gtk::Align::Start);
                label.set_valign(gtk::Align::End);
                list.add(&label);
                list.queue_draw();
            }
            Continue(true)
        }),
    );
    let hist = shell.hist.clone();

    list.connect_size_allocate(clone!(@weak adj => move |_, _| {
        adj.set_value(adj.upper());
    }));

    entry.connect_activate(clone!(@weak adj => move |entry| {
        let buffer = entry.buffer();
        let text = buffer.text();
        buffer.set_text("");
        if let Err(err) = shell.shell_stdin.send(if text.is_empty() { "\n".to_string() } else { text }) {
            eprintln!("Internal error: {err}");
        }
    }));
    entry.set_events(gdk::EventMask::KEY_PRESS_MASK);
    entry.connect_key_press_event(
        clone!(@weak hist => @default-return Inhibit(false), move |entry, event| {
            if event.keyval() == gdk::keys::constants::Up {
                if let Some(prev) = hist.borrow().prev() {
                    entry.buffer().set_text(prev);
                    entry.set_position(-1);
                }
                Inhibit(true)
            } else if event.keyval() == gdk::keys::constants::Down {
                if let Some(next) = hist.borrow().next() {
                    entry.buffer().set_text(next);
                } else {
                    entry.buffer().set_text("");
                }
                entry.set_position(-1);
                Inhibit(true)
            } else {
                Inhibit(false)
            }
        }),
    );

    {
        let clear_btn = gtk::Button::builder()
            .label("Clear")
            .relief(gtk::ReliefStyle::Normal)
            .visible(true)
            .halign(gtk::Align::End)
            .valign(gtk::Align::Center)
            .build();
        clear_btn.connect_clicked(clone!(@weak list, @strong hist => move |_| {
            for c in list.children() {
                list.remove(&c);
            }
            hist.borrow_mut().clear();
        }));
        let save_history_btn = gtk::Button::builder()
            .label("Save history")
            .relief(gtk::ReliefStyle::Normal)
            .visible(true)
            .sensitive(false)
            .halign(gtk::Align::End)
            .valign(gtk::Align::Center)
            .build();
        save_history_btn.connect_clicked(clone!(@weak list => move |_| {
        }));
        let copy_history_btn = gtk::Button::builder()
            .label("Copy history")
            .relief(gtk::ReliefStyle::Normal)
            .visible(true)
            .halign(gtk::Align::End)
            .valign(gtk::Align::Center)
            .build();
        copy_history_btn.connect_clicked(clone!(@weak list, @strong hist => move |copy_history_btn| {
            if let Some(clip) = gtk::Clipboard::default(&copy_history_btn.display()) {
                let output = hist.borrow().history.iter().fold(String::new(), |mut acc, (p, l)| {
                    match p {
                        LinePrefix::Output =>{},
                        LinePrefix::Ps1 => acc.push_str(SYS_PS1),
                        LinePrefix::Ps2 => acc.push_str(SYS_PS2),
                    }
                    acc.push_str(l);
                    acc.push('\n');
                    acc
                });
                clip.set_text(&output);
            }
        }));
        let btn_container = gtk::Box::builder()
            .orientation(gtk::Orientation::Horizontal)
            .margin(0)
            .spacing(5)
            .visible(true)
            .halign(gtk::Align::Fill)
            .valign(gtk::Align::Start)
            .hexpand(true)
            .vexpand(false)
            .build();
        btn_container.pack_end(&save_history_btn, false, false, 0);
        btn_container.pack_end(&copy_history_btn, false, false, 0);
        btn_container.pack_end(&clear_btn, false, false, 0);
        let close_btn = gtk::Button::builder()
            .label("Close")
            .relief(gtk::ReliefStyle::Normal)
            .visible(true)
            .halign(gtk::Align::Start)
            .valign(gtk::Align::Center)
            .build();
        close_btn.connect_clicked(clone!(@weak w => move |_| {
            w.close();
        }));
        btn_container.pack_start(&close_btn, false, false, 0);
        b.pack_start(&btn_container, false, true, 0);
    }
    b.pack_start(&scrolled_window, true, true, 0);
    {
        let entry_container = gtk::Box::builder()
            .orientation(gtk::Orientation::Horizontal)
            .margin(0)
            .spacing(4)
            .visible(true)
            .halign(gtk::Align::Fill)
            .valign(gtk::Align::Fill)
            .build();
        let enter_btn = gtk::Button::builder()
            .label("enter")
            .relief(gtk::ReliefStyle::Normal)
            .visible(true)
            .halign(gtk::Align::Fill)
            .valign(gtk::Align::Fill)
            .build();
        enter_btn.connect_clicked(clone!(@weak entry => move |_| {
            entry.activate();
            entry.set_has_focus(true);
        }));
        entry_container.pack_start(&entry, true, true, 0);
        entry_container.pack_end(&enter_btn, false, false, 0);
        b.pack_start(&entry_container, false, true, 0);
    }
    w.set_child(Some(&b));
    entry.set_has_focus(true);
    w.show_all();
    w
}

/// Handle input inside of shell instance
fn handle_input(
    text: String,
    needed_more: bool,
    py: Python<'_>,
    locals: &PyDict,
    globals: &PyDict,
    tx: &glib::Sender<(LinePrefix, String)>,
) -> Result<bool, Box<dyn std::error::Error>> {
    locals.set_item("gerb", globals.get_item("gerb").unwrap())?;
    let shell = globals.get_item("gerb").unwrap().getattr("__shell")?;
    Ok(
        match shell
            .getattr("push")?
            .call1((if text.is_empty() { "\n" } else { &text },))
        {
            Ok(result) => {
                let needs_more_input: bool = result.extract()?;
                if !needs_more_input {
                    let r = py
                        .eval("gerb.__stdout.getvalue()", Some(globals), None)?
                        .str()?
                        .to_string_lossy();
                    py.run(
                        "gerb.__stdout.seek(0); gerb.__stdout.truncate(0)",
                        Some(globals),
                        None,
                    )?;
                    if !r.is_empty() {
                        if needed_more {
                            tx.send((LinePrefix::Ps2, format!("{}\n", &text,)))?;
                        } else {
                            tx.send((LinePrefix::Ps1, format!("{}\n", &text,)))?;
                        }
                        tx.send((LinePrefix::Output, r.to_string()))?;
                    } else if needed_more {
                        tx.send((LinePrefix::Ps2, text))?;
                    } else {
                        tx.send((LinePrefix::Ps1, text))?;
                    }
                } else if needed_more {
                    tx.send((
                        LinePrefix::Ps2,
                        if text.is_empty() {
                            "\n".to_string()
                        } else {
                            text
                        },
                    ))?;
                } else {
                    tx.send((
                        LinePrefix::Ps1,
                        if text.is_empty() {
                            "\n".to_string()
                        } else {
                            text
                        },
                    ))?;
                }
                needs_more_input
            }
            Err(err) => {
                tx.send((LinePrefix::Output, err.to_string()))?;
                false
            }
        },
    )
}

/// Helper function to setup python globals.
fn setup_globals<'py>(
    py: Python<'py>,
    __id: Uuid,
    locals_dict: &Py<PyDict>,
) -> Result<Py<PyDict>, Box<dyn std::error::Error + 'py>> {
    let globals = PyDict::new(py);
    // Import and get sys.modules
    let sys = PyModule::import(py, "sys")?;
    let py_modules: &PyDict = sys.getattr("modules")?.downcast()?;

    let io = PyModule::import(py, "io")?;
    let code = PyModule::import(py, "code")?;
    py_modules.set_item("sys", sys)?;
    py.import("io")?;
    py.import("sys")?;
    let gerb = Gerb {
        __id,
        __stdout: io.getattr("StringIO")?.call0()?.into(),
        __shell: code
            .getattr("InteractiveConsole")?
            .call1((locals_dict.as_ref(py),))?
            .into(),
        // [ref:python_api_main_loop_channel]
        __send: Py::new(py, Sender(None))?,
        // [ref:python_api_response_channel]
        __rcv: Py::new(py, Receiver(None))?,
        __types_dict: Gerb::types(py),
    };
    let gerb = PyCell::new(py, gerb)?;
    globals.set_item("gerb", gerb)?;
    globals.set_item("sys", sys)?;
    py.run(
        r#"""
sys.stdout = gerb.__stdout
sys.stderr = gerb.__stdout
"""#,
        Some(globals),
        None,
    )?;
    Ok(globals.into())
}

fn shell_thread(
    tx: glib::Sender<(LinePrefix, String)>,
    globals_dict: Py<PyDict>,
    locals_dict: Py<PyDict>,
    tx_py: glib::Sender<String>,
    rx_py2: mpsc::Receiver<String>,
    rx_shell: mpsc::Receiver<String>,
) -> Result<(), Box<dyn std::error::Error>> {
    let needs_more_input = Cell::new(false);
    let globals = &globals_dict;
    let locals = &locals_dict;
    let res: Result<(), Box<dyn std::error::Error>> = Python::with_gil(|py| {
        {
            // [ref:python_api_main_loop_channel]
            let c: &PyCell<Sender> = globals
                .as_ref(py)
                .get_item("gerb")
                .unwrap()
                .getattr("__send")?
                .extract()?;
            let mut guard: PyRefMut<'_, Sender> = c.borrow_mut();
            let n_mutable: &mut Sender = &mut guard;
            n_mutable.0 = Some(tx_py);
        }
        {
            // [ref:python_api_response_channel]
            let c: &PyCell<Receiver> = globals
                .as_ref(py)
                .get_item("gerb")
                .unwrap()
                .getattr("__rcv")?
                .extract()?;
            let mut guard: PyRefMut<'_, Receiver> = c.borrow_mut();
            let n_mutable: &mut Receiver = &mut guard;
            n_mutable.0 = Some(rx_py2);
        }
        locals
            .as_ref(py)
            .set_item("gerb", globals.as_ref(py).get_item("gerb").unwrap())?;
        Ok(())
    });
    res?;

    while let Ok(text) = rx_shell.recv() {
        if text.is_empty() && !needs_more_input.get() {
            let res: Result<(), Box<dyn std::error::Error>> = Python::with_gil(|py| {
                // [ref:python_api_main_loop_channel]
                let c: &PyCell<Sender> = globals
                    .as_ref(py)
                    .get_item("gerb")
                    .unwrap()
                    .getattr("__send")?
                    .extract()?;
                let mut guard: PyRefMut<'_, Sender> = c.borrow_mut();
                let n_mutable: &mut Sender = &mut guard;
                n_mutable.0.as_ref().unwrap().send(String::new())?;
                Ok(())
            });
            res?;
            continue;
        }
        needs_more_input.set(Python::with_gil(|py| {
            handle_input(
                text,
                needs_more_input.get(),
                py,
                locals.as_ref(py),
                globals.as_ref(py),
                &tx,
            )
            .unwrap()
        }));
    }
    Ok(())
}

pub struct ShellInstance {
    pub hist: Rc<RefCell<ShellHistory>>,
    /// shell stdin channel
    pub shell_stdin: mpsc::Sender<String>,
}

impl ShellInstance {
    pub fn new(
        runtime: Runtime,
        mut main_loop_rx: impl FnMut(&mpsc::Sender<String>, String) -> Continue + 'static,
        mut stdout_rx: impl FnMut(&Rc<RefCell<ShellHistory>>, (LinePrefix, String)) -> Continue
            + 'static,
    ) -> Self {
        let locals_dict: Py<PyDict> = Python::with_gil(|py| {
            let dict: Py<PyDict> = PyDict::new(py).into();
            dict
        });
        let runtime_id = runtime.register_obj(runtime.upcast_ref());
        let globals_dict: Py<PyDict> =
            Python::with_gil(|py| setup_globals(py, runtime_id, &locals_dict).unwrap());

        let hist = Rc::new(RefCell::new(ShellHistory {
            cursor: Cell::new(0),
            history: vec![],
        }));

        // shell stdout channel
        let (tx, rx) = MainContext::channel::<(LinePrefix, String)>(PRIORITY_DEFAULT);
        // shell stdin channel
        let (tx_shell, rx_shell) = std::sync::mpsc::channel::<String>();
        // shell -> runtime channel
        // [ref:python_api_main_loop_channel]
        let (tx_py, rx_py) = MainContext::channel(PRIORITY_DEFAULT);
        // runtime -> shell channel
        // [ref:python_api_response_channel]
        let (tx_py2, rx_py2) = std::sync::mpsc::channel::<String>();

        rx_py.attach(
            None,
            // [ref:python_api_main_loop_channel]
            move |msg| main_loop_rx(&tx_py2, msg),
        );
        let hist_ref = hist.clone();
        rx.attach(None, move |line| stdout_rx(&hist_ref, line));

        std::thread::spawn(move || {
            shell_thread(tx, globals_dict, locals_dict, tx_py, rx_py2, rx_shell).unwrap()
        });

        Self {
            hist,
            shell_stdin: tx_shell,
        }
    }
}