Skip to content

Commit

Permalink
Expose whether a human output is desired.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Oct 2, 2024
1 parent 2de9bd0 commit 58c3695
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/ui.toit
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,23 @@ interface Printer:
/** Emits the given $json-object of the given message-$kind. */
emit-structured --kind/int json-object/any

/** Whether the printer wants a human representation for the given $kind. */
wants-human --kind/int -> bool

/**
A printer that prints human-readable output.
*/
abstract class HumanPrinterBase implements Printer:

abstract needs-structured --kind/int -> bool
abstract emit-structured --kind/int object/any

abstract print_ str/string

needs-structured --kind/int -> bool:
return false

wants-human --kind/int -> bool:
return true

emit --kind/int msg/string:
prefix := ""
if kind == Ui.ERROR:
Expand Down Expand Up @@ -190,11 +197,15 @@ Typically, this printer is used in shell scripts or other non-interactive
*/
abstract class PlainPrinterBase implements Printer:

abstract needs-structured --kind/int -> bool
abstract emit-structured --kind/int object/any

abstract print_ str/string

needs-structured --kind/int -> bool:
return false

wants-human --kind/int -> bool:
return false

emit --kind/int msg/string:
print_ msg

Expand Down Expand Up @@ -673,6 +684,12 @@ class Ui:
wants-structured --kind/int=RESULT -> bool:
return printer_.needs-structured --kind=kind

/**
Whether the UI wants a human representation for the given $kind.
*/
wants-human --kind/int=RESULT -> bool:
return printer_.wants-human --kind=kind

/**
Aborts the program with the given error message.
Expand All @@ -698,25 +715,22 @@ class Ui:
--printer=printer or this.printer_

class HumanPrinter extends HumanPrinterBase:
needs-structured --kind/int -> bool: return false

print_ str/string:
print str

emit-structured --kind/int object/any:
unreachable

class PlainPrinter extends PlainPrinterBase:
needs-structured --kind/int -> bool: return false

print_ str/string:
print str

emit-structured --kind/int object/any:
unreachable

class JsonPrinter extends PlainPrinterBase:
needs-structured --kind/int -> bool: return kind == Ui.RESULT
class JsonPrinter extends HumanPrinterBase:
needs-structured --kind/int -> bool:
return kind == Ui.RESULT

print_ str/string:
print-on-stderr_ str
Expand Down
6 changes: 3 additions & 3 deletions tests/ui-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ test-json:
ui := Ui --printer=printer

// Anything that isn't a result is emitted on stderr as if it was
// a console Ui.
// a human Ui.
ui.emit --info "hello"
expect-equals "hello\n" printer.stderr
printer.reset
Expand All @@ -364,9 +364,9 @@ test-json:
expect-equals "{\"foo\":1,\"bar\":2}" printer.stdout

ui.emit --warning "some warning"
expect-equals "some warning\n" printer.stderr
expect-equals "Warning: some warning\n" printer.stderr
printer.reset

ui.emit --error "some error"
expect-equals "some error\n" printer.stderr
expect-equals "Error: some error\n" printer.stderr
printer.reset

0 comments on commit 58c3695

Please sign in to comment.