This commit is contained in:
Martin Vrhovšek 2025-03-15 17:42:48 +01:00
parent c2255bf1e0
commit 7dd74025fe

View File

@ -16,23 +16,15 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
continue; continue;
}; };
// todo make available commads be listed from enum itself const Case = enum { add, edit, remove, quit };
const Case = enum { add, edit, remove, move, quit };
var fields = std.ArrayList([]const u8).init(allocator); try stdout.print("Please enter your choice [add/remove/edit/quit]: ", .{});
defer fields.deinit();
inline for (@typeInfo(Case).Enum.fields) |f| {
fields.append(f.name);
}
try stdout.print("Please enter your choice [add/remove/edit/move/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);
const case = std.meta.stringToEnum(Case, input) orelse continue; const case = std.meta.stringToEnum(Case, input) orelse continue;
var task_name: []const u8 = undefined; var task_name: []const u8 = undefined;
@ -79,56 +71,11 @@ pub fn userInteraction(allocator: std.mem.Allocator, tasks: *std.ArrayList([]con
allocator.free(tasks.items[id]); allocator.free(tasks.items[id]);
_ = tasks.orderedRemove(id); _ = tasks.orderedRemove(id);
}, },
.move => {
try stdout.print("Please enter task name or number: ", .{});
_ = getTaskIndex(tasks, task_name) catch {
std.debug.print("Invalid task name...", .{});
continue;
};
const new_input = getInput(allocator) catch |err| {
std.debug.print("Failed to get new input:\n{}\n", .{err});
continue;
};
defer allocator.free(new_input);
_ = std.fmt.parseInt(usize, new_input, 10) catch |err| {
std.debug.print("Failed to convert the input into ID:\n{}\n", .{err});
continue;
};
// todo restructure arraylist
},
.quit => break, .quit => break,
} }
} }
} }
fn get_fields_str(allocator: std.mem.Allocator, arr: std.ArrayList([]const u8)) ![]const u8 {
// get len
var len = 0;
for (arr.items) |value| {
len += value.len;
}
len += arr.capacity - 1;
// todo free mem
var result = try allocator.alloc(u8, len);
defer allocator.free(result);
var cur_len = 0;
for (arr.items) |value| {
@memcpy(result[0..], value);
cur_len += value.len;
if (value == arr.items[arr.capacity-1]) continue;
@memcpy(result[cur_len..], "/");
cur_len += 1;
}
return result;
}
fn getInput(allocator: std.mem.Allocator) ![]const u8 { fn getInput(allocator: std.mem.Allocator) ![]const u8 {
const stdin = std.io.getStdIn().reader(); const stdin = std.io.getStdIn().reader();
const bare_line = try stdin.readUntilDelimiterAlloc(allocator, '\n', 8192); const bare_line = try stdin.readUntilDelimiterAlloc(allocator, '\n', 8192);