Skip to content

Commit

Permalink
Use 'emit --info', 'emit --verbose', ...
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Oct 1, 2024
1 parent 7640384 commit c7d60ce
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 67 deletions.
14 changes: 7 additions & 7 deletions examples/ui.toit
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import cli show *

some-chatty-method cli/Cli:
ui := cli.ui
ui.debug "This is a debug message."
ui.verbose "This is a verbose message."
ui.inform "This is an information message."
ui.warn "This is a warning message."
ui.error "This is an error message."
ui.interactive "This is an interactive message."
ui.emit --debug "This is a debug message."
ui.emit --verbose "This is a verbose message."
ui.emit --info "This is an information message."
ui.emit --warning "This is a warning message."
ui.emit --error "This is an error message."
ui.emit --interactive "This is an interactive message."
// By convention, 'result' calls should only happen in the method that
// initially received the Invocation object.
// For demonstration purposes, we call it here.
ui.result "This is a result message."
ui.emit --result "This is a result message."

emit-structured cli/Cli:
ui := cli.ui
Expand Down
2 changes: 1 addition & 1 deletion src/parser_.toit
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Parser_:
// Otherwise, ignore the ui that was determined through the command line and
// print the usage on stderr, followed by an exit 1.
ui := test-ui_ or (Ui --level=Ui.QUIET-LEVEL --printer=StderrPrinter_)
ui.error str
ui.emit --error str
help-command_ path [] --invoked-command=invoked-command_ --ui=ui
ui.abort
unreachable
Expand Down
231 changes: 197 additions & 34 deletions src/ui.toit
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Ui:
constructor --.level=NORMAL-LEVEL --printer/Printer:
printer_ = printer
if not DEBUG-LEVEL >= level >= SILENT-LEVEL:
error "Invalid level: $level"
throw "Invalid level: $level"

constructor.console --level/int=NORMAL-LEVEL:
return Ui --level=level --printer=ConsolePrinter
Expand All @@ -219,47 +219,50 @@ class Ui:
As such, the object must be a valid JSON object.
Otherwise, the $object is converted to a string.
*/
inform object/any:
// TODO(florian): change the bool type to 'True'.
emit --info/bool object/any:
emit --kind=INFO --structured=: object

/** Alias for $inform. */
print object/any:
inform object
/** Variant of $emit using the $DEBUG kind. */
// TODO(florian): change the bool type to 'True'.
emit --debug/bool object/any:
emit --kind=DEBUG --structured=: object

/** Variant of $inform using the $DEBUG kind. */
debug object/any:
emit --kind=DEBUG --structured=: "$object"
/** Variant of $emit using the $VERBOSE kind. */
// TODO(florian): change the bool type to 'True'.
emit --verbose/bool object/any:
emit --kind=VERBOSE --structured=: object

/** Variant of $inform using the $VERBOSE kind. */
verbose object/any:
emit --kind=VERBOSE --structured=: "$object"
/** Variant of $emit using the $WARNING kind. */
// TODO(florian): change the bool type to 'True'.
emit --warning/bool object/any:
emit --kind=WARNING --structured=: object

/** Variant of $verbose that only calls the block when necessary. */
verbose [generator]:
do_ --kind=VERBOSE generator
/** Variant of $emit using the $INTERACTIVE kind. */
// TODO(florian): change the bool type to 'True'.
emit --interactive/bool object/any:
emit --kind=INTERACTIVE --structured=: object

/** Emits the given $object at a warning-level as a string. */
warn object/any:
emit --kind=WARNING --structured=: "$object"
/** Variant of $emit using the $ERROR kind. */
// TODO(florian): change the bool type to 'True'.
emit --error/bool object/any:
emit --kind=ERROR --structured=: object

/** Emits the given $object at an interactive-level as a string. */
interactive object/any:
emit --kind=INTERACTIVE --structured=: "$object"

/** Emits the given $object at an error-level as a string. */
error object/any:
emit --kind=ERROR --structured=: "$object"
/**
Variant of $emit using the $RESULT kind.
/** Emits the given $object at a result-level as a string. */
result object/any:
emit --kind=RESULT --structured=: "$object"
A program should do only a single result output per run.
*/
// TODO(florian): change the bool type to 'True'.
emit --result/bool object/any:
emit --kind=RESULT --structured=: object

/**
Aborts the program with the given error message.
First emits $object at an error-level as as tring, then calls $abort.
*/
abort object/any:
error object
emit --error object
abort

