Global reference to Writer

Super noob question: I want to declare a reference to stdOut within a zig file. I tried var stdout = std.io.getStdOut().writer(); at the top level but that doesn’t work:
debug: failed to determine terminal size; using conservative guess 80x25
Sorry for the very basic question - just starting out :o)

The message is from the Progress bar and is related to your terminal/console.
If you don’t use the std library Progress, then it is displayed from the zig command.

Thank you! That explains the message about the terminal size - I obviously misunderstood that.

Regarding the reference to stdout - it appears that calling getStdOut at the global level is not valid (not a comptime expression?) So how should I make a global variable to assign std.io.getStdOut().writer() ?

To fix this you can initialize the value at runtime, e.g. as the first thing in the main:

var stdout: PutTheWriterTypeHere = undefined;

pub fn main() void {
    stdout = std.io.getStdOut().writer();
}
1 Like

Thanks!

This is what I’m getting:

$ zig build
debug: failed to determine terminal size; using conservative guess 80x25
install
└─ install sdlgame
   └─ zig build-exe sdlgame Debug native 1 errors
src\sdlgame.zig:7:30: error: expected type 'type', found 'fn (comptime type, comptime type, comptime anytype) type'
var stdout2: std.io.Writer = undefined;

I presume I should be using some type other than std.io.Writer, but I don’t know how to find out what the type should be. I’ve tried AnyWriter and GenericWriter which are Writer names in io.zig

Found an answer to my question here: How do I pass a stream or writer parameter to a function in Zig? - Stack Overflow

If in doubt you can also always discover the exact type using @compileLog:

@compileLog(@TypeOf(std.io.getStdOut().writer()));
1 Like

That’s awesome - thank you