Skip to content

Commit f20c92e

Browse files
committed
Add proper testing and syntax highlighting, interactivity
1 parent fe01a44 commit f20c92e

File tree

31 files changed

+1082
-343
lines changed

31 files changed

+1082
-343
lines changed

Cargo.lock

Lines changed: 92 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "divvun-runtime"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
edition = "2024"
55
repository = "https://github.com/divvun/divvun-runtime"
66
license = "MIT OR Apache-2.0"
@@ -132,7 +132,7 @@ mod-jq = ["jaq-core", "jaq-std", "jaq-json"]
132132
ffi = []
133133

134134
[workspace]
135-
members = [".", "cli", "xml-conv", "macros"]
135+
members = [".", "cli", "xml-conv", "macros", "crates/syntax-highlight"]
136136

137137
[package.metadata.binstall]
138138
pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }{ archive-suffix }"

bindings/deno/deno.jsonc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@divvun/runtime",
3+
"version": "0.0.1",
4+
"description": "Divvun Runtime bindings for Deno",
5+
"type": "module",
6+
"license": "MIT OR Apache-2.0",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/divvun/divvun-runtime"
10+
},
11+
"exports": {
12+
".": "./mod.ts",
13+
"./test": "./test/mod.ts"
14+
}
15+
}

bindings/deno/test/mod.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { Bundle } from "../mod.ts";
2-
export * from "jsr:@std/assert";
2+
export * from "jsr:@std/assert@1";
33

44
let bundle: Bundle;
55