do_ --kind/int [generator] -> none:
Expand All @@ -274,7 +277,7 @@ class Ui:
else if level == SILENT-LEVEL:
if kind < RESULT: return
else:
error "Invalid level: $level"
throw "Invalid level: $level"
generator.call

/**
Expand All @@ -290,37 +293,133 @@ class Ui:
key entries into the $table. The values are used in the header row. Printers
are *not* required to use the $header.
*/
emit-table --kind/int=RESULT table/List --title/string?=null --header/Map?=null:
emit-table --kind/int table/List --title/string?=null --header/Map?=null:
do_ --kind=kind:
if printer_.needs-structured --kind=kind:
printer_.emit-structured --kind=kind table
else:
printer_.emit-table --kind=kind --title=title --header=header table

/** Variant of $(emit-table --kind table) using the $DEBUG kind. */
emit-table --debug/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=DEBUG table --title=title --header=header

/** Variant of $(emit-table --kind table) using the $VERBOSE kind. */
emit-table --verbose/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=VERBOSE table --title=title --header=header

/** Variant of $(emit-table --kind table) using the $INFO kind. */
emit-table --info/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=INFO table --title=title --header=header

/** Variant of $(emit-table --kind table) using the $WARNING kind. */
emit-table --warning/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=WARNING table --title=title --header=header

/** Variant of $(emit-table --kind table) using the $INTERACTIVE kind. */
emit-table --interactive/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=INTERACTIVE table --title=title --header=header

/** Variant of $(emit-table --kind table) using the $ERROR kind. */
emit-table --error/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=ERROR table --title=title --header=header

/**
Variant of $(emit-table --kind table) using the $RESULT kind.
A program should do only a single result output per run.
*/
emit-table --result/bool table/List --title/string?=null --header/Map?=null:
emit-table --kind=RESULT table --title=title --header=header

/**
Emits a list.
Printers are *not* required to display the title.
*/
emit-list --kind/int=RESULT list/List --title/string?=null:
emit-list --kind/int list/List --title/string?=null:
do_ --kind=kind:
if printer_.needs-structured --kind=kind:
printer_.emit-structured --kind=kind list
else:
printer_.emit-list --kind=kind --title=title list

/** Variant of $(emit-list --kind list) using the $DEBUG kind. */
emit-list --debug/bool list/List --title/string?=null:
emit-list --kind=DEBUG list --title=title

/** Variant of $(emit-list --kind list) using the $VERBOSE kind. */
emit-list --verbose/bool list/List --title/string?=null:
emit-list --kind=VERBOSE list --title=title

/** Variant of $(emit-list --kind list) using the $INFO kind. */
emit-list --info/bool list/List --title/string?=null:
emit-list --kind=INFO list --title=title

/** Variant of $(emit-list --kind list) using the $WARNING kind. */
emit-list --warning/bool list/List --title/string?=null:
emit-list --kind=WARNING list --title=title

/** Variant of $(emit-list --kind list) using the $INTERACTIVE kind. */
emit-list --interactive/bool list/List --title/string?=null:
emit-list --kind=INTERACTIVE list --title=title

/** Variant of $(emit-list --kind list) using the $ERROR kind. */
emit-list --error/bool list/List --title/string?=null:
emit-list --kind=ERROR list --title=title

/**
Variant of $(emit-list --kind list) using the $RESULT kind.
A program should do only a single result output per run.
*/
emit-list --result/bool list/List --title/string?=null:
emit-list --kind=RESULT list --title=title

/**
Emits a map.
Printers are *not* required to display the title.
*/
emit-map --kind/int=RESULT map/Map --title/string?=null:
emit-map --kind/int map/Map --title/string?=null:
do_ --kind=kind:
if printer_.needs-structured --kind=kind:
printer_.emit-structured --kind=kind map
else:
printer_.emit-map --kind=kind --title=title map

/** Variant of $(emit-map --kind map) using the $DEBUG kind. */
emit-map --debug/bool map/Map --title/string?=null:
emit-map --kind=DEBUG map --title=title

/** Variant of $(emit-map --kind map) using the $VERBOSE kind. */
emit-map --verbose/bool map/Map --title/string?=null:
emit-map --kind=VERBOSE map --title=title

/** Variant of $(emit-map --kind map) using the $INFO kind. */
emit-map --info/bool map/Map --title/string?=null:
emit-map --kind=INFO map --title=title

