about summary refs log tree commit diff
path: root/src/main.zig
blob: 15df5c994d2a1b73eb205deae0e0b565165c1780 (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
75
76
77
78
79
80
const std = @import("std");
const ls = @import("ls.zig");
const socket_server = @import("socket-server.zig");
const sdl_window = @import("sdl-window.zig");
const brr = @import("brr.zig");

const commands = [_]struct {
    name: []const u8,
    command: Command,
}{
    .{ .name = "ls", .command = Command.LS },
    .{ .name = "tcp", .command = Command.TCP },
    .{ .name = "window", .command = Command.WINDOW },
    .{ .name = "brr", .command = Command.BRR },
};

const Command = enum {
    LS,
    TCP,
    WINDOW,
    BRR,
};

const Arg = struct {
    const Self = @This();

    name: []const u8,
    command: Command = undefined,

    fn parse(self: *Self) !void {
        inline for (commands) |cmd| {
            if (std.mem.eql(u8, cmd.name, self.name)) {
                self.command = cmd.command;
            }
        }
    }
};

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);

    if (args.len == 1) {
        std.debug.print("No command provided. Use 'help' for usage information.\n", .{});
        return;
    }

    if (args.len == 3) {
        if (std.mem.eql(u8, args[1], "brr")) {
            try brr.brr(allocator, args[2]);
            return;
        }
    }

    if (args.len == 2) {
        var argument = Arg{
            .name = args[1],
        };
        try argument.parse();
        switch (argument.command) {
            .LS => try ls.ls(),
            .TCP => try socket_server.start_server(),
            .WINDOW => try sdl_window.present_sdl_window(),
            else => {
                return;
            },
        }
    }

    std.debug.print("Unknown command. Use 'help' for usage information.\n", .{});
    print_help();
}

fn print_help() void {
    std.debug.print("[usage] tinkerbunk ls\n", .{});
}