Compare commits

..
28 Commits
Author SHA1 Message Date
nathanmcrae f38b61e8c1 Remove spurious output 2025-01-14 20:42:55 -08:00
nathanmcrae f55feb0de5 Fix allocation error 2025-01-14 20:42:55 -08:00
nathanmcrae 0d17afe989 Update for zig 0.13 and remove libmagic
20250114T160010
2025-01-14 20:42:54 -08:00
nathanmcrae 934a89e2bf Add TODOs to readme 2025-01-14 20:42:54 -08:00
nathanmcrae 9f5cbc3167 Clean up 2025-01-14 20:42:54 -08:00
nathanmcrae fd95228b1b Add buffer between columns 2025-01-14 20:42:54 -08:00
nathanmcrae 956ab75ebf Update readme 2025-01-14 20:42:54 -08:00
nathanmcrae 6370747ea8 Only display filename, not full path 2025-01-14 20:42:53 -08:00
nathanmcrae cda3c636e2 Add zig-out to .gitignore 2025-01-14 20:42:53 -08:00
nathanmcrae 90437e05e1 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.
2025-01-14 20:42:53 -08:00
nathanmcrae 0c30bd3ae2 Display multiple files 2025-01-14 20:42:53 -08:00
nathanmcrae a5e5c8e59d Add docmap struct
We want to have an array of docmaps, one for each file and be able to
iterate through them. That way the layout can be done in a single
function.
2025-01-14 20:42:53 -08:00
nathanmcrae ef397ae075 Write file name inline 2025-01-14 20:42:53 -08:00
nathanmcrae a76001e0d4 Finish text render test 2025-01-14 20:42:52 -08:00
nathanmcrae bf1f5822f4 Add SDL TTF for text rendering 2025-01-14 20:42:52 -08:00
nathanmcrae 2349c13b67 Added red mark at end of long line 2025-01-14 20:42:52 -08:00
nathanmcrae e87ca40870 Rename bin to docmap 2025-01-14 20:42:52 -08:00
nathanmcrae 074a828aff zig fmt 2025-01-14 20:42:52 -08:00
nathanmcrae ac6bc6012b Added columns 2025-01-14 20:42:51 -08:00
nathanmcrae 57d011fdd6 First pass at a document map viewer 2025-01-14 20:42:51 -08:00
nathanmcrae fb45fe256f update to latest zig 2025-01-14 20:42:51 -08:00
nathanmcrae d8fec2b4ea update to latest zig 2025-01-14 20:42:51 -08:00
nathanmcrae 10ec26c7b7 update to latest zig 2025-01-14 20:42:51 -08:00
nathanmcrae aaedf032c3 update to latest zig 2025-01-14 20:42:51 -08:00
nathanmcrae 05c9940033 zig has extern unions now 2025-01-14 20:42:50 -08:00
nathanmcrae 67ff60279b update to latest zig 2025-01-14 20:42:50 -08:00
nathanmcrae a0bd68be0f don't free the rwop twice 2025-01-14 20:42:50 -08:00
nathanmcrae d985eed563 initial version 2025-01-14 20:42:50 -08:00
3 changed files with 32 additions and 43 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
/zig-cache/
/.zig-cache/
/zig-out/
+14 -12
View File
@@ -1,19 +1,21 @@
const Builder = @import("std").build.Builder;
const std = @import("std");
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("docmap", "src/main.zig");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "docmap",
.root_source_file = b.path("src/main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});
exe.addIncludePath(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2-devel-2.26.5-mingw/SDL2-2.26.5/x86_64-w64-mingw32/include/SDL2" });
exe.addIncludePath(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2_ttf-devel-2.24.0-mingw/SDL2_ttf-2.24.0/x86_64-w64-mingw32/include/SDL2" });
exe.addObjectFile(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2-devel-2.30.11-mingw/SDL2-2.30.11/x86_64-w64-mingw32/bin/SDL2.dll" });
exe.addObjectFile(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2_ttf-devel-2.24.0-mingw/SDL2_ttf-2.24.0/x86_64-w64-mingw32/bin/SDL2_ttf.dll" });
exe.setBuildMode(mode);
exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("SDL2_TTF");
exe.linkSystemLibrary("magic");
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
const run = b.step("run", "Run the demo");
const run_cmd = exe.run();
run.dependOn(&run_cmd.step);
}
+15 -28
View File
@@ -25,18 +25,12 @@ const FileDocMap = struct {
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit()) {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) {
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 args = try std.process.argsAlloc(allocator);
@@ -52,7 +46,7 @@ pub fn main() !void {
}
defer c.SDL_Quit();
const screen = c.SDL_CreateWindow("Document Map Viewer", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, window_width, window_height, c.SDL_WINDOW_OPENGL) orelse
const screen = c.SDL_CreateWindow("Document Map Viewer", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, window_width, window_height, c.SDL_WINDOW_OPENGL | c.SDL_WINDOW_RESIZABLE) orelse
{
c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
@@ -81,12 +75,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);
@@ -107,21 +96,19 @@ pub fn main() !void {
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| {
if (std.fs.cwd().openDir(arg, .{ .iterate = true })) |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) {
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)]);
if (std.mem.eql(u8, entry.name[(entry.name.len - 4)..(entry.name.len)], ".org")) {
try file_paths.append(entry_path[0..(entry_path.len)]);
} else {
allocator.free(entry_path);
}
@@ -131,8 +118,8 @@ pub fn main() !void {
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);
const path = try allocator.alloc(u8, arg.len);
@memcpy(path, arg);
try file_paths.append(path);
} else |_| {
std.debug.print("Arg not file: '{s}'\n", .{arg});
@@ -150,7 +137,7 @@ pub fn main() !void {
file_name = token;
}
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, @ptrCast([*c]const u8, file_name), text_color);
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, @ptrCast(file_name), text_color);
if (surface == null) {
c.SDL_Log("Unable to render text");
return error.RenderTextFailed;
@@ -162,7 +149,7 @@ pub fn main() !void {
return error.CreateTextureFailed;
}
var rect = c.SDL_Rect{
const rect = c.SDL_Rect{
.x = 0,
.y = 0,
.w = surface.*.w,
@@ -174,7 +161,7 @@ pub fn main() !void {
defer file.close();
const stat = try file.stat();
var buffer = try allocator.alloc(u8, stat.size);
const buffer = try allocator.alloc(u8, stat.size);
const bytes_read = try file.readAll(buffer);
if (bytes_read != stat.size) {
@@ -185,7 +172,7 @@ pub fn main() !void {
return error.FileReadError;
}
var docmap = FileDocMap{
const docmap = FileDocMap{
.title_text = DrawnText{
.surface = surface,
.texture = texture,
@@ -214,7 +201,7 @@ pub fn main() !void {
while (!quit) {
var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
switch (event.type) {
c.SDL_QUIT => {
quit = true;
},