From dcfc45c8d2775d883a372c60f8abc31848ee03d4 Mon Sep 17 00:00:00 2001 From: owl Date: Sat, 14 Oct 2023 19:26:29 +0200 Subject: [PATCH] move term junk to its own file --- src/main.zig | 31 +++---------------------------- src/term.zig | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/main.zig b/src/main.zig index b268f53..76d77b9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ const std = @import("std"); const color = @import("color.zig"); const build_options = @import("build-options"); const ziglyph = @import("ziglyph"); +const term = @import("term.zig"); var cat = struct { // options @@ -46,7 +47,7 @@ const parsers = .{ fn init() !void { if (std.io.getStdOut().isTty()) { - cat.max_width = @max(try getTermWidth(), 2) - 1; + cat.max_width = @max(try term.getTermWidth(), 2) - 1; } cat.pos.mod = @intCast(num_color_entries / cat.freq); @@ -65,32 +66,6 @@ fn init() !void { } } -fn getTermWidth() !u16 { - return switch (@import("builtin").target.os.tag) { - .windows => getTermWidthWindows(), - else => getTermWidthIoctl(), - }; -} - -fn getTermWidthIoctl() !u16 { - var termsz: std.os.system.winsize = undefined; - if (-1 == std.os.system.ioctl(1, std.os.system.T.IOCGWINSZ, @intFromPtr(&termsz))) - return error.IoCtl; - return termsz.ws_col; -} - -fn getTermWidthWindows() !u16 { - const win = std.os.windows; - var csbi: win.CONSOLE_SCREEN_BUFFER_INFO = undefined; - const stdout_h = try win.GetStdHandle(win.STD_OUTPUT_HANDLE); - // https://learn.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo#return-value - if (0 == win.kernel32.GetConsoleScreenBufferInfo(stdout_h, &csbi)) - return error.JustWindowsThings; - const columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; - // it's signed? you can have negative columns? - return @intCast(columns); -} - pub fn main() !void { @setFloatMode(std.builtin.FloatMode.Optimized); var diag = clap.Diagnostic{}; @@ -348,7 +323,7 @@ test "it counts grapheme clusters correctly" { test "term width on windows?" { const win = std.os.windows; - const w = getTermWidth() catch { + const w = term.getTermWidth() catch { const eno = win.kernel32.GetLastError(); _ = eno; //std.log.err("last error: {d}", .{eno}); diff --git a/src/term.zig b/src/term.zig index e69de29..8aca864 100644 --- a/src/term.zig +++ b/src/term.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +pub fn getTermWidth() !u16 { + return switch (@import("builtin").target.os.tag) { + .windows => getTermWidthWindows(), + else => getTermWidthIoctl(), + }; +} + +fn getTermWidthIoctl() !u16 { + var termsz: std.os.system.winsize = undefined; + if (-1 == std.os.system.ioctl(1, std.os.system.T.IOCGWINSZ, @intFromPtr(&termsz))) + return error.IoCtl; + return termsz.ws_col; +} + +fn getTermWidthWindows() !u16 { + const win = std.os.windows; + const stdout_h = try win.GetStdHandle(win.STD_OUTPUT_HANDLE); + var csbi: win.CONSOLE_SCREEN_BUFFER_INFO = undefined; + + // https://learn.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo#return-value + if (0 == win.kernel32.GetConsoleScreenBufferInfo(stdout_h, &csbi)) + return error.JustWindowsThings; + + const columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; + // it's signed? you can have negative columns? + return @intCast(columns); +} -- 2.45.2