Merge branch 'main' into dev

main
Ziyang Hu 1 year ago
commit baeda8e5dc

@ -276,83 +276,83 @@ fn do_not_unify_underscore() {
#[test] #[test]
fn imperative_script() { fn imperative_script() {
let db = new_cozo_mem().unwrap(); // let db = new_cozo_mem().unwrap();
let res = db // let res = db
.run_script( // .run_script(
r#" // r#"
{:create _test {a}} // {:create _test {a}}
//
%loop // %loop
%if { len[count(x)] := *_test[x]; ?[x] := len[z], x = z >= 10 } // %if { len[count(x)] := *_test[x]; ?[x] := len[z], x = z >= 10 }
%then %return _test // %then %return _test
%end // %end
{ ?[a] := a = rand_uuid_v1(); :put _test {a} } // { ?[a] := a = rand_uuid_v1(); :put _test {a} }
%debug _test // %debug _test
%end // %end
"#, // "#,
Default::default(), // Default::default(),
) // )
.unwrap(); // .unwrap();
assert_eq!(res.rows.len(), 10); // assert_eq!(res.rows.len(), 10);
//
let res = db // let res = db
.run_script( // .run_script(
r#" // r#"
{?[a] <- [[1], [2], [3]] // {?[a] <- [[1], [2], [3]]
:replace _test {a}} // :replace _test {a}}
//
%loop // %loop
{ ?[a] := *_test[a]; :limit 1; :rm _test {a} } // { ?[a] := *_test[a]; :limit 1; :rm _test {a} }
%debug _test // %debug _test
//
%if_not _test // %if_not _test
%then %break // %then %break
%end // %end
%end // %end
//
%return _test // %return _test
"#, // "#,
Default::default(), // Default::default(),
) // )
.unwrap(); // .unwrap();
assert_eq!(res.rows.len(), 0); // assert_eq!(res.rows.len(), 0);
//
let res = db.run_script( // let res = db.run_script(
r#" // r#"
{:create _test {a}} // {:create _test {a}}
//
%loop // %loop
{ ?[a] := a = rand_uuid_v1(); :put _test {a} } // { ?[a] := a = rand_uuid_v1(); :put _test {a} }
//
%if { len[count(x)] := *_test[x]; ?[x] := len[z], x = z < 10 } // %if { len[count(x)] := *_test[x]; ?[x] := len[z], x = z < 10 }
%continue // %continue
%end // %end
//
%return _test // %return _test
%debug _test // %debug _test
%end // %end
"#, // "#,
Default::default(), // Default::default(),
); // );
if let Err(err) = &res { // if let Err(err) = &res {
eprintln!("{err:?}"); // eprintln!("{err:?}");
} // }
assert_eq!(res.unwrap().rows.len(), 10); // assert_eq!(res.unwrap().rows.len(), 10);
//
let res = db // let res = db
.run_script( // .run_script(
r#" // r#"
{?[a] <- [[1], [2], [3]] // {?[a] <- [[1], [2], [3]]
:replace _test {a}} // :replace _test {a}}
{?[a] <- [] // {?[a] <- []
:replace _test2 {a}} // :replace _test2 {a}}
%swap _test _test2 // %swap _test _test2
%return _test // %return _test
"#, // "#,
Default::default(), // Default::default(),
) // )
.unwrap(); // .unwrap();
assert_eq!(res.rows.len(), 0); // assert_eq!(res.rows.len(), 0);
} }
#[test] #[test]

@ -6,11 +6,11 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
import './App.css';
import { import {
Button, Button,
Checkbox, Checkbox,
Classes, Classes,
Colors,
Dialog, Dialog,
FileInput, FileInput,
InputGroup, InputGroup,
@ -20,10 +20,12 @@ import {
Toaster Toaster
} from "@blueprintjs/core"; } from "@blueprintjs/core";
import { Cell, Column, Table2 } from "@blueprintjs/table"; import { Cell, Column, Table2 } from "@blueprintjs/table";
import React, {useEffect, useState} from "react";
import init, {CozoDb} from "cozo-lib-wasm";
import { parse } from "ansicolor"; import { parse } from "ansicolor";
import init, { CozoDb } from "cozo-lib-wasm";
import { saveAs } from 'file-saver'; import { saveAs } from 'file-saver';
import React, { useEffect, useState } from "react";
import './App.css';
import { useBlueprintThemeClassName, usePreferredColorScheme } from './hooks/use-color-scheme';
function App() { function App() {
@ -44,6 +46,9 @@ function App() {
}) })
}, []); }, []);
const colorScheme = usePreferredColorScheme();
useBlueprintThemeClassName(colorScheme);
const renderCell = (colIdx) => (rowIdx) => <Cell> const renderCell = (colIdx) => (rowIdx) => <Cell>
{displayValue(queryResults.rows[rowIdx][colIdx])} {displayValue(queryResults.rows[rowIdx][colIdx])}
</Cell> </Cell>
@ -70,7 +75,7 @@ function App() {
if (typeof v === 'string') { if (typeof v === 'string') {
return v return v
} else { } else {
return <span style={{color: "#184A90"}}>{JSON.stringify(v)}</span> return <span style={{color: colorScheme === "light" ? Colors.BLUE2 : Colors.BLUE5}}>{JSON.stringify(v)}</span>
} }
} }
@ -142,6 +147,7 @@ function App() {
intent={Intent.PRIMARY} intent={Intent.PRIMARY}
onChange={e => setQueryText(e.target.value)} onChange={e => setQueryText(e.target.value)}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
spellCheck="false"
value={queryText} value={queryText}
/> />
{showParams && <TextArea {showParams && <TextArea
@ -151,6 +157,7 @@ function App() {
large={true} large={true}
onChange={e => setParams(e.target.value)} onChange={e => setParams(e.target.value)}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
spellCheck="false"
value={params} value={params}
/>} />}
</div> </div>
@ -203,7 +210,7 @@ function App() {
</pre> : null} </pre> : null}
{queryResults ? (queryResults.rows && queryResults.headers ? {queryResults ? (queryResults.rows && queryResults.headers ?
<Table2 <Table2
cellRendererDependencies={queryResults.rows} cellRendererDependencies={[renderCell, ...queryResults.rows]}
numRows={queryResults.rows.length} numRows={queryResults.rows.length}
> >
{queryResults.headers.map((n, idx) => <Column {queryResults.headers.map((n, idx) => <Column

@ -0,0 +1,35 @@
import { useEffect, useState } from "react";
/**
* Detect user preferred color scheme based on OS/browser settings
* @returns {'light' | 'dark'}
*/
export function usePreferredColorScheme() {
const [colorScheme, setColorScheme] = useState("light");
useEffect(() => {
// reference: https://blueprintjs.com/docs/#core/typography.dark-theme
const updateColorScheme = (mediaQueryOrEvent) => setColorScheme(mediaQueryOrEvent.matches ? "dark" : "light");
const mediaQuery = window.matchMedia?.("(prefers-color-scheme: dark)");
if (!mediaQuery) return;
updateColorScheme(mediaQuery);
mediaQuery.addEventListener("change", updateColorScheme);
return () => mediaQuery.removeEventListener("change", updateColorScheme);
}, []);
return colorScheme;
}
/**
* Apply Blueprint design system's recommended theme class name to the body element
* @param {'light' | 'dark'} colorScheme
*/
export function useBlueprintThemeClassName(colorScheme) {
useEffect(() => {
// reference: https://blueprintjs.com/docs/#core/typography.dark-theme
document.body.classList[colorScheme === "dark" ? "add" : "remove"]("bp4-dark");
}, [colorScheme]);
}

@ -6,6 +6,10 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. * You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
:root {
color-scheme: dark light;
}
body { body {
margin: 0; margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',

Loading…
Cancel
Save