From 8e2775530d328b37437675155523c5bc448878f5 Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Thu, 27 Sep 2007 00:35:57 +0000 Subject: [PATCH] Much more advanced :echo[err] methods which can print objects/etc. through eval() --- chrome/content/vimperator/commands.js | 48 +++++++++++++-- chrome/content/vimperator/ui.js | 6 +- chrome/content/vimperator/vimperator.js | 80 ++++++++++++++++++++----- 3 files changed, 110 insertions(+), 24 deletions(-) diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index c449ef33..404b30a8 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -483,15 +483,53 @@ function Commands() //{{{ "Here, downloads can be paused, canceled and resumed." } )); - addDefaultCommand(new Command(["ec[ho]"], - function(args) { vimperator.echo(args); } , + + function argToString(arg, color) + { + try { + // TODO: move to vimperator.eval()? + arg = eval(arg); + } + catch (e) + { + vimperator.echoerr(e.toString()); + return null; + } + + if (typeof arg === "object") + arg = vimperator.objectToString(arg, color); + else if (typeof arg === "function") + arg = arg.toString().replace(//, ">"); + else if (typeof arg === "number" || typeof arg === "boolean") + arg = "" + arg; + else if (typeof arg === "undefined") + arg = "undefined"; + + return arg; + } + addDefaultCommand(new Command(["ec[ho]"], + function(args) + { + var res = argToString(args, true); + if (res != null) + vimperator.echo(res); + }, + { + usage: ["ec[ho] {arg}"], short_help: "Display a string at the bottom of the window", - help: "Echo all arguments of this command. Useful for showing informational messages.
Multiple lines can be separated by \\n." + help: "Useful for showing informational messages.Multiple lines can be separated by \\n.
" + + " can either be a quoted string, or any expression which can be fed to eval() like 4+5. " + + "You can also view the source code of objects and functions if the return value of {arg} is an object or function." } )); addDefaultCommand(new Command(["echoe[rr]"], - function(args) { vimperator.echoerr(args); } , + function(args) + { + var res = argToString(args, false); + if (res != null) + vimperator.echoerr(res); + }, { short_help: "Display an error string at the bottom of the window", help: "Echo all arguments of this command highlighted in red. Useful for showing important messages." @@ -1565,7 +1603,7 @@ function Commands() //{{{ // TODO: if special, run the last command var output = vimperator.system(args) if (typeof output === "string") - vimperator.echo("
" + output + "
"); + vimperator.echo(output); else vimperator.echoerr("Invalid system command: " + args); }, diff --git a/chrome/content/vimperator/ui.js b/chrome/content/vimperator/ui.js index 2f314915..9e9039af 100644 --- a/chrome/content/vimperator/ui.js +++ b/chrome/content/vimperator/ui.js @@ -185,9 +185,7 @@ function CommandLine() //{{{ multiline_input_widget.collapsed = true; - var output = str.replace(/\n|\\n/g, "
"); - //output = ":" + command_widget.value + "
" + output; - output += "
" + var output = "
" + str + "
"; output += 'Press ENTER or type command to continue'; output += '"; + } + else if (typeof value === "boolean") + value = "" + value + ""; + else if (value == null || value == "undefined") + value = "" + value + ""; + else if (typeof value === "object" || typeof value === "function") + { + // for java packages value.toString() would crash so badly + // that we cannot even try/catch it + if (/^\[JavaPackage.*\]$/.test(value)) + value = "[JavaPackage]"; + else + { + var str = value.toString(); + if (typeof str == "string") // can be "undefined" + value = str.replace(//g, ">"); + } + } + + string += "" + i + ": " + value + "\n"; + } + else + string += i + ": " + value + "\n"; } - vimperator.log(string, level); return string; }, + // logs a message to the javascript error console + // if msg is an object, it is beautified + log: function(msg, level) + { + //if (Options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst + if (typeof msg == "object") + msg = this.objectToString(msg, false); + + console_service.logStringMessage('vimperator: ' + msg); + }, + // open one or more URLs // // @param urls: either a string or an array of urls