//! Whether the reader loads mail's images from the web as it opens a //! thread, or asks first (i). Mail in Spam always asks: a sender learns //! the mail was read when its images load. Every account's reader //! shares one, saved in the top state directory as it changes. const std = @import("std"); const Settings = @import("Settings.zig"); const mem = std.mem; const testing = std.testing; const Images = @This(); /// Whether they load without asking, outside Spam. auto: bool, settings: ?Settings, pub const file = "images"; /// The choice saved in `settings`: they load unless "ask" was saved. pub fn load(settings: ?Settings) Images { var buf: [16]u8 = undefined; const saved = if (settings) |s| s.load(file, &buf) else null; return .{ .auto = !mem.eql(u8, mem.trim(u8, saved orelse "", " \n"), "ask"), .settings = settings }; } pub fn toggle(self: *Images) void { self.auto = !self.auto; const s = self.settings orelse return; s.save(file, if (self.auto) "load\n" else "ask\n"); } test "images load unless asking was saved, and the choice is saved as it changes" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); const settings: Settings = .{ .io = testing.io, .dir = tmp.dir }; var images = load(settings); try testing.expect(images.auto); images.toggle(); try testing.expect(!load(settings).auto); images.toggle(); try testing.expect(load(settings).auto); }