-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
698 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/uni | ||
/dist | ||
/unidata/.cache | ||
/wasm/main.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<style> | ||
.light, .light input { background-color: #fff; color: #252525; } | ||
.dark, .dark input { background-color: #252525; color: #fff; } | ||
.sepia, .sepia input { background-color: #f4ecd8;; color: #5b4636;; } | ||
|
||
input { flex-grow: 1; } | ||
input, button { font: 16px monospace; border: 1px solid #666; } | ||
input:focus { box-shadow: 0px 0px .2em #6262fd;; } | ||
p { font: 14px sans-serif; margin-top: 1em; } | ||
div { display: flex; } | ||
</style> | ||
</head> | ||
<body class="light"> | ||
<pre id="output">uni WASM demo; <a href="https://github.com/arp242/uni">homepage</a>. Type <code>help</code> for usage; the <a href="https://github.com/arp242/uni/#usage">README</a> has more examples</a> | ||
|
||
</pre> | ||
<div><span>$ uni </span><input id="input"></div> | ||
<p> | ||
Color schemes: | ||
<button class="light">Light</button> | ||
<button class="dark">Dark</button> | ||
<button class="sepia">Sepia</button> | ||
| | ||
Up/down arrows to cycle history, ^L to clear the screen. | ||
</p> | ||
|
||
<script src="wasm_exec.js"></script> | ||
<script src="term.js"></script> | ||
<script> | ||
(function() { | ||
if (!('WebAssembly' in window)) { | ||
document.body.innerText = 'Sorry, you need a browser with WebAssembly support'; | ||
return; | ||
} | ||
|
||
set_output(window.output); | ||
|
||
fetch('main.wasm').then(response => response.arrayBuffer()).then(function(bin) { | ||
readline('uni', window.output, window.input, function(cmd) { | ||
const go = new Go(); | ||
go.argv = cmd; | ||
go.exit = (code) => { | ||
if (code > 0) | ||
output.innerText += 'Exit ' + code + '\n'; | ||
}; | ||
|
||
WebAssembly.instantiate(bin, go.importObject).then((result) => { | ||
go.run(result.instance); | ||
}); | ||
}); | ||
}); | ||
})(); | ||
</script> | ||
|
||
<script> | ||
if (location.hash !== '') | ||
document.body.className = location.hash.substr(1); | ||
|
||
document.querySelectorAll('button').forEach((elem) => { | ||
elem.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
document.body.className = elem.className; | ||
location.hash = elem.className; | ||
input.focus(); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
cd ../ && GOOS=js GOARCH=wasm go build -o wasm/main.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import http.server | ||
h = http.server.SimpleHTTPRequestHandler | ||
h.extensions_map = {'': 'text/html', '.wasm': 'application/wasm', '.js': 'application/javascript'} | ||
http.server.HTTPServer(('127.0.0.1', 2000), h).serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
(function() { | ||
// Write output to HTML element. | ||
window.set_output = (output) => { | ||
// Write stdout to terminal. | ||
let outputBuf = ''; | ||
const decoder = new TextDecoder("utf-8"); | ||
global.fs.writeSync = (fd, buf) => { | ||
outputBuf += decoder.decode(buf); | ||
const nl = outputBuf.lastIndexOf("\n"); | ||
if (nl != -1) { | ||
output.innerText += outputBuf.substr(0, nl + 1); | ||
window.scrollTo(0, document.body.scrollHeight); | ||
outputBuf = outputBuf.substr(nl + 1); | ||
} | ||
return buf.length; | ||
}; | ||
}; | ||
|
||
// Provide a readline-like input. | ||
window.readline = function(progname, output, input, cb) { | ||
var hist = [], hist_index = 0, reading_stdin = false; | ||
|
||
global.fs.read = (fd, buffer, offset, length, position, callback) => { | ||
reading_stdin = true; | ||
output.innerText += 'reading from stdin...\n' | ||
}; | ||
|
||
input.addEventListener('keydown', (e) => { | ||
//console.log(e.keyCode); | ||
|
||
// ^L | ||
if (e.ctrlKey && e.keyCode === 76) { | ||
e.preventDefault(); | ||
output.innerText = ''; | ||
} | ||
// ^P, arrow up | ||
else if ((e.ctrlKey && e.keyCode === 80) || e.keyCode === 38) { | ||
e.preventDefault(); | ||
input.value = hist[hist.length - hist_index - 1] || ''; | ||
if (hist_index < hist.length - 1) | ||
hist_index++; | ||
} | ||
// Arrow down; no ^N as it seems that can't be overridden: https://stackoverflow.com/q/38838302 | ||
else if (e.keyCode === 40) { | ||
e.preventDefault(); | ||
input.value = hist[hist.length - hist_index] || ''; | ||
if (hist_index > 0) | ||
hist_index--; | ||
} | ||
// Enter | ||
else if (e.keyCode === 13) { | ||
e.preventDefault(); | ||
|
||
if (progname !== '') | ||
output.innerText += '$ ' + progname + ' '; | ||
output.innerText += input.value + "\n"; | ||
|
||
if (reading_stdin) { | ||
reading_stdin = false; | ||
var cmd = (hist[hist.length - 1] + ' ' + input.value).split(' '); | ||
} | ||
else { | ||
hist.push(input.value); | ||
var cmd = input.value.split(' '); | ||
} | ||
|
||
input.value = ''; | ||
if (cmd.length === 0) | ||
return; | ||
if (cmd[0] !== progname) | ||
cmd = [progname].concat(cmd); | ||
|
||
cb(cmd); | ||
} | ||
}); | ||
|
||
// Focus on load. | ||
input.focus(); | ||
}; | ||
}()); |
Oops, something went wrong.