char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport console extension to typescript1738b3c

main
1.9 KiB63 linesraw
1use std::convert::Infallible;
2use std::io::{self, IsTerminal};
3use std::rc::Rc;
4use std::time::Instant;
5
6use avril_core::{Extension, Op};
7
8use avril_ext_stdio::Stdio;
9
10/// Whether inspected values written to this stream should be colored.
11pub fn colors(stderr: bool) -> bool {
12    let terminal = if stderr {
13        io::stderr().is_terminal()
14    } else {
15        io::stdout().is_terminal()
16    };
17    terminal && std::env::var_os("NO_COLOR").is_none_or(|value| value.is_empty())
18}
19
20pub fn init(stdio: Rc<Stdio>) -> Extension {
21    let clear = stdio.clone();
22    let start = Instant::now();
23    Extension {
24        name: "ext:console",
25        dependencies: &[],
26        ops: vec![
27            Op::new("op_console_print", move |(stderr, text): (bool, String)| {
28                stdio.print(stderr, text)
29            }),
30            Op::new("op_console_clear", move |_: ()| {
31                if io::stdout().is_terminal() {
32                    clear.print(false, "\x1b[1;1H\x1b[0J".into())?;
33                }
34                Ok::<_, io::Error>(())
35            }),
36            Op::new("op_console_colors", |stderr: bool| {
37                Ok::<_, Infallible>(colors(stderr))
38            }),
39            Op::new("op_console_now", move |_: ()| {
40                Ok::<_, io::Error>(start.elapsed().as_secs_f64() * 1000.0)
41            }),
42        ],
43        modules: &[
44            (
45                "ext:console/main.js",
46                include_str!(concat!(env!("OUT_DIR"), "/main.js")),
47            ),
48            (
49                "ext:console/inspect.js",
50                include_str!(concat!(env!("OUT_DIR"), "/inspect.js")),
51            ),
52            (
53                "ext:console/format.js",
54                include_str!(concat!(env!("OUT_DIR"), "/format.js")),
55            ),
56            (
57                "ext:console/table.js",
58                include_str!(concat!(env!("OUT_DIR"), "/table.js")),
59            ),
60        ],
61        entry_point: Some("ext:console/main.js"),
62    }
63}