~mil/mepo

ref: d8fc3a107c750ca3299c7a7c92a30b15835a4ae2 mepo/src/api/filedump.zig -rw-r--r-- 2.2 KiB
d8fc3a10Miles Alan filedump: Working saving of state for preferences 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const Mepo = @import("../Mepo.zig");
const types = @import("../types.zig");
const std = @import("std");
const utilfile = @import("../util/utilfile.zig");
const utilprefs = @import("../util/utilprefs.zig");

pub const spec = .{
    .name = "filedump",
    .desc = "Save the current state of all preferences to a file.",
    .args = (&[_]types.MepoFnSpecArg{
        .{ .tag = .Text, .name = "filepath", .desc = "Path to file to save state to" },
    })[0..],
    .execute = execute,
};

fn execute(mepo: *Mepo, args: [types.MepoFnNargs]types.MepoArg) !void {
    const filepath = args[0].Text;
    try filedump(mepo, filepath);
}

fn filedump(mepo: *Mepo, filepath: []const u8) !void {
    var arena = std.heap.ArenaAllocator.init(mepo.allocator);
    defer arena.deinit();

    const expanded_path = try utilfile.wordexp_filepath(arena.allocator(), filepath);
    try utilfile.mkdirp_folder_basename(expanded_path);
    const mepolang_file = try std.fs.createFileAbsoluteZ(expanded_path, .{});
    defer mepolang_file.close();

    var lines = std.ArrayList([]const u8).init(arena.allocator());

    // 1. Preferences
    for (utilprefs.prefs_mapping) |pref| {
        var is_text = false;
        const numerical_val : f64 = switch (pref.value) {
          .b => @intToFloat(f64, @boolToInt(pref.value.b)),
          .u => @intToFloat(f64, pref.value.u),
          .f => pref.value.f,
          .t => v: { is_text = true; break :v -1; },
        };
        const prefset_statement = statement: {
            if (is_text) {
                break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_t {s} [{s}]", .{pref.name, pref.value.t});
            } else {
                break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_n {s} {d:0.02}", .{pref.name, numerical_val});
            }
        };
        try lines.append(prefset_statement);
    }

    // 2. Bind_button table
    // 3. Bind_gesture table
    // 4. Bind_click table
    // 5. Bind_key table
    // 6. Bind_signal table
    // 7. Pins data
    // 8. TODO: need to rework pin_groups to be reworked into (1) prefs

    const file_data = try std.mem.join(arena.allocator(), "\n", lines.items);
    try mepolang_file.writeAll(file_data);
}