Print & Formatted print

Printing is done by streaming text to the Console. There are different methods on the Tty Class to do so:

  • Tty.WriteLn(str): Writes a string and appends a newline character
  • Tty.Write(str): Writes a string and doesn't append a newline character (stdout).
  • Tty.WriteErrorLn(str): same but to stderr
  • Tty.WriteError(str): you already know what this does

You can add color to your strings by using the utils in the Jane.Text.ANSIStyling class:

file use -s Jane.Text.ANSIStyling; // Makes override and extension methods available in the file
// -s means it's a static member, ANSIStyling is a class that provides static methods

let x: i32 = 4;

Tty.WriteLn("${x} + 4 = ${x + 4}".ANSIRed().ANSIItalic()); // This prints red and italic text to the console!

Tty.WriteLn(ANSIBlue("This is blue")); // This works too

Tty.WriteLn("This is green, bold, italic and has a white background"::(ANSIRed ANSIItalic ANSIBold ANSIBGWhite));
// Using the turbopufferfish chainer you can call multiple methods with the same return and input types a bit prettier imo and its like 4 characters shorter idk

Strings can be formatted by default, if you don't want that, use the raw"" string literal, or escape the dollar sign with a backslash

// Curly braces include evaluatable code, whose value will be converted to string if necessary
let day_number: i32 = 31;
Tty.WriteLn("${day_number} days");

// Using the same argument twice can be accomplished by using double brackets:
Tty.WriteLn("${"Alice"}, this is ${"Bob"}. ${{1}}, this is ${{0}}");

// And you can specify them after too:
Tty.WriteLn("${subject} ${verb} ${object}"{
    subject "the quick brown fox"
    verb "jumps over"
    object "the lazy dog"
});

// Other than strings, numbers for example can be formatted too using a format string or just the radix:
Tty.WriteLn("Base 10: ${69420}"); // 69420
Tty.WriteLn("Base 2 (binary): ${69420.f(2)}); // 10000111100101100
Tty.WriteLn("Base 8 (octal): ${69420.f(8)}"); // 207454
Tty.WriteLn("Base 16 (hexadecimal): ${69420.f(16)}"); // 10f2c, standard is lowercase
Tty.WriteLn("Base 16 (hexadecimal, uppercase): ${69420.f(16).upper()}"); // 10F2C, if you need your hexadecimals uppercase use the inbuilt `str.upper()` function

Tty.WriteLn("Pad Start, clip to 2 decimal places: ${Math.Pi.f("000.00")}")

format string follows this pattern

See also