Browse Source

Make directories to search in configurable (and preview with sushi?!)

Luna Stadler 4 years ago
parent
commit
3b3f687b4d
1 changed files with 36 additions and 2 deletions
  1. 36 2
      zig/sdl/hello_sdl.zig

+ 36 - 2
zig/sdl/hello_sdl.zig

@ -229,7 +229,13 @@ const Runner = struct {
229 229
    }
230 230
};
231 231
232
const Config = struct {
233
    var searchDirectories: []const []const u8 = undefined;
234
    var searchDirectoriesString: []const u8 = undefined;
235
};
236
232 237
var cmd_buf: [1000]u8 = undefined;
238
var argv_buf: [100][]const u8 = undefined;
233 239
234 240
const GoDocRunner = struct {
235 241
    fn init() Runner {
@ -348,7 +354,12 @@ const SearchRunner = struct {
348 354
349 355
    fn toArgv(cmd: []const u8) []const []const u8 {
350 356
        _ = std.fmt.bufPrint(&cmd_buf, "{s}\x00", .{cmd["s ".len..]}) catch "???";
351
        return &[_][]const u8{ "ag", "--color", &cmd_buf, "/home/luna/k/the-thing", "/home/luna/t/zig" };
357
        argv_buf[0] = "ag";
358
        argv_buf[1] = &cmd_buf;
359
        for (Config.searchDirectories) |dir, i| {
360
            argv_buf[2 + i] = dir;
361
        }
362
        return argv_buf[0 .. 2 + Config.searchDirectories.len];
352 363
    }
353 364
};
354 365
@ -363,7 +374,7 @@ const FileRunner = struct {
363 374
364 375
    fn toArgv(cmd: []const u8) []const []const u8 {
365 376
        // FIXME: replace with choice + selection whenever that is implemented
366
        _ = std.fmt.bufPrint(&cmd_buf, "find ~/k/the-thing ~/t/zig -type f -name '{s}' | head -n1 | tee /tmp/file-search && ([ \"$(wc -l < /tmp/file-search)\" = \"1\" ] && cat \"$(head -n1 /tmp/file-search)\") || echo \"not found\"\x00", .{cmd["file ".len..]}) catch "???";
377
        _ = std.fmt.bufPrint(&cmd_buf, "find {s} -type f -path '*{s}*' -or -name '*{s}*' | tee /tmp/file-search && ([ \"$(wc -l < /tmp/file-search)\" = \"1\" ] && ((file --mime \"$(head -n1 /tmp/file-search)\" | grep 'text/' &> /dev/null && cat \"$(head -n1 /tmp/file-search)\") || sushi \"$(head -n1 /tmp/file-search)\")) || echo \"not found\"\x00", .{ Config.searchDirectoriesString, cmd["file ".len..], cmd["file ".len..] }) catch "???";
367 378
        return &[_][]const u8{ "bash", "-c", &cmd_buf };
368 379
    }
369 380
};
@ -412,6 +423,27 @@ pub fn main() !void {
412 423
    const args = try std.process.argsAlloc(gpa);
413 424
    defer std.process.argsFree(gpa, args);
414 425
426
    var dirsList = std.ArrayList([]const u8).init(gpa);
427
    var dirsString = std.ArrayList(u8).init(gpa);
428
    if (std.os.getenv("SEARCH_DIRS")) |dirsEnv| {
429
        var dirs = std.mem.split(u8, dirsEnv, ":");
430
        var dir = dirs.next();
431
        while (dir != null) : (dir = dirs.next()) {
432
            try dirsList.append(dir.?);
433
        }
434
    } else {
435
        try dirsList.append("/home/luna/k/the-thing");
436
        try dirsList.append("/home/luna/t/zig");
437
    }
438
    Config.searchDirectories = dirsList.toOwnedSlice();
439
    for (Config.searchDirectories) |dir| {
440
        try dirsString.appendSlice(dir);
441
        try dirsString.append(' ');
442
    }
443
    Config.searchDirectoriesString = dirsString.toOwnedSlice();
444
    defer gpa.free(Config.searchDirectories);
445
    defer gpa.free(Config.searchDirectoriesString);
446
415 447
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
416 448
        c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
417 449
        return error.SDLInitializationFailed;
@ -673,6 +705,8 @@ pub fn main() !void {
673 705
                continue;
674 706
            }
675 707
708
            // TODO: indicate if command is still running
709
676 710
            {
677 711
                const name = try gpa.dupeZ(u8, command.name);
678 712
                const result_text = c.TTF_RenderUTF8_Shaded(font, name, gray, c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 });