1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-09 08:15:46 +01:00

Much more advanced :echo[err] methods which can print objects/etc. through eval()

This commit is contained in:
Martin Stubenschrott
2007-09-27 00:35:57 +00:00
parent c2fa37e4b3
commit 8e2775530d
3 changed files with 110 additions and 24 deletions

View File

@@ -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(/</g, "&lt;").replace(/>/, "&gt;");
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.<br/>Multiple lines can be separated by \\n."
help: "Useful for showing informational messages.Multiple lines can be separated by \\n.<br/>" +
"<code class=\"argument\"}{arg}</code> 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 <code class=\"argument\">{arg}</code> 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("<pre>" + output + "</pre>");
vimperator.echo(output);
else
vimperator.echoerr("Invalid system command: " + args);
},

View File

@@ -185,9 +185,7 @@ function CommandLine() //{{{
multiline_input_widget.collapsed = true;
var output = str.replace(/\n|\\n/g, "<br/>");
//output = ":" + command_widget.value + "<br/>" + output;
output += "<br/>"
var output = "<pre>" + str + "</pre>";
output += '<span id="end-prompt" style="color: green; background-color: white;">Press ENTER or type command to continue</span>';
output += '<span id="more-prompt" style="display: none; position: fixed; top: auto; bottom: 0; left: 0; right: 0; color: green; background-color: white;">';
output += "-- More --";
@@ -289,7 +287,7 @@ function CommandLine() //{{{
str = "";
setNormalStyle();
if (flags || str.indexOf("\n") > -1 || str.indexOf("\\n") > -1 || str.indexOf("<br>") > -1 || str.indexOf("<br/>") > -1)
if (flags || str.indexOf("\n") > -1 || str.indexOf("<br>") > -1 || str.indexOf("<br/>") > -1)
{
setMultiline(str);
}

View File

@@ -346,39 +346,89 @@ const vimperator = (function() //{{{
}
},
// logs a message to the javascript error console
log: function(msg, level)
// if color = true it uses HTML markup to color certain items
objectToString: function(object, color)
{
// if (Options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst
console_service.logStringMessage('vimperator: ' + msg);
},
// log an object to the javascript error console and print all
// properties of the object
logObject: function(object, level)
{
if (typeof object != 'object')
if (object === null)
return "null";
if (typeof object != "object")
return false;
var string = object + '::\n';
var string = "";
var obj = "";
try { // for window.JSON
obj = object.toString();
} catch (e) {
obj = "&lt;Object&gt;";
}
if (color)
string += "<span style=\"color: magenta; font-weight: bold;\">" + obj + "</span>::\n";
else
string += obj + "::\n";
for (var i in object)
{
var value;
try
{
var value = object[i];
if (i == "JSON") // without this ugly hack, ":echo window" does not work
value = "[object JSON]";
else
value = object[i];
}
catch (e)
{
value = "";
}
string += i + ": " + value + "\n";
if (color)
{
// syntax highlighting for special items
if (typeof value === "number")
value = "<span style=\"color: red;\">" + value + "</span>";
else if (typeof value === "string")
{
value = value.replace(/\n/, "\\n").replace(/</, "&lt;");
value = "<span style=\"color: green;\">\"" + value + "\"</span>";
}
else if (typeof value === "boolean")
value = "<span style=\"color: blue;\">" + value + "</span>";
else if (value == null || value == "undefined")
value = "<span style=\"color: blue;\">" + value + "</span>";
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, "&lt;").replace(/>/g, "&gt;");
}
}
string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + 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