Skip to content

Commit

Permalink
Merge branch 'work'
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPork committed Sep 14, 2017
2 parents 53a6ab4 + bb76f87 commit e3b21ad
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 97 deletions.
8 changes: 2 additions & 6 deletions _build_js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ mkdir -p out/js
echo 'Generating lang.js...'
php ./dump_js_lang.php

if [[ $ESP_DEMO ]]; then
demofile=js/demo.js
else
demofile=
fi

echo 'Processing JS...'
if [[ $ESP_PROD ]]; then
smarg=
demofile=
else
smarg=--source-maps
demofile=js/demo.js
fi

npm run babel -- -o "out/js/app.$FRONT_END_HASH.js" ${smarg} js/lib \
Expand Down
2 changes: 1 addition & 1 deletion _debug_replacements.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
'term_height' => '25',
'default_bg' => '0',
'default_fg' => '7',
'show_buttons' => '0',
'show_buttons' => '1',
'show_config_links' => '1',

'uart_baud' => 115200,
Expand Down
22 changes: 15 additions & 7 deletions base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@
parse_str($argv[1], $_GET);
}

if (!file_exists(__DIR__ . '/_env.php')) {
die("Copy <b>_env.php.example</b> to <b>_env.php</b> and check the settings inside!");
}

define('GIT_HASH', trim(shell_exec('git rev-parse --short HEAD')));

require_once __DIR__ . '/_env.php';

$prod = defined('STDIN');
define('DEBUG', !$prod);
$root = DEBUG ? json_encode(ESP_IP) : 'location.host';

// Resolve hostname for ajax etc
$root = 'location.host';
if (!file_exists(__DIR__ . '/_env.php')) {
if (DEBUG) {
die("No _env.php found! Copy _env.php.example</b> to <b>_env.php</b> and check the settings inside!");
}
} else {
if (DEBUG) {
require_once __DIR__ . '/_env.php';
$root = json_encode(ESP_IP);
}
}

define('JS_WEB_ROOT', $root);


define('ESP_DEMO', (bool)getenv('ESP_DEMO'));
if (ESP_DEMO) {
define('DEMO_APS', <<<APS
Expand Down
2 changes: 1 addition & 1 deletion js/appcommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ $.ready(function () {
Modal.init()
Notify.init()

// remove tabindixes from h2 if wide
// remove tabindices from h2 if wide
if (window.innerWidth > 550) {
$('.Box h2').forEach(function (x) {
x.removeAttribute('tabindex')
Expand Down
181 changes: 164 additions & 17 deletions js/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ class ANSIParser {
// something something nothing
this.currentSequence = 0
this.handler('write', character)
} else if (code === 0x07) this.handler('bell')
} else if (code < 0x03) this.handler('_null')
else if (code === 0x03) this.handler('sigint')
else if (code <= 0x06) this.handler('_null')
else if (code === 0x07) this.handler('bell')
else if (code === 0x08) this.handler('back')
else if (code === 0x0a) this.handler('new-line')
else if (code === 0x0d) this.handler('return')
Expand Down Expand Up @@ -203,8 +206,8 @@ class ScrollingTerminal {
} else if (action === 'return') {
this.cursor.x = 0
} else if (action === 'set-cursor') {
this.cursor.x = args[0]
this.cursor.y = args[1]
this.cursor.x = args[1]
this.cursor.y = args[0]
this.clampCursor()
} else if (action === 'move-cursor-y') {
this.cursor.y += args[0]
Expand Down Expand Up @@ -370,7 +373,8 @@ let demoData = {
}
setTimeout(loop, 200)
}
}
},
mouseReceiver: null
}

let demoshIndex = {
Expand Down Expand Up @@ -613,6 +617,115 @@ let demoshIndex = {
this.destroy()
}
},
mouse: class ShowMouse extends Process {
constructor (shell) {
super()
this.shell = shell
}
run () {
this.shell.terminal.trackMouse = true
demoData.mouseReceiver = this
this.randomData = []
this.highlighted = {}
let characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
for (let i = 0; i < 23; i++) {
let line = ''
for (let j = 0; j < 79; j++) {
line += characters[Math.floor(characters.length * Math.random())]
}
this.randomData.push(line)
}
this.scrollOffset = 0
this.render()
}
render () {
this.emit('write', '\x1b[m\x1b[2J\x1b[1;1H')
this.emit('write', '\x1b[97m\x1b[1mMouse Demo\r\n\x1b[mMouse movement, clicking and scrolling!')

// render random data for scrolling
for (let y = 0; y < 23; y++) {
let index = y + this.scrollOffset
// proper modulo:
index = ((index % this.randomData.length) + this.randomData.length) % this.randomData.length
let line = this.randomData[index]
let lineData = `\x1b[${3 + y};1H\x1b[38;5;239m`
for (let x in line) {
if (this.highlighted[(y + 2) * 80 + (+x)]) lineData += '\x1b[97m'
lineData += line[x]
if (this.highlighted[(y + 2) * 80 + (+x)]) lineData += '\x1b[38;5;239m'
}
this.emit('write', lineData)
}

// move cursor to mouse
if (this.mouse) {
this.emit('write', `\x1b[${this.mouse.y + 1};${this.mouse.x + 1}H`)
}
}
mouseMove (x, y) {
this.mouse = { x, y }
this.render()
}
mouseDown (x, y, button) {
if (button === 4) this.scrollOffset--
else if (button === 5) this.scrollOffset++
else this.highlighted[y * 80 + x] = !this.highlighted[y * 80 + x]
this.render()
}
mouseUp (x, y, button) {}
destroy () {
this.shell.terminal.write('\x1b[2J\x1b[1;1H')
this.shell.terminal.trackMouse = false
if (demoData.mouseReceiver === this) demoData.mouseReceiver = null
super.destroy()
}
},
sudo: class Sudo extends Process {
run (...args) {
if (args.length === 0) this.emit('write', '\x1b[31musage: sudo <command>\x1b[0m\n')
else if (args.length === 4 && args.join(' ').toLowerCase() === 'make me a sandwich') {
const b = '\x1b[33m'
const r = '\x1b[0m'
const l = '\x1b[32m'
const c = '\x1b[38;5;229m'
const h = '\x1b[38;5;225m'
this.emit('write',
` ${b}_.---._\r\n` +
` _.-~ ~-._\r\n` +
` _.-~ ~-._\r\n` +
` _.-~ ~---._\r\n` +
` _.-~ ~\\\r\n` +
` .-~ _.;\r\n` +
` :-._ _.-~ ./\r\n` +
` \`-._~-._ _..__.-~ _.-~\r\n` +
` ${c}/ ${b}~-._~-._ / .__..--${c}~-${l}---._\r\n` +
`${c} \\_____(_${b};-._\\. _.-~_/${c} ~)${l}.. . \\\r\n` +
`${l} /(_____ ${b}\\\`--...--~_.-~${c}______..-+${l}_______)\r\n` +
`${l} .(_________/${b}\`--...--~/${l} _/ ${h} ${b}/\\\r\n` +
`${b} /-._${h} \\_ ${l}(___./_..-~${h}__.....${b}__..-~./\r\n` +
`${b} \`-._~-._${h} ~\\--------~ .-~${b}_..__.-~ _.-~\r\n` +
`${b} ~-._~-._ ${h}~---------\` ${b}/ .__..--~\r\n` +
`${b} ~-._\\. _.-~_/\r\n` +
`${b} \\\`--...--~_.-~\r\n` +
`${b} \`--...--~${r}\r\n`)
} else {
this.emit('exec', args.join(' '))
return
}
this.destroy()
}
},
make: class Make extends Process {
run (...args) {
if (args.length === 0) this.emit('write', '\x1b[31mmake: *** No targets specified. Stop.\x1b[0m\r\n')
else if (args.length === 3 && args.join(' ').toLowerCase() === 'me a sandwich') {
this.emit('write', '\x1b[31mmake: me a sandwich : Permission denied\x1b[0m\r\n')
} else {
this.emit('write', `\x1b[31mmake: *** No rule to make target '${args.join(' ').toLowerCase()}'. Stop.\x1b[0m\r\n`)
}
this.destroy()
}
},
pwd: '/this/is/a/demo\r\n',
cd: '\x1b[38;5;239mNo directories to change to\r\n',
whoami: `${window.navigator.userAgent}\r\n`,
Expand All @@ -624,15 +737,22 @@ let demoshIndex = {
mv: '\x1b[38;5;239mNothing to move because this is a demo.\r\n',
ln: '\x1b[38;5;239mNothing to link because this is a demo.\r\n',
touch: '\x1b[38;5;239mNothing to touch\r\n',
exit: '\x1b[38;5;239mNowhere to go\r\n'
exit: '\x1b[38;5;239mNowhere to go\r\n',
github: class GoToGithub extends Process {
run () {
window.open('https://github.com/espterm/espterm-firmware')
this.destroy()
}
}
}

class DemoShell {
constructor (terminal, printInfo) {
this.terminal = terminal
this.terminal.reset()
this.parser = new ANSIParser((...args) => this.handleParsed(...args))
this.input = ''
this.history = []
this.historyIndex = 0
this.cursorPos = 0
this.child = null
this.index = demoshIndex
Expand All @@ -651,38 +771,53 @@ class DemoShell {
this.terminal.write('\x1b[34;1mdemosh \x1b[m')
if (!success) this.terminal.write('\x1b[31m')
this.terminal.write('$ \x1b[m')
this.input = ''
this.history.unshift('')
this.cursorPos = 0
}
copyFromHistoryIndex () {
if (!this.historyIndex) return
let current = this.history[this.historyIndex]
this.history[0] = current
this.historyIndex = 0
}
handleParsed (action, ...args) {
this.terminal.write('\b\x1b[P'.repeat(this.cursorPos))
if (action === 'write') {
this.input = this.input.substr(0, this.cursorPos) + args[0] + this.input.substr(this.cursorPos)
this.copyFromHistoryIndex()
this.history[0] = this.history[0].substr(0, this.cursorPos) + args[0] + this.history[0].substr(this.cursorPos)
this.cursorPos++
} else if (action === 'back') {
this.input = this.input.substr(0, this.cursorPos - 1) + this.input.substr(this.cursorPos)
this.copyFromHistoryIndex()
this.history[0] = this.history[0].substr(0, this.cursorPos - 1) + this.history[0].substr(this.cursorPos)
this.cursorPos--
if (this.cursorPos < 0) this.cursorPos = 0
} else if (action === 'move-cursor-x') {
this.cursorPos = Math.max(0, Math.min(this.input.length, this.cursorPos + args[0]))
this.cursorPos = Math.max(0, Math.min(this.history[0].length, this.cursorPos + args[0]))
} else if (action === 'delete-line') {
this.input = ''
this.copyFromHistoryIndex()
this.history[0] = ''
this.cursorPos = 0
} else if (action === 'delete-word') {
let words = this.input.substr(0, this.cursorPos).split(' ')
this.copyFromHistoryIndex()
let words = this.history[0].substr(0, this.cursorPos).split(' ')
words.pop()
this.input = words.join(' ') + this.input.substr(this.cursorPos)
this.history[0] = words.join(' ') + this.history[0].substr(this.cursorPos)
this.cursorPos = words.join(' ').length
} else if (action === 'move-cursor-y') {
this.historyIndex -= args[0]
if (this.historyIndex < 0) this.historyIndex = 0
if (this.historyIndex >= this.history.length) this.historyIndex = this.history.length - 1
this.cursorPos = this.history[this.historyIndex].length
}

this.terminal.write(this.input)
this.terminal.write('\b'.repeat(this.input.length))
this.terminal.write(this.history[this.historyIndex])
this.terminal.write('\b'.repeat(this.history[this.historyIndex].length))
this.terminal.moveForward(this.cursorPos)
this.terminal.write('') // dummy. Apply the moveFoward

if (action === 'return') {
this.terminal.write('\r\n')
this.parse(this.input)
this.parse(this.history[this.historyIndex])
}
}
parse (input) {
Expand Down Expand Up @@ -719,9 +854,12 @@ class DemoShell {
if (Process instanceof Function) {
this.child = new Process(this)
let write = data => this.terminal.write(data)
let exec = line => this.run(line)
this.child.on('write', write)
this.child.on('exec', exec)
this.child.on('exit', code => {
if (this.child) this.child.off('write', write)
if (this.child) this.child.off('exec', exec)
this.child = null
this.prompt(!code)
})
Expand All @@ -748,7 +886,16 @@ window.demoInterface = {
else if (action instanceof Function) action(this.terminal, this.shell)
}
} else if (type === 'm' || type === 'p' || type === 'r') {
console.log(JSON.stringify(data))
let row = parse2B(content, 0)
let column = parse2B(content, 2)
let button = parse2B(content, 4)
let modifiers = parse2B(content, 6)

if (demoData.mouseReceiver) {
if (type === 'm') demoData.mouseReceiver.mouseMove(column, row, button, modifiers)
else if (type === 'p') demoData.mouseReceiver.mouseDown(column, row, button, modifiers)
else if (type === 'r') demoData.mouseReceiver.mouseUp(column, row, button, modifiers)
}
}
},
init (screen) {
Expand Down
16 changes: 6 additions & 10 deletions js/soft_keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ window.initSoftKeyboard = function (screen, input) {

let keyboardOpen = false

// moves the input to where the cursor is on the canvas.
// this is because most browsers will always scroll to wherever the focused
// input is
let updateInputPosition = function () {
if (!keyboardOpen) return

Expand All @@ -20,16 +23,9 @@ window.initSoftKeyboard = function (screen, input) {

screen.on('cursor-moved', updateInputPosition)

let kbOpen = function (open) {
keyboardOpen = open
updateInputPosition()
if (open) keyInput.focus()
else keyInput.blur()
}

qs('#term-kb-open').addEventListener('click', function () {
kbOpen(true)
return false
qs('#term-kb-open').addEventListener('click', e => {
e.preventDefault()
keyInput.focus()
})

// Chrome for Android doesn't send proper keydown/keypress events with
Expand Down
4 changes: 1 addition & 3 deletions js/term.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/** Init the terminal sub-module - called from HTML */
window.termInit = function (opts) {
let { labels, theme, allFn } = opts

window.termInit = function ({ labels, theme, allFn }) {
const screen = new TermScreen()
const conn = Conn(screen)
const input = Input(conn)
Expand Down
4 changes: 2 additions & 2 deletions js/term_conn.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ window.Conn = function (screen) {

return {
ws: null,
init: init,
init,
send: doSend,
canSend: canSend // check flood control
canSend // check flood control
}
}

0 comments on commit e3b21ad

Please sign in to comment.