Skip to content

Instantly share code, notes, and snippets.

@kameko
Created November 15, 2018 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kameko/2b9548186ba8341a31cec36b77af1b3b to your computer and use it in GitHub Desktop.
Save kameko/2b9548186ba8341a31cec36b77af1b3b to your computer and use it in GitHub Desktop.
IO (so far)
"use strict";
Caesura.System.IO = function (args) {
args = args || {};
let hidden = Caesura.System.class(args, "Caesura.System.IO");
let reveal = hidden.reveal;
reveal.outputMethod = undefined;
reveal.defaultHTMLoutputElementId = args.defaultHTMLoutputElementId || "CaesuraIO";
reveal.newlineOptions = {
None: { detector: undefined, value: "" },
System: { detector: /[\n\r]/, value: "\n" },
HTML: { detector: /<br \/>/, value: "<br id='newline' />" },
};
reveal.indentationOptions = {
None: { detector: undefined, value: "" },
System: { detector: / /, value: " " },
HTML: { detector: /&nbsp;/, value: "&nbsp;" },
};
reveal.outputOptions = {
None: function (str) { },
System: function (str) {
if (str === reveal.newlineOptions.System.value) return; // console.log already newlines for us.
let out = str.toString();
out = out.replace(new RegExp(reveal.indentationOptions.HTML.detector, 'g'), reveal.indentationOptions.System.value);
if (Caesura.System.Backend.Platform == "Browser") {
// for some reason, the browser's console.log ignores the first \n in a string.
// this isn't a problem in desktop runtimes, so we need to account for that here.
out += reveal.newlineOptions.System.value;
}
console.log(out);
},
HTML: function (str) {
if (str === null || str === undefined) return;
let out = str.toString();
out = out.replace(new RegExp(reveal.newlineOptions.System.detector, 'g'), reveal.newlineOptions.HTML.value);
document.getElementById(reveal.defaultHTMLoutputElementId).innerHTML += out;
},
Browser: function (str) {
reveal.outputOptions.System(str);
reveal.outputOptions.HTML(str);
}
// TODO: runtime option that uses native runtime output. console.log won't cut it, automatically appends a newline.
}
if (args.outputMethod instanceof Function) {
reveal.outputMethod = args.outputMethod;
} else if (args.outputGenerator instanceof Function) {
reveal.outputMethod = args.outputGenerator(reveal);
} else if (args.Browser) {
reveal.outputMethod = reveal.outputOptions.Browser;
} else if (args.System) {
reveal.outputMethod = reveal.outputOptions.System;
} else if (args.HTML) {
reveal.outputMethod = reveal.outputOptions.HTML;
} else if (Caesura.System.Backend.Platform == "Browser" && !args.noAutomaticDefaultOptions) {
reveal.outputMethod = reveal.outputOptions.Browser;
} else if (Caesura.System.Backend.Platform == "Runtime" && !args.noAutomaticDefaultOptions) {
reveal.outputMethod = reveal.outputOptions.System;
}
reveal.write = function (str) {
hidden.raw_write(str, arguments)
}
reveal.writeln = function (str) {
if (str !== undefined) {
hidden.raw_write(str, arguments);
}
hidden.raw_write(reveal.newlineOptions.System.value);
}
hidden.raw_write = function (str, rest) {
let out = str;
// TODO: check if there are not an equal amount of {x} elements
// than arguments, and throw an exception.
if (rest !== undefined && rest.length > 0) {
for (let i = 1; i < rest.length; i++) {
out = out.replace(new RegExp("\{?(" + (i - 1) + ")\}", 'g'), rest[i]);
}
}
let regex = /\{s:(\d+)\}/g;
let match;
while ((match = regex.exec(out)) != null) {
out = out.replace(/\{s:(\d+)\}/g, reveal.indentationOptions.HTML.value.repeat(Number(match[1])));
}
reveal.outputMethod(out);
}
return reveal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment