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
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{io, path::Path};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Key {
    Backspace,
    Left,
    Right,
    Up,
    Down,
    Home,
    End,
    PageUp,
    PageDown,
    BackTab,
    Delete,
    Insert,
    F(u8),
    Char(char),
    Alt(char),
    Ctrl(char),
    Null,
    Esc,
}

pub fn get_key() -> Key {
    deserialize_from_stdin().unwrap()
}

pub fn open_file(path: &Path) {
    println!("{}", path.to_string_lossy());
    unsafe { host_open_file() };
}

pub fn set_selectable(selectable: bool) {
    let selectable = if selectable { 1 } else { 0 };
    unsafe { host_set_selectable(selectable) };
}

pub fn get_help() -> Vec<String> {
    unsafe { host_get_help() };
    deserialize_from_stdin().unwrap_or_default()
}

fn deserialize_from_stdin<T: DeserializeOwned>() -> Option<T> {
    let mut json = String::new();
    io::stdin().read_line(&mut json).unwrap();
    serde_json::from_str(&json).ok()
}

#[link(wasm_import_module = "mosaic")]
extern "C" {
    fn host_open_file();
    fn host_set_selectable(selectable: i32);
    fn host_get_help();
}