6.58. kink/io/OUTPUT_PRINTER

Provides a `printer` implementation which wraps `output`.

6.58.1. OUTPUT_PRINTER.new(Output Charset Newline ...[$config={}])

`new` makes a val which supports both `printer` and `output` types, which encodes strings using `Charset`, and writes bytes to `Output`.

Config methods:

• C.flush_on_print

• C.replace_conversion_error

• C.synchronize

If C.flush_on_print is called, the buffer of `Output` is flushed every time after `print` or `print_line` is called.

print_xxx methods converts strings into bytes using `Charset`. When a string cannot be encoded, by default, print_xxx methods tail-call $error with an `exception`. If C.replace_conversion_error is called, print_xxx methods write replacement byte sequence at the place of the conversion error.

If C.synchronize is called, access to `Output`, and conversion from str to bin, are serialized by a single mutex. The option is useful when parallel write/print operations are not avoidable, like in the case of stderr.

Preconditions

`Output` must be an `output`.

`Charset` must be a `charset`.

`Newline` must be a `str`.

Example

:BIN_OUTPUT.require_from('kink/io/')
:OUTPUT_PRINTER.require_from('kink/io/')
:CHARSET.require_from('kink/')

:Out <- BIN_OUTPUT.new
:P <- OUTPUT_PRINTER.new(Out CHARSET.utf8 "\r\n"){(:W)
  W.flush_on_print
}
P.print_line('foo')
stdout.print_line(Out.bin.repr) # => (bin 0x66 0x6f 0x6f 0x0d 0x0a)