LeoJS - How To Print javascript Output?

33 views
Skip to first unread message

Thomas Passin

unread,
Jan 2, 2024, 12:43:46 PMJan 2
to leo-editor
This script apparently runs without error but nothing shows in the Output window:

@language javascript
console.log('this is another test');

Using print() doesn't work, of course, because there isn't a print() function.  How can we get a visible output?

Félix

unread,
Jan 2, 2024, 8:16:51 PMJan 2
to leo-editor
Hi Thomas! :)

I gather that by an 'Output window' you mean the LeoJS Log Window? (The equivalent of Leo's Log Window)
log_pane.png

Using console.log (which is indeed the equivalent of python's print function, which prints in the terminal instead of Leo's Log Window), will itself indeed print in the developer's-tools-terminal. (not recommended)

What I recommend in contrast, is the g.es function (and other 'g' methods like es_print, etc.) which does print in the Log Window. 

See (and study) this example script that gives a few pointers on how to do various useful stuff :

@language javascript
const vscode = g.app.vscode;
g.es("hahahaha");
// 'await' for doCommandByName required only if other code in script is 'awaited'.
await c.doCommandByName('insert-headline-time');

const userInput = await vscode.window.showInputBox({
    placeHolder: 'Enter something', // Placeholder text in the input box
    prompt: 'Please enter some text:', // Prompt message above the input box
});
if (userInput === undefined) {
    g.es('User canceled the input.');
} else {
    g.es('User input:', userInput);
}
try {
    const response = await fetch(apiUrl);
    g.es("about to call");
    if (!response.ok) {
        throw new Error('Network response was not ok');
    }
    const data = await response.json();
    g.es("got it!!", JSON.stringify(data));
} catch (error) {
    g.es("oh no!");
    console.error('Fetch error:', error);
}


It will insert the current time on the selected node, then pop an input box and print it back out in the log window, along with the result of an HTTP fetch call to some test API. Here is a screenshot of the result of running this script online in vscode on the web :
screen_script_web.png

Hope this helps! 

Félix

Mike Hodson

unread,
Jan 2, 2024, 8:33:47 PMJan 2
to leo-e...@googlegroups.com
I'll admit I have not scripted Leo at all, and have only used it sparingly as an editor/organizer because of oddities in autosave..

But why in all the world is a function called 'es' [I presume g means global scope] something that prints to the log window?

Why is it not named something intuitive? g.writelog ; g.print g.log something; but 'es' ? What does 'es' mean? 

Mike

Félix

unread,
Jan 2, 2024, 8:39:00 PMJan 2
to leo-editor
Lo and behold, it means emit string 

😆

Félix

Thomas Passin

unread,
Jan 2, 2024, 9:02:15 PMJan 2
to leo-editor
Ha! Should have tried that right off!.  Thanks.

jkn

unread,
Jan 3, 2024, 2:34:23 AMJan 3
to leo-editor

Yes, "es" == "emit string" is one of the arcane bits of Leo-lore, IMO. I think the docs should mention this, IIRC they do not and it must have caused some puzzlement over the years...

Edward K. Ream

unread,
Jan 3, 2024, 5:55:17 AMJan 3
to leo-e...@googlegroups.com
On Tue, Jan 2, 2024 at 7:33 PM Mike Hodson <mys...@gmail.com> wrote:

I'll admit I have not scripted Leo at all, and have only used it sparingly as an editor/organizer because of oddities in autosave..

But why in all the world is a function called 'es' [I presume g means global scope] something that prints to the log window?

Because g.es gets called a lot, and "es" is a prefix for other functions such as g.es_exception.

It's one of the few names that Leonistas should just remember, along with c, g, p, g.app, etc.

The principle is, the more common the usage, the shorter the name. Think e = mc**2.

Edward

P.S. g.es outputs a string (actually, all its arguments) to the log.

g.os would be confusing, because os typically means operating system. We're stuck with g.es because renaming it would break existing scripts. The origins of the name are lost in the mists of Leo's history.

If you *really* don't like, g.es, you are free to create a plugin that aliases g.es to g.output_to_log_pane :-)

Edward

Edward K. Ream

unread,
Jan 3, 2024, 6:08:07 AMJan 3
to leo-editor
On Wednesday, January 3, 2024 at 4:55:17 AM UTC-6 Edward K. Ream wrote:
On Tue, Jan 2, 2024 at 7:33 PM Mike Hodson wrote:

But why in all the world is a function called 'es'...

Because g.es gets called a lot, and "es" is a prefix for other functions such as g.es_exception.

g.es is a workhorse, but leoGlobals.py contains many other useful functions (and a few classes) with longer names.

g.printObj(obj) pretty prints objects. I use it all the time for debugging. It uses pprint (Python's pretty printer) only as a fallback.

g.objToString(obj) creates a string, so g.es(g.objToString(obj)) will pretty print obj to Leo's log.

Edward
Reply all
Reply to author
Forward
0 new messages