Fix wrong session index + save file corruption

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2025-12-06 14:25:01 +01:00
parent a9ff0a6d07
commit e6966a628c
5 changed files with 26 additions and 3 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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,
});
}
}

View File

@@ -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;
}
}

View File

@@ -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,
});
}