Add "get card field" command

This commit is contained in:
Tangent Wantwight 2023-10-18 22:55:34 -04:00
parent 68213df945
commit de4495a997
2 changed files with 35 additions and 20 deletions

View File

@ -1,19 +1,8 @@
import { ALL as HTML } from "./lib/html";
import { parse } from "./parser";
import { runNoctl, Vm } from "./vm";
import { AsHtml, AsText, TextPiece } from "./words";
/**
* Basic unit of information, also an "actor" in the programming system
*/
type Card = {
/** Unique identifier */
id: number;
/** Key-value properties on the card */
fields: Record<string, string>;
/** Eventually: a markdown string containing code, but for now, just code */
code: string;
};
import { Card, CardVm, GetField } from './lib/card';
import { ALL as HTML } from './lib/html';
import { parse } from './parser';
import { runNoctl, Vm } from './vm';
import { AsHtml, AsText, TextPiece } from './words';
/**
* Updates a card's fields
@ -43,7 +32,7 @@ function parseFields(card: Card, fields: string) {
* @param code Script to run
* @returns Markup to render / output
*/
function renderCard(state: Vm, code: string) {
function renderCard(state: CardVm, code: string) {
const script = parse(code);
if (script[0]) {
runNoctl(state, script[1], (word) => (state.output += AsHtml(word) + "\n"));
@ -128,10 +117,11 @@ function render() {
parseFields(theCard, fieldInput.value);
theCard.code = codeInput.value;
const vm: Vm = {
const vm: CardVm = {
mode: "render",
commands: { ...HTML },
commands: { ...HTML, get: GetField },
output: "",
card: theCard,
};
const html = renderCard(vm, theCard.code);
@ -145,7 +135,7 @@ document.body.append(
fieldInput,
codeInput,
rerender,
state,
display,
state,
debugDisplay
);

25
src/lib/card.ts Normal file
View File

@ -0,0 +1,25 @@
import { Vm } from '../vm';
import { TextPiece } from '../words';
import { getOpt } from './options';
/**
* Basic unit of information, also an "actor" in the programming system
*/
export type Card = {
/** Unique identifier */
id: number;
/** Key-value properties on the card */
fields: Record<string, string>;
/** Eventually: a markdown string containing code, but for now, just code */
code: string;
};
export type CardVm = Vm<{
card: Card;
}>;
export function GetField(vm: CardVm, argv: TextPiece[]): TextPiece {
const [{ error = false }, fieldName] = getOpt(argv, { min: 1, max: 1 });
// TODO: handle error
return { text: vm.card.fields[fieldName] ?? "" };
}