Skip to content

Commit

Permalink
util: Console JS implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
briansfrank committed Jun 18, 2024
1 parent e51f631 commit de16d4e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 7 deletions.
61 changes: 61 additions & 0 deletions src/util/es/Console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright (c) 2024, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 18 Jun 24 Brian Frank Creation
//

/**
* Console
*/
class Console extends sys.Obj {

static cur() { return Console.#cur; }

static #cur = new Console();

constructor() { super(); }

typeof() { return Console.type$; }

width() { return null; }

height() { return null; }

debug(msg) { console.debug(msg); return this; }

info(msg) { console.info(msg); return this; }

warn(msg) { console.warn(msg); return this; }

err(msg) { console.error(msg); return this; }

table(obj) { console.table(obj); return this; }

group(msg, collapsed)
{
if (!collapsed)
console.group(msg)
else
console.groupCollapsed(msg);
return this;
}

groupEnd()
{
console.groupEnd();
return this;
}

prompt(msg)
{
return null; // unsupported
}

promptPassword(msg)
{
return null; // unsupported
}
}

2 changes: 1 addition & 1 deletion src/util/fan/Console.fan
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
** if installed. In browser JavaScript environments this APIs uses
** the JS debugging window.
**
//@Js
@Js
native const final class Console
{
** Get the default console for the virtual machine
Expand Down
59 changes: 59 additions & 0 deletions src/util/js/Console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Copyright (c) 2023, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 18 Jun 24 Brian Frank Creation
//

/**
* Console
*/
fan.util.Console = fan.sys.Obj.$extend(fan.sys.Obj);

fan.util.Console.prototype.$ctor = function() {}
fan.util.Console.prototype.$typeof = function() { return fan.util.Console.$type; }

fan.util.Console.cur = function() { return fan.util.Console.$cur; }

fan.util.Console.$cur = new fan.util.Console();

fan.util.Console.prototype.debug = function(msg) { console.debug(msg); return this; }

fan.util.Console.prototype.info = function(msg) { console.info(msg); return this; }

fan.util.Console.prototype.warn = function(msg) { console.warn(msg); return this; }

fan.util.Console.prototype.err = function(msg) { console.error(msg); return this; }

fan.util.Console.prototype.width = function() { return null; }

fan.util.Console.prototype.height = function() { return null; }

fan.util.Console.prototype.table = function(obj) { console.table(obj); return this; }

fan.util.Console.prototype.group = function(msg, collapsed)
{
if (!collapsed)
console.group(msg)
else
console.groupCollapsed(msg);
return this;
}

fan.util.Console.prototype.groupEnd = function()
{
console.groupEnd();
return this;
}

fan.util.Console.prototype.prompt = function(msg)
{
return null; // unsupported
}

fan.util.Console.prototype.promptPassword = function(msg)
{
return null; // unsupported
}

19 changes: 13 additions & 6 deletions src/util/test/ConsoleTest.fan
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
**
** ConsoleTest isn't an actual test, just a program to run to see results
**
//@Js
@Js
class ConsoleTest : Test
{
Void main()
static Void main()
{
c := Console.cur
c.info("Console $c [$c.typeof]")
c.info("width = $c.width")
c.info("height = $c.height")
echo("Console $c [$c.typeof]")
echo("width = $c.width")
echo("height = $c.height")

// basic logging
c.debug("Debug message!")
Expand All @@ -28,7 +28,7 @@ class ConsoleTest : Test
// tables
c.table("scalar")

// indent/unindent
// group
c.group("indent 0")
c.info("line 1")
c.warn("line 2")
Expand All @@ -40,6 +40,13 @@ class ConsoleTest : Test
c.groupEnd
c.info("line 5 back to zero")

// group collapsed
c.group("Collapsed", true)
c.info("alpha")
c.info("beta")
c.info("gamma")
c.groupEnd

// prompt
c.info("Prompt 1>")
x := c.prompt
Expand Down

0 comments on commit de16d4e

Please sign in to comment.