~mil/mepo

fc10c8bb13d3aed635306208e70da82b450a3734 — Miles Alan a month ago fd06a1f
filedump: Allow saving pins/preferences/bindings seperately

First arg datatypes can be specified as {r,b,p} to save preferences, bindings,
and pins seperatly or all together.
2 files changed, 59 insertions(+), 51 deletions(-)

M src/api/filedump.zig
M src/config.zig
M src/api/filedump.zig => src/api/filedump.zig +58 -50
@@ 8,17 8,19 @@ pub const spec = .{
    .name = "filedump",
    .desc = "Save the current state of all preferences to a file.",
    .args = (&[_]types.MepoFnSpecArg{
        .{ .tag = .Text, .name = "datatypes", .desc = "Types of data to save (p = pins, r = preferences, b = bindings); ex. 'pr' would save both pins and preferences but not bindings" },
        .{ .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);
    const save_types = args[0].Text;
    const filepath = args[1].Text;
    try filedump(mepo, save_types, filepath);
}

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



@@ 26,62 28,68 @@ fn filedump(mepo: *Mepo, filepath: []const u8) !void {
    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 format_as : enum { Text, HexText, Number } = .Number;
    if (std.mem.containsAtLeast(u8, save_types, 1, "r")) {
        for (utilprefs.prefs_mapping) |pref| {
            var format_as : enum { Text, HexText, Number } = .Number;

        const numerical_val : f64 = switch (pref.value) {
          .b => @intToFloat(f64, @boolToInt(pref.value.b)),
          .u => @intToFloat(f64, pref.value.u),
          .u24 => v: { format_as = .HexText; break :v -1; },
          .f => pref.value.f,
          .t => v: { format_as = .Text; break :v -1; },
        };
        const prefset_statement = statement: {
            if (format_as == .Text) {
                break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_t {s} [{s}];", .{pref.name, pref.value.t});
            } else if (format_as == .Number) {
                break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_n {s} {d:0.02};", .{pref.name, numerical_val});
            } else if (format_as == .HexText) {
                break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_t {s} #{x};", .{pref.name, pref.value.u24});
            } else {
                break :statement "";
            }
        };
        try lines.append(prefset_statement);
            const numerical_val : f64 = switch (pref.value) {
              .b => @intToFloat(f64, @boolToInt(pref.value.b)),
              .u => @intToFloat(f64, pref.value.u),
              .u24 => v: { format_as = .HexText; break :v -1; },
              .f => pref.value.f,
              .t => v: { format_as = .Text; break :v -1; },
            };
            const prefset_statement = statement: {
                if (format_as == .Text) {
                    break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_t {s} [{s}];", .{pref.name, pref.value.t});
                } else if (format_as == .Number) {
                    break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_n {s} {d:0.02};", .{pref.name, numerical_val});
                } else if (format_as == .HexText) {
                    break :statement try std.fmt.allocPrint(arena.allocator(), "prefset_t {s} #{x};", .{pref.name, pref.value.u24});
                } else {
                    break :statement "";
                }
            };
            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
    for (mepo.pin_groups) |pg, pg_i| {
        for (pg.items) |pin| {
            const is_structural : u32 = b: {
                if (pin.category == .Structural) break :b 1;
                break :b 0;
            };
            const statement_pin_add = try std.fmt.allocPrint(
                arena.allocator(),
                "pin_add {d} {d} {d:.4} {d:.4} [{s}] [{s}];",
                .{ pg_i, is_structural, pin.lat, pin.lon, pin.handle, pin.name },
            );
            try lines.append(statement_pin_add);
    // 2. Bindings
    if (std.mem.containsAtLeast(u8, save_types, 1, "b")) {
        // - TODO: Bind_button table
        // - TODO: Bind_gesture table
        // - TODO: Bind_click table
        // - TODO: Bind_key table
        // - TODO: Bind_signal table
    }

            var it = pin.metadata.iterator();
            while (it.next()) |kv| {
                const statement_meta = try std.fmt.allocPrint(
    // 3. Pins data
    if (std.mem.containsAtLeast(u8, save_types, 1, "p")) {
        for (mepo.pin_groups) |pg, pg_i| {
            for (pg.items) |pin| {
                const is_structural : u32 = b: {
                    if (pin.category == .Structural) break :b 1;
                    break :b 0;
                };
                const statement_pin_add = try std.fmt.allocPrint(
                    arena.allocator(),
                    "pin_meta {d} [{s}] [{s}] [{s}];",
                    .{ pg_i, pin.handle, kv.key_ptr.*, kv.value_ptr.* },
                    "pin_add {d} {d} {d:.4} {d:.4} [{s}] [{s}];",
                    .{ pg_i, is_structural, pin.lat, pin.lon, pin.handle, pin.name },
                );
                try lines.append(statement_meta);
                try lines.append(statement_pin_add);

                var it = pin.metadata.iterator();
                while (it.next()) |kv| {
                    const statement_meta = try std.fmt.allocPrint(
                        arena.allocator(),
                        "pin_meta {d} [{s}] [{s}] [{s}];",
                        .{ pg_i, pin.handle, kv.key_ptr.*, kv.value_ptr.* },
                    );
                    try lines.append(statement_meta);
                }
            }
        }
    }

M src/config.zig => src/config.zig +1 -1
@@ 62,7 62,7 @@ pub const DefaultBaseConfig =
    \\ bind_key c c [quit];
    \\ bind_key _ y [clipcopy];
    \\ bind_key s y [clippaste];
    \\ bind_key c s [filedump $XDG_CONFIG_HOME/mepo/savestate];
    \\ bind_key c s [filedump rbp $XDG_CONFIG_HOME/mepo/savestate];
    \\ bind_key cs s [fileload $XDG_CONFIG_HOME/mepo/savestate];
    \\
    \\ # Scripts hotkeys;