258 lines
6.9 KiB
JavaScript
258 lines
6.9 KiB
JavaScript
var Command = function(terminal) {
|
|
|
|
this.terminal = terminal;
|
|
|
|
this.commandList = [];
|
|
|
|
this.parser = new Parser();
|
|
|
|
var self = this;
|
|
|
|
this.run = function(line) {
|
|
if (self.inputEcho) {
|
|
self.terminal.ui.print(terminal.ui.prompt.textContent);
|
|
self.terminal.ui.println(line);
|
|
}
|
|
if (line.length > 0) {
|
|
self.parser.setLine(line);
|
|
self.parser.parse();
|
|
var args = self.parser.tokens;
|
|
var f = self['command_' + args[0]];
|
|
if (f != null) {
|
|
try {
|
|
f(args);
|
|
} catch (e) {
|
|
self.printErrorLine(e);
|
|
}
|
|
} else {
|
|
if (self.terminal.ws.webSocket.readyState == WebSocket.OPEN) {
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.terminal.printResult;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'Command', args: args }
|
|
));
|
|
} else {
|
|
self.println('Not connected');
|
|
}
|
|
}
|
|
}
|
|
if (self.inputCallback != null) {
|
|
self.inputCallback();
|
|
}
|
|
};
|
|
|
|
this.command_help = function(args) {
|
|
self.println('');
|
|
self.println('- Short Help -');
|
|
self.println('Press TAB to use input completion.');
|
|
self.println('To show input history press UP or DOWN.');
|
|
self.println('');
|
|
self.println('- Command List -');
|
|
for (var i = 0; i < self.commandList.length; i++) {
|
|
self.print(' ' + self.commandList[i]);
|
|
}
|
|
self.println('');
|
|
self.println('');
|
|
};
|
|
|
|
this.command_clear = function(args) {
|
|
self.terminal.ui.clear();
|
|
};
|
|
|
|
this.loginCallback = function(account, password) {
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.terminal.printResult;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'Command', args: [ 'login', account, password ] }
|
|
));
|
|
self.terminal.loop();
|
|
};
|
|
|
|
this.command_login = function(args) {
|
|
self.terminal.ui.login(self.loginCallback);
|
|
};
|
|
|
|
this.command_args = function(args) {
|
|
for (var i = 0; i < args.length; i++) {
|
|
self.println(i + ': ' + args[i]);
|
|
}
|
|
};
|
|
|
|
this.command_echo = function(args) {
|
|
args.shift();
|
|
self.println(args.join(' '));
|
|
};
|
|
|
|
this.command_date = function(args) {
|
|
self.println(new Date());
|
|
};
|
|
|
|
this.command_pwd = function(args) {
|
|
self.println(self.terminal.session.currentDirectory);
|
|
};
|
|
|
|
this.command_cd = function(args) {
|
|
if (args.length < 2) {
|
|
self.terminal.session.changeDirectory(self.terminal.session.homeDirectory);
|
|
} else {
|
|
self.terminal.session.changeDirectory(args[1]);
|
|
}
|
|
};
|
|
|
|
this.lsCallback = function(packet, args) {
|
|
if (packet.opcode == 'FileList') {
|
|
for (var i = 0; i < packet.list.length; i++) {
|
|
if (packet.list[i].type == 'DIRECTORY') {
|
|
self.terminal.ui.println(packet.list[i].name, '#4080ff');
|
|
} else if (packet.list[i].type == 'FILE') {
|
|
self.terminal.ui.println(packet.list[i].name);
|
|
} else if (packet.list[i].type == 'OTHER') {
|
|
} else {
|
|
self.terminal.ui.println(packet.list[i].name, '#ff4040');
|
|
}
|
|
}
|
|
} else if (packet.opcode == 'FileException') {
|
|
self.printErrorLine(packet.type);
|
|
}
|
|
};
|
|
|
|
this.command_ls = function(args) {
|
|
if (args.length < 2) {
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.lsCallback;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'list', self.terminal.session.currentDirectory ] }
|
|
));
|
|
} else {
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.lsCallback;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'list', self.terminal.session.toAbsolutePath(args[1]) ] }
|
|
));
|
|
}
|
|
};
|
|
|
|
this.catCallback = function(packet, args) {
|
|
if (packet.opcode == 'File') {
|
|
self.print(Unicode.fromUTF8(Base64.decode(packet.content)));
|
|
} else if (packet.opcode == 'FileException') {
|
|
self.printErrorLine(packet.type);
|
|
} else {
|
|
self.printErrorLine('Unknown opcode: ' + packet.opcode);
|
|
}
|
|
};
|
|
|
|
this.command_cat = function(args) {
|
|
if (args.length < 2) {
|
|
self.printErrorLine(args[0] + ': missing operand');
|
|
return;
|
|
}
|
|
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.catCallback;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'load', self.terminal.session.toAbsolutePath(args[1]) ] }
|
|
));
|
|
};
|
|
|
|
this.rmCallback = function(packet, args) {
|
|
if (packet.opcode == 'FileException') {
|
|
self.printErrorLine(packet.type);
|
|
}
|
|
};
|
|
|
|
this.command_rm = function(args) {
|
|
if (args.length < 2) {
|
|
self.printErrorLine(args[0] + ': missing operand');
|
|
return;
|
|
}
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.rmCallback;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'delete', self.terminal.session.toAbsolutePath(args[1]) ] }
|
|
));
|
|
};
|
|
|
|
this.mkdirCallback = function(packet, args) {
|
|
if (packet.opcode == 'FileException') {
|
|
self.printErrorLine(packet.type);
|
|
}
|
|
};
|
|
|
|
this.command_mkdir = function(args) {
|
|
if (args.length < 2) {
|
|
self.printErrorLine(args[0] + ': missing operand');
|
|
return;
|
|
}
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.mkdirCallback;
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'createDirectory', self.terminal.session.toAbsolutePath(args[1]) ] }
|
|
));
|
|
};
|
|
|
|
this.viCallback = function(packet, args) {
|
|
var content = '';
|
|
if (packet.opcode == 'File') {
|
|
content = Unicode.fromUTF8(Base64.decode(packet.content));
|
|
} else if (packet.opcode == 'FileException') {
|
|
if (packet.type == 'IS_DIRECTORY') {
|
|
self.printErrorLine(packet.type);
|
|
return;
|
|
}
|
|
}
|
|
|
|
var path = self.terminal.session.toAbsolutePath(args[0]);
|
|
var editor = new TextEditor(self.terminal);
|
|
editor.name.textContent = path;
|
|
editor.area.value = content;
|
|
self.terminal.ui.append(editor.node);
|
|
};
|
|
|
|
this.command_vi = function(args) {
|
|
if (args.length < 2) {
|
|
self.printErrorLine(args[0] + ': missing operand');
|
|
return;
|
|
}
|
|
var id = self.terminal.nextID();
|
|
self.terminal.callbacks[id.toString()] = self.viCallback;
|
|
var path = self.terminal.session.toAbsolutePath(args[1]);
|
|
self.terminal.args[id.toString()] = [ path ];
|
|
self.terminal.ws.webSocket.send(JSON.stringify(
|
|
{ id: id, opcode: 'FileAPI', args: [ 'load', path ] }
|
|
));
|
|
};
|
|
|
|
for (var v in this) {
|
|
if (v.indexOf('command_') == 0) {
|
|
this.commandList.push(v.substr('command_'.length));
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
Command.prototype.COLOR = '#40a060';
|
|
Command.prototype.ERROR_COLOR = '#e050b0';
|
|
|
|
Command.prototype.print = function(s) {
|
|
this.terminal.ui.print(s, this.COLOR);
|
|
};
|
|
|
|
Command.prototype.println = function(s) {
|
|
this.print(s + '\n');
|
|
};
|
|
|
|
Command.prototype.printError = function(s) {
|
|
this.terminal.ui.print(s, this.ERROR_COLOR);
|
|
};
|
|
|
|
Command.prototype.printErrorLine = function(s) {
|
|
this.printError(s + '\n');
|
|
};
|
|
|
|
Command.prototype.readLine = function(callback, echo) {
|
|
this.inputCallback = callback;
|
|
this.inputEcho = echo;
|
|
this.terminal.ui.readLine(this.run, this.commandList);
|
|
};
|