paradoxxxzero_butterfly/coffees/main.coffee
2015-04-14 14:23:10 +02:00

113 lines
3 KiB
CoffeeScript

# *-* coding: utf-8 *-*
# This file is part of butterfly
#
# butterfly Copyright (C) 2014 Florian Mounier
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cols = rows = null
quit = false
open_ts = (new Date()).getTime()
$ = document.querySelectorAll.bind(document)
document.addEventListener 'DOMContentLoaded', ->
send = (data) ->
ws.send 'S' + data
ctl = (type, args...) ->
params = args.join(',')
if type == 'Resize'
ws.send 'R' + params
if location.protocol == 'https:'
ws_url = 'wss://'
else
ws_url = 'ws://'
ws_url += document.location.host + '/ws' + location.pathname
ws = new WebSocket ws_url
ws.addEventListener 'open', ->
console.log "WebSocket open", arguments
ws.send 'R' + term.cols + ',' + term.rows
open_ts = (new Date()).getTime()
ws.addEventListener 'error', ->
console.log "WebSocket error", arguments
t_stop = null
last_data = ''
ws.addEventListener 'message', (e) ->
if term.stop
last_data += e.data
last_data = last_data.slice(-10 * 1024) # Keep last 10kb
if t_stop
clearTimeout t_stop if t_stop
t_stop = setTimeout ->
term.stop = false
term.element.classList.remove 'stopped'
term.write last_data
last_data = ''
t_stop = null
, 100
return
term.write e.data
ws.addEventListener 'close', ->
console.log "WebSocket closed", arguments
setTimeout ->
term.write 'Closed'
# Allow quick reload
term.skipNextKey = true
term.element.classList.add('dead')
, 1
quit = true
# Don't autoclose if websocket didn't last 1 minute
if (new Date()).getTime() - open_ts > 60 * 1000
open('','_self').close()
term = new Terminal document.body, send, ctl
addEventListener 'beforeunload', ->
if not quit
'This will exit the terminal session'
term.ws = ws
window.butterfly = term
window.bench = (n=100000000) ->
rnd = ''
while rnd.length < n
rnd += Math.random().toString(36).substring(2)
console.time('bench')
console.profile('bench')
term.write rnd
console.profileEnd()
console.timeEnd('bench')
window.cbench = (n=100000000) ->
rnd = ''
while rnd.length < n
rnd += "\x1b[#{30 + parseInt(Math.random() * 20)}m"
rnd += Math.random().toString(36).substring(2)
console.time('cbench')
console.profile('cbench')
term.write rnd
console.profileEnd()
console.timeEnd('cbench')