Files
rainmelt/html/Command.js
2012-08-06 22:49:21 +09:00

261 lines
6.6 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;
var command = args.shift();
self.terminal.ws.webSocket.send(JSON.stringify(
{ id: id, opcode: 'Command', command: command, args: args }
));
} else {
self.println('Not connected');
}
}
}
if (self.inputCallback != null) {
self.inputCallback();
}
};
this.helpCallback = function(packet, args) {
if (packet.opcode == 'StringList') {
self.println('- Remote Command List -');
for (var i = 0; i < packet.list.length; i++) {
self.print(' ' + packet.list[i]);
}
self.println('');
} else {
self.printErrorLine(packet.opcode);
}
};
this.command_help = function(args) {
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('- Local Command List -');
for (var i = 0; i < self.commandList.length; i++) {
self.print(' ' + self.commandList[i]);
}
self.println('');
self.println('');
self.terminal.api.commandList(self.helpCallback, null);
};
this.command_clear = function(args) {
self.terminal.ui.clear();
};
this.whoCallback = function(packet, args) {
if (packet.opcode == 'AccountList') {
for (var i = 0; i < packet.list.length; i++) {
self.println(packet.list[i].name);
}
} else {
self.printErrorLine(packet.opcode);
}
};
this.command_who = function(args) {
self.terminal.api.who(self.whoCallback, null);
};
this.loginCallback = function(account, password) {
self.terminal.api.login(self.terminal.printResult, null, 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) {
self.terminal.fileAPI.list(self.lsCallback, null, self.terminal.session.currentDirectory);
} else {
self.terminal.fileAPI.list(self.lsCallback, null, 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;
}
self.terminal.fileAPI.load(self.catCallback, null, 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;
}
self.terminal.fileAPI.delete(self.rmCallback, null, 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;
}
self.terminal.fileAPI.createDirectory(self.mkdirCallback, null, 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 path = self.terminal.session.toAbsolutePath(args[1]);
self.terminal.fileAPI.load(self.viCallback, [ path ], path);
};
this.command_upload = function(args) {
var uploader = new Uploader(self.terminal);
self.terminal.ui.append(uploader.node);
};
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);
};