diff --git a/src/config/SavedUsers.zig b/src/config/SavedUsers.zig index dd08eb4..4891f27 100644 --- a/src/config/SavedUsers.zig +++ b/src/config/SavedUsers.zig @@ -5,6 +5,8 @@ const SavedUsers = @This(); const User = struct { username: []const u8, session_index: usize, + first_run: bool, + allocated_username: bool, }; user_list: std.ArrayList(User), @@ -18,5 +20,9 @@ pub fn init() SavedUsers { } pub fn deinit(self: *SavedUsers, allocator: std.mem.Allocator) void { + for (self.user_list.items) |user| { + if (user.allocated_username) allocator.free(user.username); + } + self.user_list.deinit(allocator); } diff --git a/src/config/migrator.zig b/src/config/migrator.zig index bfba870..124ec89 100644 --- a/src/config/migrator.zig +++ b/src/config/migrator.zig @@ -238,7 +238,12 @@ pub fn tryMigrateIniSaveFile(allocator: std.mem.Allocator, save_ini: *ini.Ini(Ol if (std.mem.eql(u8, user, username)) saved_users.last_username_index = i; } - try saved_users.user_list.append(allocator, .{ .username = username, .session_index = save.session_index orelse 0 }); + try saved_users.user_list.append(allocator, .{ + .username = username, + .session_index = save.session_index orelse 0, + .first_run = false, + .allocated_username = false, + }); } return true; diff --git a/src/main.zig b/src/main.zig index d1e429f..8565c9a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -255,18 +255,23 @@ pub fn main() !void { const session_index = std.fmt.parseInt(usize, session_index_str, 10) catch continue; try saved_users.user_list.append(allocator, .{ - .username = username, + .username = try allocator.dupe(u8, username), .session_index = session_index, + .first_run = false, + .allocated_username = true, }); } } // If no save file previously existed, fill it up with all usernames + // TODO: Add new username with existing save file if (config.save and saved_users.user_list.items.len == 0) { for (usernames.items) |user| { try saved_users.user_list.append(allocator, .{ .username = user, .session_index = 0, + .first_run = true, + .allocated_username = false, }); } } diff --git a/src/tui/components/Session.zig b/src/tui/components/Session.zig index ac6188b..74ab7a2 100644 --- a/src/tui/components/Session.zig +++ b/src/tui/components/Session.zig @@ -46,7 +46,10 @@ pub fn addEnvironment(self: *Session, environment: Environment) !void { fn sessionChanged(env: Env, maybe_user_list: ?*UserList) void { if (maybe_user_list) |user_list| { - user_list.label.list.items[user_list.label.current].session_index.* = env.index; + const user = user_list.label.list.items[user_list.label.current]; + if (!user.first_run) return; + + user.session_index.* = env.index; } } diff --git a/src/tui/components/UserList.zig b/src/tui/components/UserList.zig index 83819bb..d9ed846 100644 --- a/src/tui/components/UserList.zig +++ b/src/tui/components/UserList.zig @@ -11,6 +11,7 @@ pub const User = struct { name: []const u8, session_index: *usize, allocated_index: bool, + first_run: bool, }; const UserLabel = generic.CyclableLabel(User, *Session); @@ -27,9 +28,11 @@ pub fn init(allocator: Allocator, buffer: *TerminalBuffer, usernames: StringList if (username.len == 0) continue; var maybe_session_index: ?*usize = null; + var first_run = true; for (saved_users.user_list.items) |*saved_user| { if (std.mem.eql(u8, username, saved_user.username)) { maybe_session_index = &saved_user.session_index; + first_run = saved_user.first_run; break; } } @@ -45,6 +48,7 @@ pub fn init(allocator: Allocator, buffer: *TerminalBuffer, usernames: StringList .name = username, .session_index = maybe_session_index.?, .allocated_index = allocated_index, + .first_run = first_run, }); }