Skip to content

Commit

Permalink
REPL polishing.
Browse files Browse the repository at this point in the history
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

rabchev committed Mar 31, 2013
1 parent 8b03e60 commit 1fe4ca1
Showing 3 changed files with 65 additions and 18 deletions.
55 changes: 42 additions & 13 deletions lib/shell.js
Original file line number Diff line number Diff line change
@@ -78,11 +78,14 @@ function initialize(server, options, fn) {

var cwd = process.cwd(),
env = process.env,
linebreak = "\n", // check if we need to add \r\n for windows
promptChar = process.platform === "wind32" ? ">" : "$",
stdin,
args,
cmd,
proc,
dir;
dir,
replSrv;

io = io.listen(server, { log: false });
io.sockets.on("connection", function (socket) {
@@ -91,8 +94,8 @@ function initialize(server, options, fn) {
if (env.WEB_SHELL) {
proc = spawn(env.WEB_SHELL, null, { cwd: cwd, env: env });
stdin = proc.stdin;
stdin.write(command);
stdin.end();
stdin.write(command + linebreak);
//stdin.end();
} else {
proc = spawn(cmd, args, { cwd: cwd, env: env });
stdin = proc.stdin;
@@ -126,7 +129,19 @@ function initialize(server, options, fn) {
});

socket.on("signal", function (signal) {
if (proc) {
var cmd;

if (replSrv) {
switch (signal) {
case "SIGINT":
cmd = ".break";
break;
case "SIGQUIT":
cmd = ".exit";
break;
}
stdin.write(cmd + linebreak);
} else if (proc) {
proc.kill(signal);
}
});
@@ -135,9 +150,7 @@ function initialize(server, options, fn) {
var i, arg;

if (stdin) {
console.log(command);
stdin.write(command + "\n");
//stdin.end();
stdin.write(command + linebreak);
} else {
args = cmdLine.parse(command);
cmd = args.splice(0, 1)[0];
@@ -198,29 +211,45 @@ function initialize(server, options, fn) {

stdin = input;
output.on("data", function (data) {
console.log(data);
socket.emit("console", data);
});

repl.start({
replSrv = repl.start({
prompt: "> ",
input: input,
output: output,
terminal: false,
useColors: true
}).on("exit", function () {
console.log("REPL exited!");
});

replSrv.on("exit", function () {
stdin = null;
socket.emit("exit", "");
socket.emit("configure", {
prompt : cwd,
promptChar : promptChar
});
socket.emit("exit");
replSrv = null;
});

socket.emit("configure", {
prompt : "",
promptChar : ">"
});

break;
default:
execCmd(command);
}
}
});

socket.emit("exit", "cwd: " + cwd);
socket.emit("configure", {
srvOS : process.platform,
prompt : cwd,
promptChar : promptChar
});
socket.emit("exit");
});
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -43,5 +43,5 @@
"scripts": {
"start": "node ./bin/run.js"
},
"engines": { "node": ">=0.8.6" }
"engines": { "node": ">=0.10" }
}
26 changes: 22 additions & 4 deletions web/init.js
Original file line number Diff line number Diff line change
@@ -47,13 +47,15 @@
"46" : ["<span style=\"background-color:cyan;\">", "</span>"],
"47" : ["<span style=\"background-color:white;\">", "</span>"]
},
prompt = "~$",
prompt = "~",
promptChar = "",
cursorPos = 0,
uiLineIdx = 0,
uiLineWrp,
uiLineCnt,
uiLineSuf,
cursor;
cursor,
srvOS;

function parseVT100(i, data, closures) {
var code = "",
@@ -187,7 +189,7 @@

function addNewLine() {
var id = "ln" + ++uiLineIdx;
appendContent("<div id=\"" + id + "\">" + prompt + "&nbsp;<span id=\"lnCnt\"></span><span id=\"cursor\" class=\"inverse\">&nbsp;</span><span id=\"lnSuf\"></span></div>");
appendContent("<div id=\"" + id + "\">" + prompt + promptChar + "&nbsp;<span id=\"lnCnt\"></span><span id=\"cursor\" class=\"inverse\">&nbsp;</span><span id=\"lnSuf\"></span></div>");
uiLineWrp = $("#" + id);
uiLineCnt = uiLineWrp.find("#lnCnt");
uiLineSuf = uiLineWrp.find("#lnSuf");
@@ -323,15 +325,31 @@
socket.emit("signal", "SIGINT");
appendContent("^C");
break;
case 68:
socket.emit("signal", "SIGQUIT");
appendContent("^D");
break;
}
}
});

socket.on("configure", function (data) {
if (data.srvOS) {
srvOS = data.srvOS;
}
if (data.prompt || data.prompt === "") {
prompt = data.prompt;
}
if (data.promptChar) {
promptChar = data.promptChar;
}
});

socket.on("exit", function (data) {
clearCursor();
if (data) {
if (data.indexOf("cwd: ") === 0) {
prompt = data.substr(5) + "$";
prompt = data.substr(5) + promptChar;
} else {
appendContent(data);
}

0 comments on commit 1fe4ca1

Please sign in to comment.