interactive shell navigation and history
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172const std = @import("std");const assert = std.debug.assert;const input_output = @import("input_output.zig");
pub const Theme = struct { prompt: ?[]const u8 = null, match: ?[]const u8 = null, cursor_row: ?[]const u8 = null, status: ?[]const u8 = null, preview_current: ?[]const u8 = null, bg: ?[]const u8 = null, fg: ?[]const u8 = null,};
pub const Config = struct { openers: []Opener = &.{}, ignored_patterns: []const []const u8 = &.{}, favorites: []const []const u8 = &.{}, editor: ?[]const u8 = null, max_depth: usize = 3, theme: Theme = .{}, overrides: []FolderOverride = &.{},
pub const Opener = struct { extensions: []const []const u8, action: []const u8, command: []const u8, };};
pub const FolderOverride = struct { path: []const u8, openers: ?[]Config.Opener = null, ignored_patterns: ?[]const []const u8 = null, favorites: ?[]const []const u8 = null, editor: ?[]const u8 = null, max_depth: ?usize = null, theme: ?Theme = null,};
pub fn applyFolderOverride(base: Config, current_directory: []const u8) Config { for (base.overrides) |*override| { assert(override.path.len > 0); if (!std.mem.eql(u8, current_directory, override.path)) continue; var result = base; if (override.openers) |openers| result.openers = openers; if (override.ignored_patterns) |patterns| result.ignored_patterns = patterns; if (override.favorites) |favorites| result.favorites = favorites; if (override.editor) |editor| result.editor = editor; if (override.max_depth) |depth| result.max_depth = depth; if (override.theme) |theme| result.theme = theme; return result; } return base;}
const max_config_bytes: usize = 1024 * 1024;
pub fn loadConfig(allocator: std.mem.Allocator) ?std.json.Parsed(Config) { const home = input_output.getenv("HOME") orelse { input_output.warn("HOME not set, skipping config load\n", .{}); return null; }; if (home.len == 0) { input_output.warn("HOME is empty, skipping config load\n", .{}); return null; } const current_directory = std.Io.Dir.cwd(); const path_parts = [_][]const u8{ home, ".config", "magdalena", "config.json" }; const path = std.fs.path.join(allocator, &path_parts) catch { input_output.warn("failed to allocate config path\n", .{}); return null; }; defer allocator.free(path);
const content = current_directory.readFileAlloc( input_output.runtime(), path, allocator, .limited(max_config_bytes), ) catch |err| { if (err == error.StreamTooLong) { input_output.warn( "config too large (over {d} bytes), ignoring {s}\n", .{ max_config_bytes, path }, ); return null; } if (err != error.FileNotFound) { input_output.warn("failed to read config {s}: {}\n", .{ path, err }); } return null; }; defer allocator.free(content);
const parsed = std.json.parseFromSlice(Config, allocator, content, .{ .ignore_unknown_fields = true, .allocate = .alloc_always, }) catch |err| { input_output.warn("failed to parse config {s}: {}\n", .{ path, err }); return null; }; return parsed;}
const testing = std.testing;
test "applyFolderOverride: no overrides returns base" { const base = Config{ .max_depth = 3, .editor = "vim" }; const got = applyFolderOverride(base, "/anywhere"); try testing.expectEqual(@as(usize, 3), got.max_depth); try testing.expectEqualStrings("vim", got.editor.?);}
test "applyFolderOverride: matching path overrides only provided fields" { const ignored = [_][]const u8{ "node_modules", ".git" }; var overrides = [_]FolderOverride{ .{ .path = "/work", .max_depth = 9, .editor = "nvim" }, }; const base = Config{ .max_depth = 3, .editor = "vim", .ignored_patterns = &ignored, .overrides = &overrides, };
const got = applyFolderOverride(base, "/work"); try testing.expectEqual(@as(usize, 9), got.max_depth); try testing.expectEqualStrings("nvim", got.editor.?); try testing.expectEqual(base.ignored_patterns.ptr, got.ignored_patterns.ptr);}
test "applyFolderOverride: non-matching path leaves base untouched" { var overrides = [_]FolderOverride{ .{ .path = "/work", .max_depth = 9 }, }; const base = Config{ .max_depth = 3, .editor = "vim", .overrides = &overrides };
const got = applyFolderOverride(base, "/elsewhere"); try testing.expectEqual(@as(usize, 3), got.max_depth); try testing.expectEqualStrings("vim", got.editor.?);}
test "applyFolderOverride: first matching override wins" { var overrides = [_]FolderOverride{ .{ .path = "/work", .max_depth = 5 }, .{ .path = "/work", .max_depth = 8 }, }; const base = Config{ .max_depth = 3, .overrides = &overrides };
const got = applyFolderOverride(base, "/work"); try testing.expectEqual(@as(usize, 5), got.max_depth);}
test "applyFolderOverride: theme replaces wholesale" { var overrides = [_]FolderOverride{ .{ .path = "/work", .theme = .{ .prompt = "9" } }, }; const base = Config{ .theme = .{ .match = "3" }, .overrides = &overrides, };
const got = applyFolderOverride(base, "/work"); try testing.expectEqualStrings("9", got.theme.prompt.?); try testing.expect(got.theme.match == null);
const kept = applyFolderOverride(base, "/elsewhere"); try testing.expectEqualStrings("3", kept.theme.match.?); try testing.expect(kept.theme.prompt == null);}