-
-
Save tryenc/92082b41789abd3ad4d6 to your computer and use it in GitHub Desktop.
wrapper around replit api and jqconsole
This file contains hidden or 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
(function(Trinket) { | |
function Replit(language) { | |
this._client = undefined; | |
this._connected = false; | |
this._running = false; | |
this._reconnect = false; | |
this._language = language; | |
this._history = []; | |
this._reinitClient = function() { | |
// disconnect current client | |
try { | |
this._client.disconnect(); | |
} catch(e) { | |
console.log('disconnect error:', e); | |
} | |
this._client = undefined; | |
// init new client | |
this._initClient(); | |
this._connected = false; | |
this._running = false; | |
this._reconnect = false; | |
} | |
this._initClient = function() { | |
if (this._client) return; | |
this._client = new ReplitClient( | |
'api.repl.it' | |
, '80' | |
, this._language | |
, { | |
'time_created' : trinket.config.replit.time_created | |
, 'msg_mac' : trinket.config.replit.msg_mac | |
} | |
); | |
} | |
this._initClient(); | |
} | |
Replit.prototype.getConnection = function() { | |
if (this._connected) { | |
if (this._running) { | |
this._reinitClient(); | |
} | |
else { | |
// reusing the connection seems to throw an error on evaluate | |
// reinit until that is resolved | |
// return $.Deferred().resolve().promise(); | |
this._reinitClient(); | |
} | |
} | |
else if (this._reconnect) { | |
this._reinitClient(); | |
} | |
return this._client.connect(); | |
} | |
Replit.prototype.startListening = function(jqconsole) { | |
var self = this; | |
jqconsole.Input(function(input) { | |
self._client.write(input + '\n'); | |
}); | |
} | |
Replit.prototype.startPrompt = function(jqconsole, postEvaluateCallback) { | |
var self = this; | |
var inMultilineString = false; | |
if (typeof postEvaluateCallback === 'undefined') { | |
postEvaluateCallback = function() {} | |
} | |
jqconsole.Prompt(true, function(input) { | |
if (/^\s*$/.test(input)) { | |
self.startPrompt(jqconsole); | |
} | |
else { | |
self._client.evaluate(input, { | |
stdout : function(out) { | |
jqconsole.Write(out); | |
if (!/\n$/.test(out) && jqconsole.GetState() !== "input") { | |
jqconsole.Input(function(input) { | |
self._client.write(input + '\n'); | |
}); | |
} | |
} | |
}).then(function(result) { | |
postEvaluateCallback(); | |
if (result) { | |
if (result.error) { | |
jqconsole.Write(result.error, 'jqconsole-error', false); | |
} | |
if (result.data && result.data !== "None") { | |
jqconsole.Write(result.data + '\n'); | |
} | |
} | |
if (jqconsole.GetState() === "input") { | |
jqconsole.AbortPrompt(); | |
} | |
self.startPrompt(jqconsole, postEvaluateCallback); | |
}); | |
} | |
}, function(input) { | |
postEvaluateCallback(); | |
var indent, last_line, lines; | |
lines = input.split('\n'); | |
if (lines.length === 0) { | |
return 0; | |
} else { | |
last_line = lines[lines.length - 1]; | |
indent = last_line.match(/^\s*/)[0]; | |
last_line = lines[lines.length - 1].replace(/\s+$/, ''); | |
if (/"""/.test(last_line) && !/""".*"""/.test(last_line)) { | |
inMultilineString = !inMultilineString; | |
} | |
jqconsole._inMultiline = true; | |
if (inMultilineString) { | |
return 0; | |
} else if (last_line[last_line.length - 1] === ':') { | |
return 1; | |
} else if (indent.length && last_line && last_line[last_line.length - 1].length !== 0) { | |
return 0; | |
} else if (/^\s*#/.test(last_line)) { | |
return 0; | |
} else { | |
jqconsole._inMultiline = false; | |
return false; | |
} | |
} | |
}); | |
jqconsole.Focus(); | |
self._enterHistory(jqconsole); | |
} | |
Replit.prototype.write = function(input) { | |
this._client.write(input); | |
} | |
Replit.prototype.connected = function(value) { | |
if (typeof value === 'boolean') { | |
this._connected = value; | |
} | |
return this._connected; | |
} | |
Replit.prototype.running = function(value) { | |
if (typeof value === 'boolean') { | |
this._running = value; | |
} | |
return this._running; | |
} | |
Replit.prototype.reconnect = function(value) { | |
if (typeof value === 'boolean') { | |
this._reconnect = value; | |
if (this._reconnect) { | |
this._connected = false; | |
this._running = false; | |
} | |
} | |
return this._reconnect; | |
} | |
Replit.prototype.client = function() { | |
return this._client; | |
} | |
Replit.prototype.evaluate = function(code, options) { | |
return this._client.evaluate(code, options); | |
} | |
Replit.prototype.setHistory = function(commands) { | |
this._history = commands; | |
} | |
Replit.prototype.disconnect = function() { | |
try { | |
this._client.disconnect(); | |
} catch(e) { | |
console.log('disconnect error:', e); | |
} | |
this._reconnect = true; | |
} | |
Replit.prototype._enterHistory = function(jqconsole) { | |
var command, chrs, i, jqEvent; | |
if (this._history.length) { | |
command = this._history.shift(); | |
chrs = command.split(''); | |
for (i = 0; i < chrs.length; i++) { | |
jqEvent = $.Event('keypress'); | |
jqEvent.which = chrs[i].charCodeAt(0); | |
jqconsole.$input_source.trigger(jqEvent); | |
} | |
jqEvent = $.Event('keydown'); | |
jqEvent.which = '\r'.charCodeAt(0); | |
jqconsole.$input_source.trigger(jqEvent) | |
if (jqconsole._inMultiline) { | |
jqconsole._inMultiline = false; | |
if (this._history.length && /^\s+/.test(this._history[0])) { | |
this._history[0] = this._history[0].replace(/^\s+/, ''); | |
this._enterHistory(jqconsole); | |
} | |
else { | |
jqEvent = $.Event('keydown'); | |
jqEvent.which = '\r'.charCodeAt(0); | |
jqconsole.$input_source.trigger(jqEvent) | |
} | |
} | |
} | |
} | |
Trinket.export('embed.replit', Replit); | |
})(window.TrinketIO); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment