#Terminal dimensions (width and height)

1 messages · Page 1 of 1 (latest)

bleak sable
#

I am trying out Zig for the first time and I want to create spinning cube in the terminal but I can't find a way to get the dimensions of the terminal (so that the cube would be the right size). I would like to do it with no library if possible. (Any tips or any I would need to know is always appreciated 🙂 )

hard spoke
#

Hey! I’m away from my computer so I can’t give super detailed instructions. I’ve done this before though. Try looking at my term.zig file for my fuzzy finder. https://github.com/natecraddock/zf/blob/master/src/term.zig. The important parts are opening the /dev/tty file and the windowSize function.

languid cliff
#

Ah, read it, no, it would not, ok ok

bleak sable
languid cliff
#

Oki, This is a side I have touched a little

#

first

#

You will have to use at the bare minimum windows libs

#

since those are the ones that defines that

#

So you have to link kernel32 at the bare minimum

#

Now "link" I say, but really is just getting the functions you will use

#

now they are in C as you can see

#

There are a few functions under std.os.windows.kernel32 too

#

But you said no libs, so I guess std counts @bleak sable ?

#

to get a external function (For example AllocConsole) we have:


extern "kernel32" fn AllocConsole() void;
bleak sable
languid cliff
#

ye

#

ye

bleak sable
#

Thanks for the help. I will have a look. I am new to low level programming so it is a lot to take in.

languid cliff
#

ye ye

#

With std give me a second

languid cliff
#
pub fn wWinMain(hInstance: std.os.windows.HINSTANCE, hPrevInstance: std.os.windows.HINSTANCE, lpCmdLine: std.os.windows.LPWSTR, nCmdShow: c_int) c_int {
        _ = hInstance;
        _ = hPrevInstance;
        _ = lpCmdLine;
        _ = nCmdShow;
        var info : std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
        var current_terminal_handler = std.os.windows.kernel32.GetStdHandle(std.os.windows.STD_OUTPUT_HANDLE) orelse return 2;  
        if(
        std.os.windows.kernel32.GetConsoleScreenBufferInfo(current_terminal_handler, &info) == 0
        ) {
            return 1;
        }
        std.log.err("size is x = {}, y = {}", .{info.dwSize.X, info.dwSize.Y});
        std.log.err("cursor is x = {}, y = {}", .{info.dwCursorPosition.X, info.dwCursorPosition.Y});
        std.log.err("attributes is {}", .{info.wAttributes});
        return 0;

}

Not on windows can't test

#

i mean

#

wine ig+

#

Something along those lines

bleak sable
#

doesnt compile but I think understand your code

languid cliff
# bleak sable doesnt compile but I think understand your code
const std = @import("std");

pub fn wWinMain(hInstance: ?std.os.windows.HINSTANCE, hPrevInstance: ?std.os.windows.HINSTANCE, lpCmdLine: std.os.windows.LPWSTR, nCmdShow: c_int) c_int {
        _ = hInstance;
        _ = hPrevInstance;
        _ = lpCmdLine;
        _ = nCmdShow;
        var info : std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
        var current_terminal_handler = std.os.windows.kernel32.GetStdHandle(std.os.windows.STD_OUTPUT_HANDLE) orelse return 2;  
        if(
        std.os.windows.kernel32.GetConsoleScreenBufferInfo(current_terminal_handler, &info) == 0
        ) {
            return 1;
        }
        std.log.err("size is x = {}, y = {}", .{info.dwSize.X, info.dwSize.Y});
        std.log.err("cursor is x = {}, y = {}", .{info.dwCursorPosition.X, info.dwCursorPosition.Y});
        std.log.err("attributes is {}", .{info.wAttributes});
        return 0;

}

#

corrected

pliant sluice
#

Is wWinMain the entry point?

languid cliff
#

yes

#

You can use wWinMain and main