aboutsummaryrefslogtreecommitdiff
path: root/mirai.old/boot/sequence/message.zig
blob: d6ca08e045760074e777edfd51e667fa1606daae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Boot message buffering

const boot_limits = @import("../../common/limits/boot.zig");
const colors = @import("../../common/constants/colors.zig");
const serial = @import("../../drivers/serial/serial.zig");

var terminal_print_fn: ?*const fn ([]const u8) void = null;
var terminal_print_color_fn: ?*const fn ([]const u8, u32) void = null;

const Message = struct {
    text: [boot_limits.MAX_MESSAGE_LENGTH]u8,
    len: usize,
    color: u32,
    is_colored: bool,
};

var buffer: [boot_limits.MAX_BOOT_MESSAGES]Message = undefined;
var count: usize = 0;
var ready: bool = false;

pub fn set_terminal(
    print_fn: *const fn ([]const u8) void,
    print_color_fn: *const fn ([]const u8, u32) void,
) void {
    terminal_print_fn = print_fn;
    terminal_print_color_fn = print_color_fn;
    ready = true;
    replay();
}

pub fn is_ready() bool {
    return ready;
}

pub fn print(text: []const u8) void {
    write(text, colors.WHITE, false);
}

pub fn print_color(text: []const u8, color: u32) void {
    write(text, color, true);
}

fn write(text: []const u8, color: u32, is_colored: bool) void {
    serial.print(text);

    if (ready) {
        if (is_colored) {
            if (terminal_print_color_fn) |f| f(text, color);
        } else {
            if (terminal_print_fn) |f| f(text);
        }
    } else if (count < buffer.len) {
        var msg = &buffer[count];
        const len = @min(text.len, boot_limits.MAX_MESSAGE_LENGTH - 1);
        for (text[0..len], 0..) |c, i| {
            msg.text[i] = c;
        }
        msg.len = len;
        msg.color = color;
        msg.is_colored = is_colored;
        count += 1;
    }
}

fn replay() void {
    for (buffer[0..count]) |msg| {
        if (msg.is_colored) {
            if (terminal_print_color_fn) |f| f(msg.text[0..msg.len], msg.color);
        } else {
            if (terminal_print_fn) |f| f(msg.text[0..msg.len]);
        }
    }
    count = 0;
}