From 115de253a81f5d222155c56fa94204dc514e5719 Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Sat, 14 Oct 2023 00:01:08 +0600 Subject: [PATCH] replace ncurses_refs.c workaround with pure Zig workaround Signed-off-by: Eric Joldasov --- Makefile | 2 +- build.zig | 1 - src/ncurses_refs.c | 27 --------------------------- src/ui.zig | 27 ++++++++++----------------- 4 files changed, 11 insertions(+), 46 deletions(-) delete mode 100644 src/ncurses_refs.c diff --git a/Makefile b/Makefile index 7a77bfd..4da3915 100644 --- a/Makefile +++ b/Makefile @@ -80,7 +80,7 @@ static-%.tar.gz: @# Alternative approach, bypassing zig-build cd static-$* && ${ZIG} build-exe -target $*\ -Iinst/include -Iinst/include/ncursesw -lc inst/lib/libncursesw.a\ - --cache-dir zig-cache -static -fstrip -O ReleaseFast ../src/main.zig ../src/ncurses_refs.c + --cache-dir zig-cache -static -fstrip -O ReleaseFast ../src/main.zig cd static-$* && mv main ncdu && tar -czf ../static-$*.tar.gz ncdu rm -rf static-$* diff --git a/build.zig b/build.zig index c1df229..00c6dbe 100644 --- a/build.zig +++ b/build.zig @@ -51,5 +51,4 @@ pub fn build(b: *std.Build) void { pub fn linkNcurses(compile_step: *std.Build.CompileStep) void { compile_step.linkSystemLibrary("ncursesw"); compile_step.linkLibC(); - compile_step.addCSourceFile(.{ .file = .{ .path = "src/ncurses_refs.c" }, .flags = &.{} }); } diff --git a/src/ncurses_refs.c b/src/ncurses_refs.c deleted file mode 100644 index 40af7d1..0000000 --- a/src/ncurses_refs.c +++ /dev/null @@ -1,27 +0,0 @@ -/* SPDX-FileCopyrightText: 2021-2023 Yoran Heling - * SPDX-License-Identifier: MIT - */ - -#include - -/* Zig @cImport() has problems with the ACS_* macros. Two, in fact: - * - * 1. Naively using the ACS_* macros results in: - * - * error: cannot store runtime value in compile time variable - * return acs_map[NCURSES_CAST(u8, c)]; - * ^ - * That error doesn't make much sense to me, but it might be - * related to https://github.com/ziglang/zig/issues/5344? - * - * 2. The 'acs_map' extern variable isn't being linked correctly? - * Haven't investigated this one deeply enough yet, but attempting - * to dereference acs_map from within Zig leads to a segfault; - * its pointer value doesn't make any sense. - */ -chtype ncdu_acs_ulcorner() { return ACS_ULCORNER; } -chtype ncdu_acs_llcorner() { return ACS_LLCORNER; } -chtype ncdu_acs_urcorner() { return ACS_URCORNER; } -chtype ncdu_acs_lrcorner() { return ACS_LRCORNER; } -chtype ncdu_acs_hline() { return ACS_VLINE ; } -chtype ncdu_acs_vline() { return ACS_HLINE ; } diff --git a/src/ui.zig b/src/ui.zig index 2678994..72b7a4e 100644 --- a/src/ui.zig +++ b/src/ui.zig @@ -192,14 +192,6 @@ test "shorten" { try t("ą́ą́ą́ą́ą́ą́", 5, "ą́...̨́ą́"); // Combining marks, similarly bad. } -// ncurses_refs.c -extern fn ncdu_acs_ulcorner() c.chtype; -extern fn ncdu_acs_llcorner() c.chtype; -extern fn ncdu_acs_urcorner() c.chtype; -extern fn ncdu_acs_lrcorner() c.chtype; -extern fn ncdu_acs_hline() c.chtype; -extern fn ncdu_acs_vline() c.chtype; - const StyleAttr = struct { fg: i16, bg: i16, attr: u32 }; const StyleDef = struct { name: []const u8, @@ -535,20 +527,21 @@ pub const Box = struct { style(.default); if (width < 6 or height < 3) return s; - const ulcorner = ncdu_acs_ulcorner(); - const llcorner = ncdu_acs_llcorner(); - const urcorner = ncdu_acs_urcorner(); - const lrcorner = ncdu_acs_lrcorner(); - const acs_hline = ncdu_acs_hline(); - const acs_vline = ncdu_acs_vline(); + const acs_map = @extern(*[128]c.chtype, .{ .name = "acs_map" }); + const ulcorner = acs_map['l']; + const llcorner = acs_map['m']; + const urcorner = acs_map['k']; + const lrcorner = acs_map['j']; + const acs_hline = acs_map['q']; + const acs_vline = acs_map['x']; var i: u32 = 0; while (i < height) : (i += 1) { s.move(i, 0); - addch(if (i == 0) ulcorner else if (i == height-1) llcorner else acs_hline); - hline(if (i == 0 or i == height-1) acs_vline else ' ', width-2); + addch(if (i == 0) ulcorner else if (i == height-1) llcorner else acs_vline); + hline(if (i == 0 or i == height-1) acs_hline else ' ', width-2); s.move(i, width-1); - addch(if (i == 0) urcorner else if (i == height-1) lrcorner else acs_hline); + addch(if (i == 0) urcorner else if (i == height-1) lrcorner else acs_vline); } s.move(0, 3);