Skip to content

Commit

Permalink
util: rework Console to use NativeConsole subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
briansfrank committed Jun 29, 2024
1 parent 3a8be7a commit 46f3618
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 105 deletions.
10 changes: 5 additions & 5 deletions src/util/es/Console.js → src/util/es/NativeConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
//

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

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

static #cur = new Console();
static #curNative = new NativeConsole();

constructor() { super(); }

typeof() { return Console.type$; }
typeof() { return NativeConsole.type$; }

width() { return null; }

Expand Down
89 changes: 75 additions & 14 deletions src/util/fan/Console.fan
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,115 @@
** the JS debugging window.
**
@Js
native const final class Console
abstract const class Console
{
** Get the default console for the virtual machine
static Console cur()
static Console cur() { NativeConsole.curNative }

** Construct a console that wraps an output stream
static Console wrap(OutStream out) { throw Err("TODO") }

** Number of chars that fit horizontally in console or null if unknown
abstract Int? width()

** Number of lines that fit vertically in console or null if unknown
abstract Int? height()

** Print a message to the console at the debug level
abstract This debug(Obj? msg)

** Print a message to the console at the informational level
abstract This info(Obj? msg)

** Print a message to the console at the warning level
abstract This warn(Obj? msg)

** Print a message to the console at the error level
abstract This err(Obj? msg)

** Print tabular data to the console:
** - List of list is two dimensional data where first row is header names
** - List of items with an each method will create column per key
** - List of items without each will map to a column of "val"
** - Anything else will be table of one cell table
abstract This table(Obj? obj)

** Clear the console of all text if supported
abstract This clear()

** Enter an indented group level in the console. The JS debug
** window can specify the group to default in a collapsed state (this
** flag is ignored in a standard terminal).
abstract This group(Obj? msg, Bool collapsed := false)

** Exit an indented, collapsable group level
abstract This groupEnd()

** Prompt the user to enter a string from standard input.
** Return null if end of stream has been reached.
abstract Str? prompt(Str msg := "")

** Prompt the user to enter a password string from standard input
** with echo disabled. Return null if end of stream has been reached.
abstract Str? promptPassword(Str msg := "")
}

**************************************************************************
** NativeConsole
**************************************************************************

**
** NativeConsole binds to VM console facilities
**
@NoDoc @Js
native const class NativeConsole : Console
{
** Get the default console for the virtual machine
static NativeConsole curNative()

** Number of chars that fit horizontally in console or null if unknown
Int? width()
override Int? width()

** Number of lines that fit vertically in console or null if unknown
Int? height()
override Int? height()

** Print a message to the console at the debug level
This debug(Obj? msg)
override This debug(Obj? msg)

** Print a message to the console at the informational level
This info(Obj? msg)
override This info(Obj? msg)

** Print a message to the console at the warning level
This warn(Obj? msg)
override This warn(Obj? msg)

** Print a message to the console at the error level
This err(Obj? msg)
override This err(Obj? msg)

** Print tabular data to the console:
** - List of list is two dimensional data where first row is header names
** - List of items with an each method will create column per key
** - List of items without each will map to a column of "val"
** - Anything else will be table of one cell table
This table(Obj? obj)
override This table(Obj? obj)

** Clear the console of all text if supported
This clear()
override This clear()

** Enter an indented group level in the console. The JS debug
** window can specify the group to default in a collapsed state (this
** flag is ignored in a standard terminal).
This group(Obj? msg, Bool collapsed := false)
override This group(Obj? msg, Bool collapsed := false)

** Exit an indented, collapsable group level
This groupEnd()
override This groupEnd()

** Prompt the user to enter a string from standard input.
** Return null if end of stream has been reached.
Str? prompt(Str msg := "")
override Str? prompt(Str msg := "")

** Prompt the user to enter a password string from standard input
** with echo disabled. Return null if end of stream has been reached.
Str? promptPassword(Str msg := "")
override Str? promptPassword(Str msg := "")
}

**************************************************************************
Expand Down
18 changes: 9 additions & 9 deletions src/util/java/Console.java → src/util/java/NativeConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
import java.lang.reflect.Method;
import fan.sys.*;

public abstract class Console extends FanObj
public abstract class NativeConsole extends Console
{

//////////////////////////////////////////////////////////////////////////
// Factory
//////////////////////////////////////////////////////////////////////////

public static Console cur()
public static NativeConsole curNative()
{
if (cur == null) cur = create();
return cur;
}
private static Console cur;
private static NativeConsole cur;

/*
* To test jline2:
* java -Dfan.home=/work/fan -cp /work/fan/lib/java/sys.jar:/work/stuff/jline/jline-2.9.jar fanx.tools.Fan util::ConsoleTest
*/

private static Console create()
private static NativeConsole create()
{
try { return new Jline3Console(); } catch (Exception e) {}
try { return new Jline2Console(); } catch (Exception e) {}
Expand Down Expand Up @@ -122,7 +122,7 @@ public Console clear()

public Type typeof()
{
if (type == null) type = Type.find("util::Console");
if (type == null) type = Type.find("util::NativeConsole");
return type;
}
private static Type type;
Expand All @@ -141,7 +141,7 @@ private PrintStream out()
// StdinConsole
//////////////////////////////////////////////////////////////////////////

static class StdinConsole extends Console
static class StdinConsole extends NativeConsole
{
public String prompt(String msg)
{
Expand All @@ -167,7 +167,7 @@ public String promptPassword(String msg)
// JavaConsole
//////////////////////////////////////////////////////////////////////////

static class JavaConsole extends Console
static class JavaConsole extends NativeConsole
{
JavaConsole(java.io.Console c) { this.console = c; }

Expand All @@ -188,7 +188,7 @@ public String promptPassword(String msg)
// Jline3Console
//////////////////////////////////////////////////////////////////////////

static class Jline3Console extends Console
static class Jline3Console extends NativeConsole
{
Jline3Console() throws Exception
{
Expand Down Expand Up @@ -277,7 +277,7 @@ public String promptPassword(String msg)
// Jline3Console
//////////////////////////////////////////////////////////////////////////

static class Jline2Console extends Console
static class Jline2Console extends NativeConsole
{
Jline2Console() throws Exception
{
Expand Down
77 changes: 0 additions & 77 deletions src/util/js/Console.js

This file was deleted.

77 changes: 77 additions & 0 deletions src/util/js/NativeConsole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// 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.NativeConsole = fan.sys.Obj.$extend(fan.util.Console);

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

fan.util.NativeConsole.curNative = function() { return fan.util.NativeConsole.$curNative; }

fan.util.NativeConsole.$curNative = new fan.util.NativeConsole();

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

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

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

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

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

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

fan.util.NativeConsole.prototype.table = function(obj)
{
var grid = []
var t = fan.util.ConsoleTable.make(obj);
for (var r=0; r<t.rows().size(); ++r)
{
var row = t.rows().get(r);
var obj = {};
for (var c=0; c<t.headers().size(); ++c)
{
var key = t.headers().get(c);
var val = row.get(c);
obj[key] = val;
}
grid.push(obj);
}
console.table(grid)
return this;
}

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

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

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

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

0 comments on commit 46f3618

Please sign in to comment.