Open directories of files
You can pass files / dirs as arguments an they will be opened. The directories will be searched for text files (as identified by libmagic) and those text files will be displayed too.
This commit is contained in:
parent
0da13751d5
commit
32c0b9bb5c
|
@ -7,6 +7,7 @@ pub fn build(b: *Builder) void {
|
|||
exe.setBuildMode(mode);
|
||||
exe.linkSystemLibrary("SDL2");
|
||||
exe.linkSystemLibrary("SDL2_TTF");
|
||||
exe.linkSystemLibrary("magic");
|
||||
exe.linkSystemLibrary("c");
|
||||
|
||||
b.default_step.dependOn(&exe.step);
|
||||
|
|
79
src/main.zig
79
src/main.zig
|
@ -5,6 +5,10 @@ const c = @cImport({
|
|||
@cInclude("SDL_ttf.h");
|
||||
});
|
||||
|
||||
const m = @cImport({
|
||||
@cInclude("magic.h");
|
||||
});
|
||||
|
||||
const assert = @import("std").debug.assert;
|
||||
|
||||
const DrawnText = struct {
|
||||
|
@ -21,14 +25,24 @@ const FileDocMap = struct {
|
|||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer {
|
||||
if(gpa.deinit()) {
|
||||
if (gpa.deinit()) {
|
||||
std.debug.print("Allocator leak\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
var magic = m.magic_open(m.MAGIC_MIME | m.MAGIC_CHECK);
|
||||
const load_result = m.magic_load(magic, null);
|
||||
if (load_result != 0) {
|
||||
std.debug.print("Unable to initialize magic: {d}", .{load_result});
|
||||
return error.MagicInitializationFailed;
|
||||
}
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const window_width = 300;
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
|
||||
const window_width = 700;
|
||||
const window_height = 500;
|
||||
const column_width = 100;
|
||||
|
||||
|
@ -67,12 +81,7 @@ pub fn main() !void {
|
|||
}
|
||||
defer c.TTF_Quit();
|
||||
|
||||
const text_color: c.SDL_Color = c.SDL_Color {
|
||||
.r = 0,
|
||||
.g = 0,
|
||||
.b = 0,
|
||||
.a = 255
|
||||
};
|
||||
const text_color: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
|
||||
|
||||
// TODO: can we embed this file?
|
||||
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 12);
|
||||
|
@ -84,8 +93,47 @@ pub fn main() !void {
|
|||
|
||||
var file_paths = std.ArrayList([]const u8).init(allocator);
|
||||
defer file_paths.deinit();
|
||||
try file_paths.append("src/main.zig");
|
||||
try file_paths.append("GapProbeScan.cs");
|
||||
defer {
|
||||
for (file_paths.items) |item| {
|
||||
allocator.free(item);
|
||||
}
|
||||
}
|
||||
|
||||
var args_i: usize = 1;
|
||||
while (args_i < args.len) : (args_i += 1) {
|
||||
const arg = args[args_i];
|
||||
if (std.fs.cwd().openIterableDir(arg, .{})) |iter_dir| {
|
||||
std.debug.print("searching dir for files: '{s}'\n", .{arg});
|
||||
|
||||
var iter = iter_dir.iterate();
|
||||
while (try iter.next()) |entry| {
|
||||
if (entry.kind == .File) {
|
||||
const entry_path = try std.fmt.allocPrint(allocator, "{s}{c}{s}\x00", .{
|
||||
arg,
|
||||
std.fs.path.sep,
|
||||
entry.name,
|
||||
});
|
||||
const magic_result = m.magic_file(magic, @ptrCast([*c]const u8, entry_path));
|
||||
std.debug.print("'{s}': {s}\n", .{ entry.name, magic_result });
|
||||
if (std.mem.eql(u8, magic_result[0..5], "text/")) {
|
||||
try file_paths.append(entry_path[0..(entry_path.len - 1)]);
|
||||
} else {
|
||||
allocator.free(entry_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else |_| {
|
||||
if (std.fs.cwd().openFile(arg, .{})) |file| {
|
||||
file.close();
|
||||
std.debug.print("Will do docmap for: '{s}'\n", .{arg});
|
||||
var path = try allocator.alloc(u8, arg.len);
|
||||
std.mem.copy(u8, path, arg);
|
||||
try file_paths.append(path);
|
||||
} else |_| {
|
||||
std.debug.print("Arg not file: '{s}'\n", .{arg});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var file_docmaps = std.ArrayList(FileDocMap).init(allocator);
|
||||
defer file_docmaps.deinit();
|
||||
|
@ -103,14 +151,15 @@ pub fn main() !void {
|
|||
return error.CreateTextureFailed;
|
||||
}
|
||||
|
||||
var rect = c.SDL_Rect {
|
||||
var rect = c.SDL_Rect{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = surface.*.w,
|
||||
.h = surface.*.h,
|
||||
};
|
||||
|
||||
const file = try std.fs.cwd().openFile(path, .{ });
|
||||
std.debug.print("open file: {s}\n", .{path});
|
||||
const file = try std.fs.cwd().openFile(path, .{});
|
||||
defer file.close();
|
||||
|
||||
const stat = try file.stat();
|
||||
|
@ -125,8 +174,8 @@ pub fn main() !void {
|
|||
return error.FileReadError;
|
||||
}
|
||||
|
||||
var docmap = FileDocMap {
|
||||
.title_text = DrawnText {
|
||||
var docmap = FileDocMap{
|
||||
.title_text = DrawnText{
|
||||
.surface = surface,
|
||||
.texture = texture,
|
||||
.rect = rect,
|
||||
|
@ -191,7 +240,7 @@ pub fn main() !void {
|
|||
for (file_docmaps.items) |docmap| {
|
||||
i = 0;
|
||||
|
||||
var rect = c.SDL_Rect {
|
||||
var rect = c.SDL_Rect{
|
||||
.x = x,
|
||||
.y = y,
|
||||
.w = docmap.title_text.surface.*.w,
|
||||
|
|
Loading…
Reference in New Issue
Block a user