From 6613ae0f8f6843b867af72c1331fd2567aa72ab9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Apr 2022 21:57:52 -0700 Subject: [PATCH] libuv: starting Tty impl --- src/Command.zig | 2 +- src/libuv/Tty.zig | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/libuv/main.zig | 2 ++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/libuv/Tty.zig diff --git a/src/Command.zig b/src/Command.zig index 264c20af7..3d344459f 100644 --- a/src/Command.zig +++ b/src/Command.zig @@ -295,7 +295,7 @@ test "Command: pre exec" { .path = "/usr/bin/env", .args = &.{ "/usr/bin/env", "--version" }, .pre_exec = (struct { - fn do() void { + fn do(_: *Command) void { // This runs in the child, so we can exit and it won't // kill the test runner. os.exit(42); diff --git a/src/libuv/Tty.zig b/src/libuv/Tty.zig new file mode 100644 index 000000000..0887a0503 --- /dev/null +++ b/src/libuv/Tty.zig @@ -0,0 +1,46 @@ +//! Tty handles represent a stream for the console. +const Tty = @This(); + +const std = @import("std"); +const fd_t = std.os.fd_t; +const Allocator = std.mem.Allocator; +const testing = std.testing; +const c = @import("c.zig"); +const errors = @import("error.zig"); +const Loop = @import("Loop.zig"); +const Handle = @import("handle.zig").Handle; +const Pty = @import("../Pty.zig"); + +handle: *c.uv_tty_t, + +pub usingnamespace Handle(Tty); + +pub fn init(alloc: Allocator, loop: Loop, fd: fd_t) !Tty { + var tty = try alloc.create(c.uv_tty_t); + errdefer alloc.destroy(tty); + try errors.convertError(c.uv_tty_init(loop.loop, tty, fd, 0)); + return Tty{ .handle = tty }; +} + +pub fn deinit(self: *Tty, alloc: Allocator) void { + alloc.destroy(self.handle); + self.* = undefined; +} + +test "Tty" { + var pty = try Pty.open(.{ + .ws_row = 20, + .ws_col = 80, + .ws_xpixel = 0, + .ws_ypixel = 0, + }); + defer pty.deinit(); + + var loop = try Loop.init(testing.allocator); + defer loop.deinit(testing.allocator); + var tty = try init(testing.allocator, loop, pty.slave); + defer tty.deinit(testing.allocator); + + tty.close(null); + _ = try loop.run(.default); +} diff --git a/src/libuv/main.zig b/src/libuv/main.zig index c6c65f00b..1ac34de4d 100644 --- a/src/libuv/main.zig +++ b/src/libuv/main.zig @@ -1,6 +1,7 @@ pub const Loop = @import("Loop.zig"); pub const Async = @import("Async.zig"); pub const Timer = @import("Timer.zig"); +pub const Tty = @import("Tty.zig"); pub const Sem = @import("Sem.zig"); pub const Thread = @import("Thread.zig"); pub const Error = @import("error.zig").Error; @@ -13,6 +14,7 @@ test { _ = Loop; _ = Async; _ = Timer; + _ = Tty; _ = Sem; _ = Thread; _ = Error;