From 11bb024ed99d1955de10a244607cd08c23de8bc0 Mon Sep 17 00:00:00 2001
From: Marto <marto@marto.si>
Date: Mon, 10 Feb 2025 18:46:21 +0100
Subject: [PATCH] basic implementation - fin

---
 src/file_op.zig     |  6 ++---
 src/interaction.zig | 60 +++++++++++++++++++++++++++++----------------
 src/main.zig        |  5 ----
 3 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/src/file_op.zig b/src/file_op.zig
index 352c0cf..b61d311 100644
--- a/src/file_op.zig
+++ b/src/file_op.zig
@@ -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");
     }
 }
diff --git a/src/interaction.zig b/src/interaction.zig
index c762c2b..f6c63a6 100644
--- a/src/interaction.zig
+++ b/src/interaction.zig
@@ -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;
 
+        var task_name: []const u8 = undefined;
+
+        if (case != .quit) {
+            try stdout.print("Please enter task name: ", .{});
+            task_name = getInput(allocator) catch |err| {
+                std.debug.print("Error acquiring input:\n{}\n", .{err});
+                continue;
+            };
+        }
+
         switch (case) {
             .add => {
-                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 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", .{});
+}
diff --git a/src/main.zig b/src/main.zig
index 6ffed95..3a9b307 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -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;