__zigar.redirect(name, stream) - chung-leong/zigar GitHub Wiki

Redirect a standard stream to another stream.

Possible values for name:

  • stdin
  • stdout
  • stderr
  • root

Usage:

const c = @cImport({
    @cInclude("stdio.h");
});

pub fn scan() ?struct {
    first: i32,
    second: i32,
    third: f64,
} {
    var num1: i32 = undefined;
    var num2: i32 = undefined;
    var num3: f64 = undefined;
    if (c.scanf("%d, %d, %lf", &num1, &num2, &num3) != 3) return null;
    return .{ .first = num1, .second = num2, .third = num3 };
}
import { __zigar, scan } from './redirect-example-1.zig';
const { redirect } = __zigar;

redirect('stdin', '1234, 4567, 3.1416');
console.log(scan().valueOf());
{ first: 1234, second: 4567, third: 3.1416 }

Note:

When name is stdin, stdout, or stderr, stream should be an object that implements the file interface or something that gets automatically converted into one like a string or an array. When name is root, stream should be an object that implements the directory interface or a Map.

Redirecting the root directory only has the effect of changing target from null to the given object when an absolute path is used to reference a file or directory.


File interface | Directory interface