basic implementation - fin
This commit is contained in:
		@@ -57,13 +57,13 @@ pub fn readTasksFromFile(allocator: std.mem.Allocator, filename: []const u8) !Fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn writeTasksToFile(file_system: FileRead) !void {
 | 
			
		||||
    // truncate
 | 
			
		||||
    try file_system.file.setEndPos(0);
 | 
			
		||||
    // set cursor to 0
 | 
			
		||||
    try file_system.file.seekTo(0);
 | 
			
		||||
 | 
			
		||||
    for (file_system.tasks.items) |item| {
 | 
			
		||||
        std.debug.print("{s}\n", .{item});
 | 
			
		||||
        const clean_item = std.mem.trimLeft(u8, item, &[_]u8{0});
 | 
			
		||||
        try file_system.file.writeAll(clean_item);
 | 
			
		||||
        try file_system.file.writeAll(item);
 | 
			
		||||
        try file_system.file.writeAll("\n");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,31 +1,42 @@
 | 
			
		||||
const std = @import("std");
 | 
			
		||||
const stdout = std.io.getStdOut().writer();
 | 
			
		||||
 | 
			
		||||
const InteractionError = error{
 | 
			
		||||
    TaskNotFound,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]const u8)) !void {
 | 
			
		||||
    const stdout = std.io.getStdOut().writer();
 | 
			
		||||
    var failure: usize = 0;
 | 
			
		||||
    try clearTerminal();
 | 
			
		||||
 | 
			
		||||
    while (true) {
 | 
			
		||||
        try stdout.print("Please enter your choice add/remove/edit/quit: ", .{});
 | 
			
		||||
    while (failure < 3) {
 | 
			
		||||
        printItems(tasks) catch |err| {
 | 
			
		||||
            std.debug.print("Failed to print the items:\n{}\n", .{err});
 | 
			
		||||
            failure += 1;
 | 
			
		||||
            continue;
 | 
			
		||||
        };
 | 
			
		||||
        try stdout.print("Please enter your choice [add/remove/edit/quit]: ", .{});
 | 
			
		||||
        const input = getInput(allocator) catch |err| {
 | 
			
		||||
            std.debug.print("Error acquiring input:\n{}\n", .{err});
 | 
			
		||||
            continue;
 | 
			
		||||
        };
 | 
			
		||||
        defer allocator.free(input);
 | 
			
		||||
        std.debug.print("You entered: {s}\n", .{input});
 | 
			
		||||
 | 
			
		||||
        const Case = enum { add, edit, remove, quit };
 | 
			
		||||
        const case = std.meta.stringToEnum(Case, input) orelse return;
 | 
			
		||||
 | 
			
		||||
        switch (case) {
 | 
			
		||||
            .add => {
 | 
			
		||||
        var task_name: []const u8 = undefined;
 | 
			
		||||
 | 
			
		||||
        if (case != .quit) {
 | 
			
		||||
            try stdout.print("Please enter task name: ", .{});
 | 
			
		||||
                const task_name = getInput(allocator) catch |err| {
 | 
			
		||||
            task_name = getInput(allocator) catch |err| {
 | 
			
		||||
                std.debug.print("Error acquiring input:\n{}\n", .{err});
 | 
			
		||||
                continue;
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        switch (case) {
 | 
			
		||||
            .add => {
 | 
			
		||||
                defer allocator.free(task_name);
 | 
			
		||||
                const duped = try allocator.dupe(u8, task_name);
 | 
			
		||||
                tasks.append(duped) catch |err| {
 | 
			
		||||
@@ -33,13 +44,7 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
 | 
			
		||||
                };
 | 
			
		||||
            },
 | 
			
		||||
            .edit => {
 | 
			
		||||
                try stdout.print("Please enter task name: ", .{});
 | 
			
		||||
                const task_name = getInput(allocator) catch |err| {
 | 
			
		||||
                    std.debug.print("Error acquiring input:\n{}\n", .{err});
 | 
			
		||||
                    continue;
 | 
			
		||||
                };
 | 
			
		||||
                defer allocator.free(task_name);
 | 
			
		||||
 | 
			
		||||
                const id = getTaskIndex(tasks, task_name) catch {
 | 
			
		||||
                    std.debug.print("Invalid task name...", .{});
 | 
			
		||||
                    continue;
 | 
			
		||||
@@ -54,13 +59,7 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
 | 
			
		||||
                tasks.items[id] = new_task_item;
 | 
			
		||||
            },
 | 
			
		||||
            .remove => {
 | 
			
		||||
                try stdout.print("Please enter task name: ", .{});
 | 
			
		||||
                const task_name = getInput(allocator) catch |err| {
 | 
			
		||||
                    std.debug.print("Error acquiring input:\n{}\n", .{err});
 | 
			
		||||
                    continue;
 | 
			
		||||
                };
 | 
			
		||||
                defer allocator.free(task_name);
 | 
			
		||||
 | 
			
		||||
                const id = getTaskIndex(tasks, task_name) catch {
 | 
			
		||||
                    std.debug.print("Invalid task name...", .{});
 | 
			
		||||
                    continue;
 | 
			
		||||
@@ -72,6 +71,7 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
 | 
			
		||||
            },
 | 
			
		||||
            .quit => break,
 | 
			
		||||
        }
 | 
			
		||||
        try clearTerminal();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -92,3 +92,21 @@ fn getTaskIndex(tasks: *std.ArrayList([]const u8), task_name: []const u8) !usize
 | 
			
		||||
 | 
			
		||||
    return InteractionError.TaskNotFound;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn printItems(tasks: *std.ArrayList([]const u8)) !void {
 | 
			
		||||
    if (tasks.items.len <= 0) {
 | 
			
		||||
        try stdout.print("No items\n---------\n", .{});
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try stdout.print("Items:\n------\n", .{});
 | 
			
		||||
    for (tasks.items) |value| {
 | 
			
		||||
        try stdout.print("{s}\n", .{value});
 | 
			
		||||
    }
 | 
			
		||||
    try stdout.print("\n", .{});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn clearTerminal() !void {
 | 
			
		||||
    // clear terminal
 | 
			
		||||
    try stdout.print("\x1B[2J\x1B[H", .{});
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,11 +21,6 @@ pub fn main() !void {
 | 
			
		||||
        file_res.file.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    std.debug.print("Reading from file:\n", .{});
 | 
			
		||||
    for (file_res.tasks.items) |i| {
 | 
			
		||||
        std.debug.print("{s}\n", .{i});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    user_int.userInteraction(allocator, &file_res.tasks) catch |err| {
 | 
			
		||||
        std.debug.print("User Interaction error:\n{}\n", .{err});
 | 
			
		||||
        return err;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user