bluetoothctl scan on will stream a series of local bluetooth devices to stdout. Is there a way for me to stream this input in Zig? For example, I assume I'd want to run a separate thread running this command and streaming stdout, splitting the results into a device list and having the main programming doing something and outputting back to the user.
I currently have a small example for spawning the child process but it seems to wait for all output to complete which isn't too helpful. any advice is greatly appreciated.
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var child = std.process.Child.init(&.{ "bluetoothctl", "scan", "on" }, allocator);
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
var stdout = std.ArrayList(u8).init(allocator);
var stderr = std.ArrayList(u8).init(allocator);
errdefer {
stdout.deinit();
stderr.deinit();
}
try child.spawn();
try child.collectOutput(&stdout, &stderr, 4096);
const out = try stdout.toOwnedSlice();
defer allocator.free(out);
std.debug.print("[DEBUG] {s}\n", .{out});
child = std.process.Child.init(&.{ "bluetoothctl", "scan", "off" }, allocator);
try child.spawn();
_ = try child.wait();
}