interactive shell navigation and history
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204const std = @import("std");const assert = std.debug.assert;const command_line = @import("command_line.zig");const config = @import("config.zig");const navigate = @import("navigate.zig");const database = @import("database.zig");const input_output = @import("input_output.zig");
pub const std_options: std.Options = .{ .log_level = .err, .logFn = struct { fn log( comptime _: std.log.Level, comptime _: @EnumLiteral(), comptime _: []const u8, _: anytype, ) void {} }.log,};
pub fn main(init: std.process.Init) void { input_output.init(init.io, init.environ_map); const allocator = init.gpa;
const arguments = command_line.parseArguments(allocator, init.minimal.args) catch |err| { input_output.fatal("failed to parse arguments: {}", .{err}); }; defer arguments.deinit();
if (arguments.action == .help) { command_line.printUsage() catch |err| { input_output.fatal("failed to print usage: {}", .{err}); }; return; }
const parsed_config = config.loadConfig(allocator); defer if (parsed_config) |app_config| app_config.deinit(); const default_config = config.Config{}; const base_config = if (parsed_config) |app_config| app_config.value else default_config;
var current_directory_buffer: [std.fs.max_path_bytes]u8 = undefined; const current_directory = currentDirectory(¤t_directory_buffer); const app_config = config.applyFolderOverride(base_config, current_directory);
const database_path = database.getDefaultDatabasePath(allocator) catch |err| { input_output.fatal("failed to get database path: {}", .{err}); }; defer allocator.free(database_path);
var history = database.Database.init(allocator, database_path) catch |err| { input_output.fatal("failed to initialize database: {}", .{err}); }; defer history.deinit();
runAction(allocator, &history, arguments, &app_config) catch |err| { if (err == error.UserAbort) std.process.exit(1); input_output.fatal("action failed: {}", .{err}); };}
fn currentDirectory(buffer: *[std.fs.max_path_bytes]u8) []const u8 { const count = std.process.currentPath(input_output.runtime(), buffer) catch |err| { input_output.warn("failed to get working directory for folder overrides: {}\n", .{err}); return ""; }; assert(count <= buffer.len); return buffer[0..count];}
fn runAction( allocator: std.mem.Allocator, history: *database.Database, arguments: command_line.Arguments, app_config: *const config.Config,) !void { assert(history.base_path.len > 0); var buffer: [4096]u8 = undefined; var writer = input_output.getStdout(&buffer); assert(!writer.failed);
switch (arguments.action) { .recent_directories => try printRecentDirectories(&writer, history), .recent_files => try printRecentFiles(&writer, history), .favorites => try navigate.favorites(allocator, history, app_config, arguments.choose_file), .git_status => try navigate.gitStatus(allocator, history, app_config, arguments.choose_file), .select => try navigate.selectFromStdin(allocator, app_config, arguments.choose_file), .search => try printSearchResults(&writer, history, arguments.query), .goto_directory => try navigate.recentDirectory( allocator, history, app_config, arguments.choose_file, ), .goto_file => try navigate.recentFile(allocator, history, app_config, arguments.choose_file), .look_file => try navigate.look( allocator, history, .file, app_config, arguments.depth, arguments.choose_file, ), .look_directory => try navigate.look( allocator, history, .directory, app_config, arguments.depth, arguments.choose_file, ), .grep => try navigate.grep( allocator, history, app_config, arguments.query, arguments.choose_file, ), .cleanup => try history.cleanup(), .log_directory => { if (arguments.log_path) |path| { try history.logDirectory(path); } }, .log_file => { if (arguments.log_path) |path| { const file_type = arguments.file_type orelse "other"; const file_action = arguments.file_action orelse "open"; try history.logFile(path, file_type, file_action); } }, .help => try command_line.printUsage(), } if (writer.failed) { return error.WriteFailed; } try writer.end();}
fn printRecentDirectories(writer: *input_output.Writer, history: *database.Database) !void { assert(history.base_path.len > 0); const managed = try history.recentDirectories(); defer managed.deinit(); for (managed.value) |entry| { assert(entry.path.len > 0); try writer.print("{s}|{s}\n", .{ entry.timestamp orelse "-", entry.path }); }}
fn printRecentFiles(writer: *input_output.Writer, history: *database.Database) !void { assert(history.base_path.len > 0); const managed = try history.recentFiles(); defer managed.deinit(); for (managed.value) |entry| { assert(entry.path.len > 0); const timestamp = entry.timestamp orelse "-"; try writer.print("{s}|{s}|{s}|{s}\n", .{ timestamp, entry.file_type, entry.action, entry.path, }); }}
fn printSearchResults( writer: *input_output.Writer, history: *database.Database, query: ?[]const u8,) !void { assert(history.base_path.len > 0); const needle = query orelse return; const managed = try history.searchHistory(needle); defer managed.deinit(); const results = managed.value;
for (results.directories) |entry| { assert(entry.path.len > 0); try writer.print("D|{s}\n", .{entry.path}); } for (results.files) |entry| { assert(entry.path.len > 0); const line = .{ entry.file_type, entry.action, entry.path }; try writer.print("F|{s}|{s}|{s}\n", line); }}
test { _ = command_line; _ = config; _ = navigate; _ = database;}
test "vaxis logging disabled" { try std.testing.expect(std_options.logFn != std.log.defaultLog); try std.testing.expect(std_options.log_scope_levels.len == 0); try std.testing.expect(@backingInt(std_options.log_level) < @backingInt(std.log.Level.warn)); try std.testing.expect(@backingInt(std_options.log_level) < @backingInt(std.log.Level.info)); std_options.logFn(.err, .vaxis, "{s}", .{"suppressed"}); std_options.logFn(.info, .vaxis, "{s}", .{"suppressed"}); std_options.logFn(.warn, .vaxis_parser, "{s}", .{"suppressed"});}