6-
const sleep = (ms: number) => new Promise((resolve) => setTimeout(() => resolve(null), ms))
7-
8-
export function loadBundle(bundlePath: string) {
6+
export function loadBundle(bundlePath: string): () => Bundle {
97
Deno.test.beforeAll(async () => {
108
bundle = await Bundle.fromBundle(bundlePath);
119
});
1210

1311
return () => bundle;
1412
}
1513

16-
export function loadPath(devPath: string) {
14+
export function loadPath(devPath: string): () => Bundle {
1715
Deno.test.beforeAll(async () => {
1816
bundle = await Bundle.fromPath(devPath);
1917
});
@@ -24,31 +22,3 @@ export function loadPath(devPath: string) {
2422
export function load(): () => Bundle {
2523
return loadPath(Deno.cwd());
2624
}
27-
28-
export async function runGrammar(text: string): Promise<GrammarResponse[]> {
29-
if (bundle == null) {
30-
bundle = await Bundle.fromPath(Deno.cwd());
31-
}
32-
33-
const pipe = await bundle.create();
34-
const res = await pipe.forward(text);
35-
return (await res.json()) as unknown as GrammarResponse[];
36-
}
37-
38-
export type GrammarResponse = {
39-
form: string;
40-
beg: number;
41-
end: number;
42-
err: string;
43-
msg: [string, string];
44-
rep: string[];
45-
};
46-
47-
type DivvunRuntimeModule = typeof import("../mod.ts");
48-
49-
declare global {
50-
interface Window {
51-
_DRT?: DivvunRuntimeModule;
52-
}
53-
var _DRT: DivvunRuntimeModule | undefined;
54-
}

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ffi = ["divvun-runtime/ffi"]
1414

1515
[dependencies]
1616
divvun-runtime = { default-features = false, path = ".." }
17+
syntax-highlight = { path = "../crates/syntax-highlight", features = ["terminal"] }
1718
clap = { version = "4.5.47", features = ["env", "derive"] }
1819
fwdansi = "1.1.0"
1920
termcolor = "1.4.1"

cli/src/command/run.rs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
io::{IsTerminal, Read},
2+
io::{self, IsTerminal, Read},
33
sync::{
44
atomic::{AtomicBool, Ordering},
55
Arc, Mutex,
@@ -25,9 +25,45 @@ use crate::{
2525

2626
use super::utils;
2727

28-
pub fn dump_ast(_shell: &mut Shell, args: DebugDumpAstArgs) -> anyhow::Result<()> {
28+
fn format_input_highlighted(input: &Input, command: Option<&Command>) -> String {
29+
if !syntax_highlight::supports_color() {
30+
return format!("{:#}", input);
31+
}
32+
33+
match input {
34+
Input::Json(j) => {
35+
let json = serde_json::to_string_pretty(j).unwrap();
36+
syntax_highlight::highlight_to_terminal(&json, "json")
37+
}
38+
Input::String(s) => {
39+
let syntax = command
40+
.and_then(|cmd| cmd.kind.as_deref())
41+
.filter(|k| *k == "cg3");
42+
43+
if let Some("cg3") = syntax {
44+
syntax_highlight::highlight_to_terminal(s, "cg3")
45+
} else {
46+
s.clone()
47+
}
48+
}
49+
_ => format!("{:#}", input),
50+
}
51+
}
52+
53+
fn print_input_highlighted(
54+
shell: &mut Shell,
55+
input: &Input,
56+
command: Option<&Command>,
57+
) -> anyhow::Result<()> {
58+
let formatted = format_input_highlighted(input, command);
59+
io::Write::write_all(shell.out(), formatted.as_bytes())?;
60+
Ok(())
61+
}
62+
63+
pub fn dump_ast(shell: &mut Shell, args: DebugDumpAstArgs) -> anyhow::Result<()> {
2964
let value = crate::deno_rt::dump_ast(&std::fs::read_to_string(args.path)?)?;
30-
println!("{}", serde_json::to_string_pretty(&value).unwrap());
65+
let json = serde_json::to_string_pretty(&value).unwrap();
66+
shell.print_highlighted_stdout(&json, "json")?;
3167
Ok(())
3268
}
3369

@@ -117,7 +153,12 @@ async fn run_repl(
117153
let tap_stepping = tap_stepping.clone();
118154

119155
println!("\x1b[41;31m[{}]\x1b[0m {}", key, cmd);
120-
println!("\x1b[33m{:#}\x1b[0m", event);
156+
match event {
157+
InputEvent::Input(input) => {
158+
println!("{}", format_input_highlighted(input, Some(cmd)));
159+
}
160+
_ => println!("{:#}", event),
161+
}
121162

122163
// Store the event for the current run
123164
if let Ok(mut events) = current_events_clone.lock() {
@@ -211,10 +252,9 @@ async fn run_repl(
211252
println!();
212253
}
213254
":ast" => {
214-
println!(
215-
"{}\n",
216-
serde_json::to_string_pretty(&**bundle.definition()).unwrap()
217-
);
255+
let json = serde_json::to_string_pretty(&**bundle.definition()).unwrap();
256+
shell.print_highlighted_stdout(&json, "json")?;
257+
println!();
218258
}
219259
":step" => {
220260
let cur = is_stepping
@@ -228,7 +268,9 @@ async fn run_repl(
228268
}
229269
}
230270
":config" => {
231-
println!("{}\n", serde_json::to_string_pretty(&config).unwrap());
271+
let json = serde_json::to_string_pretty(&config).unwrap();
272+
shell.print_highlighted_stdout(&json, "json")?;
273+
println!();
232274
}
233275
":save" => {
234276
let filename = chunks.next().unwrap_or("pipeline_debug.md");
@@ -309,10 +351,13 @@ async fn run_repl(
309351
// };
310352
let mut stream = pipe.forward(Input::String(line.to_string())).await;
311353

354+
let output_cmd = bundle.definition().output.resolve(bundle.definition());
355+
312356
while let Some(input) = stream.next().await {
313357
match input {
314358
Ok(input) => {
315-
shell.print(&"<-", Some(&format!("{:#}", input)), Color::Green, false)?;
359+
shell.print(&"<-", None, Color::Green, false)?;
360+
print_input_highlighted(shell, &input, output_cmd)?;
316361

317362
if let Some(path) = args.output_path.as_deref() {
318363
match input {
@@ -519,8 +564,10 @@ pub async fn run(shell: &mut Shell, mut args: RunArgs) -> Result<(), Arc<anyhow:
519564
if let Some(input) = args.input {
520565
let mut stream = pipe.forward(Input::String(input)).await;
521566

567+
let output_cmd = bundle.definition().output.resolve(bundle.definition());
568+
522569
while let Some(Ok(input)) = stream.next().await {
523-
println!("{:#}", input);
570+
print_input_highlighted(shell, &input, output_cmd)?;
524571
}
525572

526573
// if let Some(path) = args.output_path.as_deref() {

cli/src/command/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub async fn test(_shell: &mut Shell, args: TestArgs) -> anyhow::Result<()> {
5757

5858
let mut cmd = Command::new("deno");
5959
cmd.arg("test")
60+
.arg("--hide-stacktraces")
61+
.arg("--parallel")
6062
.arg("--allow-ffi")
6163
.arg("--allow-env")
6264
.arg("--no-check")

0 commit comments

Comments
 (0)