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