typetui 0.1.0

A terminal-based typing test.
Documentation
const std = @import("std");

const ArraySize = comptime blk: {
    var size: usize = 10;
    size *= 2;
    break :blk size;
};

fn addComptime(a: comptime_int, b: comptime_int) comptime_int {
    return a + b;
}

const Result = addComptime(5, 3);

fn max(comptime T: type, a: T, b: T) T {
    return if (a > b) a else b;
}

const MyError = error{
    OutOfMemory,
    InvalidInput,
    NotFound,
};

fn mightFail(should_fail: bool) MyError!u32 {
    if (should_fail) {
        return MyError.InvalidInput;
    }
    return 42;
}

test "error handling" {
    const result = mightFail(false) catch |err| {
        std.debug.print("Error: {}\n", .{err});
        return;
    };
    std.debug.print("Result: {}\n", .{result});
}

fn findIndex(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, index| {
        if (item == target) {
            return index;
        }
    }
    return null;
}

fn getValueOrDefault(maybe: ?i32, default: i32) i32 {
    return maybe orelse default;
}

fn sumSlice(items: []const i32) i32 {
    var total: i32 = 0;
    for (items) |item| {
        total += item;
    }
    return total;
}

const greeting: []const u8 = "Hello, Zig!";

fn findNullByte(str: [:0]const u8) ?usize {
    for (str, 0..) |byte, i| {
        if (byte == 0) return i;
    }
    return null;
}

const Color = enum {
    red,
    green,
    blue,
};

fn colorName(c: Color) []const u8 {
    return switch (c) {
        .red => "Red",
        .green => "Green",
        .blue => "Blue",
    };
}

fn categorizeScore(score: u8) []const u8 {
    return switch (score) {
        90...100 => "Excellent",
        80...89 => "Good",
        70...79 => "Average",
        60...69 => "Pass",
        else => "Fail",
    };
}

const Value = union(enum) {
    int: i32,
    float: f64,
    text: []const u8,

    fn print(self: Value) void {
        switch (self) {
            .int => |i| std.debug.print("int: {}\n", .{i}),
            .float => |f| std.debug.print("float: {}\n", .{f}),
            .text => |s| std.debug.print("text: {s}\n", .{s}),
        }
    }
};

const Point = struct {
    x: f32,
    y: f32,

    const Self = @This();

    fn new(x: f32, y: f32) Self {
        return .{ .x = x, .y = y };
    }

    fn distance(self: Self, other: Self) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }

    fn scale(self: *Self, factor: f32) void {
        self.x *= factor;
        self.y *= factor;
    }
};

const Flags = packed struct(u8) {
    a: bool = false,
    b: bool = false,
    c: bool = false,
    d: bool = false,
    _: u4 = 0,
};

pub fn main() !void {
    std.debug.print("ArraySize = {}\n", .{ArraySize});
    std.debug.print("Result = {}\n", .{Result});
    const m = max(u32, 10, 20);
    std.debug.print("max = {}\n", .{m});
    const val = mightFail(false) catch |err| {
        std.debug.print("Got error: {}\n", .{err});
        return err;
    };
    std.debug.print("Success: {}\n", .{val});
    const numbers = &[_]i32{ 10, 20, 30, 40 };
    if (findIndex(numbers, 30)) |index| {
        std.debug.print("Found at index: {}\n", .{index});
    }
    const slice = numbers[1..3];
    std.debug.print("Slice sum: {}\n", .{sumSlice(slice)});
    var v = Value{ .int = 42 };
    v.print();
    v = Value{ .text = "Hello" };
    v.print();
    var p = Point.new(3.0, 4.0);
    const q = Point.new(0.0, 0.0);
    std.debug.print("Distance: {}\n", .{p.distance(q)});
    p.scale(2.0);
    std.debug.print("Scaled: ({}, {})\n", .{ p.x, p.y });
}