LEVIATHAN v962456e · 962456eee1

Standard Library

interface IConsole

What a program may say to the terminal.

since 0.1.0-alpha.1linuxwindowswasm

Overview

A function that takes an IConsole parameter prints through it, so a test can pass an implementation that records the output. use std::IConsole; makes the real console (SystemConsole) the default for such parameters.

Examples

class Recorder : IConsole {
    Array<string> calls = [];
    void write(string s) { calls = calls.add("write:" + s); }
    void writeln(string s) { calls = calls.add("writeln:" + s); }
    void writeln() { calls = calls.add("writeln"); }
}

void greet(IConsole out) {
    out.write("hello, ");
    out.writeln("world");
    out.writeln();
}

Recorder rec = Recorder();
bind IConsole => rec;
greet();
console.writeln(rec.calls.joinToString(";"));
write:hello, ;writeln:world;writeln

Methods

write

write(string s) -> void

Print a string without a trailing newline.

Parameters

s
The text to print.

Examples

class Counting : IConsole {
    int chars = 0;
    void write(string s) { chars = chars + s.length(); }
    void writeln(string s) { chars = chars + s.length(); }
    void writeln() { }
}
Counting c = Counting();
IConsole out = c;
out.write("abc");
out.write("de");
console.writeln(c.chars);
5

writeln

writeln(string s) -> void

Print a string followed by a newline.

Parameters

s
The text to print.

Examples

class Counting : IConsole {
    int lines = 0;
    void write(string s) { }
    void writeln(string s) { lines = lines + 1; }
    void writeln() { lines = lines + 1; }
}
Counting c = Counting();
IConsole out = c;
out.writeln("first");
out.writeln("second");
console.writeln(c.lines);
2
writeln() -> void

Print a bare newline.

See also

  • SystemConsole — The real console behind the IConsole interface: it prints to standard output.
  • Console — The program's standard output as an object.