/** Variant of $(emit-map --kind map) using the $WARNING kind. */
emit-map --warning/bool map/Map --title/string?=null:
emit-map --kind=WARNING map --title=title

/** Variant of $(emit-map --kind map) using the $INTERACTIVE kind. */
emit-map --interactive/bool map/Map --title/string?=null:
emit-map --kind=INTERACTIVE map --title=title

/** Variant of $(emit-map --kind map) using the $ERROR kind. */
emit-map --error/bool map/Map --title/string?=null:
emit-map --kind=ERROR map --title=title

/**
Variant of $(emit-map --kind map) using the $RESULT kind.
A program should do only a single result output per run.
*/
emit-map --result/bool map/Map --title/string?=null:
emit-map --kind=RESULT map --title=title

/**
Emits the value created by calling $structured or $text.
Expand All @@ -331,7 +430,7 @@ class Ui:
passes the result as string to the printer. The $text block may return
null to indicate that no output should be generated.
*/
emit --kind/int=RESULT [--structured] [--text]:
emit --kind/int [--structured] [--text]:
do_ --kind=kind:
if printer_.needs-structured --kind=kind:
printer_.emit-structured --kind=kind structured.call
Expand All @@ -340,19 +439,83 @@ class Ui:
if message:
printer_.emit --kind=kind "$text.call"

/** Variant of $(emit --kind [--structured] [--text]) using the $DEBUG kind. */
emit --debug/bool [--structured] [--text]:
emit --kind=DEBUG --structured=structured --text=text

/** Variant of $(emit --kind [--structured] [--text]) using the $VERBOSE kind. */
emit --verbose/bool [--structured] [--text]:
emit --kind=VERBOSE --structured=structured --text=text

/** Variant of $(emit --kind [--structured] [--text]) using the $INFO kind. */
emit --info/bool [--structured] [--text]:
emit --kind=INFO --structured=structured --text=text

/** Variant of $(emit --kind [--structured] [--text]) using the $WARNING kind. */
emit --warning/bool [--structured] [--text]:
emit --kind=WARNING --structured=structured --text=text

/** Variant of $(emit --kind [--structured] [--text]) using the $INTERACTIVE kind. */
emit --interactive/bool [--structured] [--text]:
emit --kind=INTERACTIVE --structured=structured --text=text

/** Variant of $(emit --kind [--structured] [--text]) using the $ERROR kind. */
emit --error/bool [--structured] [--text]:
emit --kind=ERROR --structured=structured --text=text

/**
Variant of $(emit --kind [--structured] [--text]) using the $RESULT kind.
A program should do only a single result output per run.
*/
emit --result/bool [--structured] [--text]:
emit --kind=RESULT --structured=structured --text=text

/**
Variant of $(emit --kind [--structured] [--text]).
If the printer needs a non-structred representation, simply converts the
result of the $structured block to a string.
*/
emit --kind/int=RESULT [--structured]:
emit --kind/int [--structured]:
do_ --kind=kind:
if printer_.needs-structured --kind=kind:
printer_.emit-structured --kind=kind structured.call
else:
printer_.emit --kind=kind "$(structured.call)"

/** Variant of $(emit --kind [--structured]) using the $DEBUG kind. */
emit --debug/bool [--structured]:
emit --kind=DEBUG --structured=structured

/** Variant of $(emit --kind [--structured]) using the $VERBOSE kind. */
emit --verbose/bool [--structured]:
emit --kind=VERBOSE --structured=structured

/** Variant of $(emit --kind [--structured]) using the $INFO kind. */
emit --info/bool [--structured]:
emit --kind=INFO --structured=structured

/** Variant of $(emit --kind [--structured]) using the $WARNING kind. */
emit --warning/bool [--structured]:
emit --kind=WARNING --structured=structured

/** Variant of $(emit --kind [--structured]) using the $INTERACTIVE kind. */
emit --interactive/bool [--structured]:
emit --kind=INTERACTIVE --structured=structured

/** Variant of $(emit --kind [--structured]) using the $ERROR kind. */
emit --error/bool [--structured]:
emit --kind=ERROR --structured=structured

/**
Variant of $(emit --kind [--structured]) using the $RESULT kind.
A program should do only a single result output per run.
*/
emit --result/bool [--structured]:
emit --kind=RESULT --structured=structured

/**
Whether the UI wants a structured representation for the given $kind.
*/
Expand Down
Loading

0 comments on commit c7d60ce

Please sign in to comment.