add/remove/edit functionality added

This commit is contained in:
Martin Vrhovšek 2025-02-10 15:57:23 +01:00
parent 840516b545
commit 05b70d844b
3 changed files with 46 additions and 7 deletions

View File

@ -53,13 +53,17 @@ pub fn readTasksFromFile(allocator: std.mem.Allocator, filename: []const u8) !Fi
},
else => return err, //propagate error
}
return FileRead{ .file = file, .tasks = tasks };
}
pub fn writeTasksToFile(file_system: FileRead) !void {
try file_system.file.setEndPos(0);
try file_system.file.seekTo(0);
for (file_system.tasks.items) |item| {
try file_system.file.writeAll(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("\n");
}
}

View File

@ -16,7 +16,7 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
defer allocator.free(input);
std.debug.print("You entered: {s}\n", .{input});
const Case = enum { add, edit, delete, quit };
const Case = enum { add, edit, remove, quit };
const case = std.meta.stringToEnum(Case, input) orelse return;
switch (case) {
@ -32,8 +32,44 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
std.debug.print("Failed to add the specified task:\n{}\n", .{err});
};
},
.edit => {},
.delete => {},
.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;
};
try stdout.print("Please enter new task name: ", .{});
const new_task_name = try getInput(allocator);
defer allocator.free(new_task_name);
const new_task_item = try allocator.dupe(u8, new_task_name);
// free existing memory alloc
allocator.free(tasks.items[id]);
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;
};
// free existing memory alloc
allocator.free(tasks.items[id]);
_ = tasks.orderedRemove(id);
},
.quit => break,
}
}
@ -47,7 +83,7 @@ fn getInput(allocator: std.mem.Allocator) ![]const u8 {
return std.mem.trim(u8, bare_line, "\r");
}
fn getTaskIndex(tasks: *std.ArrayList([]u8), task_name: []u8) !usize {
fn getTaskIndex(tasks: *std.ArrayList([]const u8), task_name: []const u8) !usize {
for (tasks.items, 0..) |value, i| {
if (std.mem.eql(u8, task_name, value)) {
return i;

View File

@ -26,7 +26,6 @@ pub fn main() !void {
std.debug.print("{s}\n", .{i});
}
// todo push allocator in
user_int.userInteraction(allocator, &file_res.tasks) catch |err| {
std.debug.print("User Interaction error:\n{}\n", .{err});
return err;