Skip to content

Commit

Permalink
moving print command to this project
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed Dec 20, 2024
1 parent f15be27 commit 955ec66
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 13 deletions.
15 changes: 12 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"@tauri-apps/plugin-shell": "^2",
"marked": "^14.1.3",
"solid-js": "^1.9.2",
"split-grid": "^1.0.11"
"split-grid": "^1.0.11",
"tabulator-tables": "^6.3.0"
},
"devDependencies": {
"@tauri-apps/cli": "^2",
Expand Down
121 changes: 112 additions & 9 deletions src/components/Application.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,117 @@
import { createSignal, onMount } from "solid-js"
import { onMount } from "solid-js"
import { showEditor } from "@ligature/ligature-components/src/editor/editor"
import { readValue } from "@ligature/ligature"
import { runWander } from "../lib/ligature-client"
import { EditorView } from "codemirror";
import { showText } from "@ligature/ligature-components/src/text/text"
import { showGraph } from "@ligature/ligature-components/src/graph/graph"
import { run as runScript, Element, Literal, Variable, Quote, Triple, Result, Error, Network, NoResult } from "@ligature/ligature"
import { TabulatorFull as Tabulator } from 'tabulator-tables'
import "tabulator-tables/dist/css/tabulator.min.css"
import Split from 'split-grid'

function networkToTableData(network: Triple[]): any[] {
let roles = new Set<string>()
let results: any[] = []
network.forEach(([e,a,v]) => {
let res = results.find((i:any) => i.element == e.value)
if (res == undefined) {
const newEntry = {element: triple.first.symbol}
newEntry[triple.role.symbol] = [triple.second.symbol]
results.push(newEntry)
roles.add(triple.role.symbol)
} else {
if (res[triple.role.symbol] != undefined) {
res[triple.role.symbol].push(triple.second.symbol)
} else {
res[triple.role.symbol] = [triple.second.symbol]
}
}
})

function printNetwork(n: Array<Triple>): string {
let res = "{\n"
for (let [e,a,v] of n) {
res = res + " " + printValue(e) + " " + printValue(a) + " " + printValue(v) + ",\n"
}
return res + "}"
}

function printValue(value: Element | Literal | Variable | Quote | Network): string {
if (value.type == 'element') {
return value.value
} else if (value.type == 'literal') {
return value.value
} else if (value.type == 'network') {
return printNetwork(value.value)
} else if (value.type == 'quote') {
throw "TODO"
} else if (value.type == 'variable') {
return value.value
} else {
throw "should never reach"
}
}

function printArgs(args: Array<Element | Literal | Variable | Quote | Network>) {
let res = ""
for (let arg of args) {
if (arg.type == "element") {
res = res + arg.value + ", "
} else if (arg.type == "network") {
res = res + printNetwork(arg.value)
} else {
res = res + "???"
}
}
return res
}

function createUiCommands(el: HTMLElement) {
return [
{
name: "print",
doc: "Print a value",
action: (args) => {
let div = document.createElement("div")
let code = document.createElement("code")
let pre = document.createElement("pre")
code.textContent = printArgs(args)
pre.appendChild(code)
div.appendChild(pre)
el.appendChild(div)
}
},
{
name: "table",
doc: "Print a table",
action: (args) => {
if (args.length == 1) {
if (args[0].type == 'network') {
let network: Array<Triple> = args[0].value
let div = document.createElement("div")
div.classList.add("table")
let innerDiv = document.createElement("div")
div.appendChild(innerDiv)
let t = new Tabulator(innerDiv, {
data: networkToTableData(network),
autoColumns: true
})
el.appendChild(div)
} else {
throw "invalid call to table"
}
} else {
throw "invalid call to table"
}
}
},
{
name: "graph",
doc: "Print a graph",
action: (args) => {
console.log("TODO")
}
},
]
}

export function Application() {
let editor: EditorView = null;
onMount(() => {
Expand All @@ -20,10 +125,8 @@ export function Application() {
})

async function run() {
let result = await runWander(editor.state.doc.toString())
let value = readValue(result)
showText(document.querySelector("#results"), value)
//showGraph(document.querySelector("#results"), value)
document.querySelector("#results").textContent = "";
runScript(editor.state.doc.toString(), createUiCommands(document.querySelector("#results")))
}

return <div style="height:100%; width: 100%">
Expand Down
5 changes: 5 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ body {

#results {
font-family: SourceCodePro, monospace;
padding: 1em;
}

.table {
padding: 1em;
}

.grid {
Expand Down

0 comments on commit 955ec66

Please sign in to comment.