Skip to content
Permalink
Browse files

Wasm version (#9)

  • Loading branch information
arp242 committed Feb 4, 2020
1 parent aa510d6 commit bfd9a565343bce6469c67ea2ae3accad597afcb4
Showing with 698 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 README.markdown
  3. +1 −1 terminal/util.go
  4. +1 −1 uni.go
  5. +72 −0 wasm/index.html
  6. +3 −0 wasm/make
  7. +6 −0 wasm/srv
  8. +80 −0 wasm/term.js
  9. +533 −0 wasm/wasm_exec.js
@@ -1,3 +1,4 @@
/uni
/dist
/unidata/.cache
/wasm/main.wasm
@@ -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)

@@ -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"

2 uni.go
@@ -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)
@@ -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>
@@ -0,0 +1,3 @@
#!/bin/sh

cd ../ && GOOS=js GOARCH=wasm go build -o wasm/main.wasm
@@ -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()
@@ -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();
};
}());

0 comments on commit bfd9a56

Please sign in to comment.
You can’t perform that action at this time.