~mil/mepo

d8fc3a107c750ca3299c7a7c92a30b15835a4ae2 — Miles Alan a month ago 06fc414
filedump: Working saving of state for preferences
2 files changed, 37 insertions(+), 14 deletions(-)

M src/api/filedump.zig
M src/util/utilprefs.zig
M src/api/filedump.zig => src/api/filedump.zig +36 -13
@@ 2,6 2,7 @@ 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",


@@ 18,21 19,43 @@ fn execute(mepo: *Mepo, args: [types.MepoFnNargs]types.MepoArg) !void {
}

fn filedump(mepo: *Mepo, filepath: []const u8) !void {
    const expanded_path = try utilfile.wordexp_filepath(mepo.allocator, filepath);
    defer mepo.allocator.free(expanded_path);
    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();

    // Save tables:
    //   bind_button
    //   bind_gesture
    //   bind_click
    //   bind_key
    //   bind_signal
    // Save prefs:
    //   loop prefs
    //   TODO: rework for pin_groups prefs merge into prefs system
    // Save pins:
    //   loop pins
    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);
}

M src/util/utilprefs.zig => src/util/utilprefs.zig +1 -1
@@ 9,7 9,7 @@ const PrefMapping = struct {
};

const n_prefs = 16;
var prefs_mapping: [n_prefs]PrefMapping = mapping: {
pub var prefs_mapping: [n_prefs]PrefMapping = mapping: {
    var mapping: [n_prefs]PrefMapping = undefined;
    mapping[0] = .{
        .name = "debug_message_enabled",