瀏覽代碼

Support for printing

David Peter 2 年之前
父節點
當前提交
187b05fa3c
共有 2 個文件被更改,包括 37 次插入9 次删除
  1. 35 7
      numbat-wasm/src/lib.rs
  2. 2 2
      numbat-wasm/www/main.css

+ 35 - 7
numbat-wasm/src/lib.rs

@@ -2,11 +2,14 @@ mod jquery_terminal_formatter;
 mod utils;
 mod wasm_importer;
 
+use std::sync::{Arc, Mutex};
+
 use jquery_terminal_formatter::{jt_format, JqueryTerminalFormatter};
+use numbat::markup as m;
 use numbat::markup::Formatter;
 use numbat::pretty_print::PrettyPrint;
 use numbat::resolver::CodeSource;
-use numbat::{Context, InterpreterResult};
+use numbat::{Context, InterpreterResult, InterpreterSettings};
 
 use wasm_bindgen::prelude::*;
 
@@ -37,14 +40,39 @@ impl Numbat {
     }
 
     pub fn interpret(&mut self, code: &str) -> String {
+        let mut output = String::new();
+
         let fmt = JqueryTerminalFormatter {};
 
-        match self.ctx.interpret(&code, CodeSource::Text) {
-            Ok((_, result)) => match result {
-                InterpreterResult::Value(q) => fmt.format(&q.pretty_print(), true),
-                InterpreterResult::Continue => "".into(),
-                InterpreterResult::Exit(_) => jt_format("error", "Error!".into()),
-            },
+        let to_be_printed: Arc<Mutex<Vec<m::Markup>>> = Arc::new(Mutex::new(vec![]));
+        let to_be_printed_c = to_be_printed.clone();
+        let mut settings = InterpreterSettings {
+            print_fn: Box::new(move |s: &m::Markup| {
+                to_be_printed_c.lock().unwrap().push(s.clone());
+            }),
+        };
+
+        match self
+            .ctx
+            .interpret_with_settings(&mut settings, &code, CodeSource::Text)
+        {
+            Ok((_, result)) => {
+                for content in to_be_printed.lock().unwrap().iter() {
+                    output.push_str(&fmt.format(content, true));
+                }
+
+                match result {
+                    InterpreterResult::Value(q) => {
+                        output.push_str(&fmt.format(&q.pretty_print(), true))
+                    }
+                    InterpreterResult::Continue => {}
+                    InterpreterResult::Exit(_) => {
+                        output.push_str(&jt_format("error", "Error!".into()))
+                    }
+                }
+
+                output
+            }
             Err(e) => format!("{:#}", e),
         }
     }

+ 2 - 2
numbat-wasm/www/main.css

@@ -90,7 +90,7 @@ p.links a {
 /* syntax highlighting */
 
 .hl-dimmed {
-    color: #3e3e3e !important;
+    color: #606060 !important;
 }
 
 .hl-string {
@@ -113,7 +113,7 @@ p.links a {
 .hl-identifier {}
 
 .hl-type-identifier {
-    color: #1d4fba !important;
+    color: #5179ce !important;
     font-style: italic !important;
 }