Skip to content

Commit

Permalink
Wasm version (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 authored Feb 4, 2020
1 parent aa510d6 commit bfd9a56
Show file tree
Hide file tree
Showing 9 changed files with 698 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/uni
/dist
/unidata/.cache
/wasm/main.wasm
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ Alternatives
- https://github.com/salty-horse/ibus-uniemoji
- https://fcitx-im.org/wiki/Unicode
- http://kassiopeia.juls.savba.sk/~garabik/software/unicode/
https://github.com/garabik/unicode (same?)
- https://billposer.org/Software/unidesc.html
- https://github.com/NoraCodes/charpicker (rofi)

Expand Down
2 changes: 1 addition & 1 deletion terminal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build plan9
// +build plan9 js

package terminal // import "arp242.net/uni/terminal"

Expand Down
2 changes: 1 addition & 1 deletion uni.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func identify(ins []string, quiet, raw bool) error {
for _, c := range in {
info, ok := unidata.FindCodepoint(c)
if !ok {
return fmt.Errorf("unknown codepoint: %.4X", c) // Should never happen.
return fmt.Errorf("unknown codepoint: U+%.4X", c) // Should never happen.
}

out = append(out, info)
Expand Down
72 changes: 72 additions & 0 deletions wasm/index.html
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&nbsp;</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>
3 changes: 3 additions & 0 deletions wasm/make
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
6 changes: 6 additions & 0 deletions wasm/srv
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()
80 changes: 80 additions & 0 deletions wasm/term.js
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();
};
}());
Loading

0 comments on commit bfd9a56

Please sign in to comment.