-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.zig
96 lines (79 loc) · 3.28 KB
/
main.zig
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const std = @import("std");
const testing = std.testing;
const c = @import("c/c.zig");
const windows = c.windows;
const utils = @import("utils.zig");
pub const types = @import("types.zig");
pub const Event = @import("events.zig").Event;
pub fn getCodepage() c_uint {
return c.GetConsoleOutputCP();
}
pub fn setCodepage(codepage: c_uint) !void {
if (c.SetConsoleOutputCP(codepage) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
}
pub const ConsoleApp = struct {
const Self = @This();
stdin_handle: windows.HANDLE,
stdout_handle: windows.HANDLE,
pub fn init() !Self {
return Self{ .stdin_handle = try windows.GetStdHandle(windows.STD_INPUT_HANDLE), .stdout_handle = try windows.GetStdHandle(windows.STD_OUTPUT_HANDLE) };
}
pub fn getInputMode(self: Self) !types.InputMode {
var mode: windows.DWORD = undefined;
if (c.GetConsoleMode(self.stdin_handle, &mode) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
return utils.fromUnsigned(types.InputMode, mode);
}
pub fn setInputMode(self: Self, mode: types.InputMode) !void {
if (c.SetConsoleMode(self.stdin_handle, utils.toUnsigned(types.InputMode, mode)) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
}
pub fn getOutputMode(self: Self) !types.OutputMode {
var mode: windows.DWORD = undefined;
if (c.GetConsoleMode(self.stdout_handle, &mode) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
return utils.fromUnsigned(types.OutputMode, mode);
}
pub fn setOutputMode(self: Self, mode: types.OutputMode) !void {
if (c.SetConsoleMode(self.stdout_handle, utils.toUnsigned(types.OutputMode, mode)) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
}
pub fn getEvent(self: Self) !Event {
var event_count: u32 = 0;
var input_record = std.mem.zeroes(c.INPUT_RECORD);
if (c.ReadConsoleInputW(self.stdin_handle, &input_record, 1, &event_count) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
return Event.fromInputRecord(input_record);
}
pub fn viewportCoords(self: Self, coords: types.Coords, viewport_rect: ?types.Rect) !types.Coords {
return types.Coords{ .x = coords.x, .y = coords.y - (viewport_rect orelse (try self.getScreenBufferInfo()).viewport_rect).top };
}
pub fn getScreenBufferInfo(self: Self) !types.ScreenBufferInfo {
var bf = std.mem.zeroes(windows.CONSOLE_SCREEN_BUFFER_INFO);
if (windows.kernel32.GetConsoleScreenBufferInfo(self.stdout_handle, &bf) == 0) {
switch (windows.kernel32.GetLastError()) {
else => |err| return windows.unexpectedError(err),
}
}
return @bitCast(types.ScreenBufferInfo, bf);
}
};