SultanC
  1. Home
  2. Examples
  3. Formatting

A table with Arabic names

std.fmt measures width in user-perceived

fmt/table.sn
// A table with Arabic names: std.fmt measures width in user-perceived
// characters (grapheme clusters), so a letter with its harakat counts once
// and the columns line up. One formatter is reused for every row.
import std.fmt.fmt;
import std.io.print;

def main(): int {
    let names: vector string = ["مُحَمَّد", "فَاطِمَة", "عَلِيّ", "Sara"];
    let scores: vector f64 = [91.5, 88.25, 79.0, 95.125];
    let title: string = "Name";
    let heading: string = "Score";
    let empty: string = "";
    let line: mutable formatter = new_formatter();
    let h: result uint, fmt_error = format_into(&mutable line, "{:<10}|{:>7}\n{2:-<10}+{2:->7}\n", [fmt_arg.text(&title), fmt_arg.text(&heading), fmt_arg.text(&empty)]);
    let total: mutable f64 = 0.0;
    let i: mutable uint = 0;
    while (i < (length(names) as uint)) {
        let w: result uint, fmt_error = format_into(&mutable line, "{:<10}|{:>7.2}\n", [fmt_arg.text(&names[i]), fmt_arg.f64(scores[i])]);
        total := total + scores[i];
        i := i + 1;
    }
    let label: string = "Average";
    let f: result uint, fmt_error = format_into(&mutable line, "{:<10}|{:>7.3}\n", [fmt_arg.text(&label), fmt_arg.f64(total / 4.0)]);
    let printed: result int, int = print(take_formatter_text(&mutable line));
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/fmt/table.sn

Or compile it to an executable:

./build/sultanc examples/en/fmt/table.sn -o build/example && ./build/example

Source on GitHub   All examples