termio: reader => backend

This commit is contained in:
Mitchell Hashimoto
2024-07-15 10:14:05 -07:00
parent dc6dc1d3d2
commit 001a6d2624
6 changed files with 42 additions and 41 deletions

View File

@ -460,7 +460,7 @@ pub fn init(
.padding = padding,
.full_config = config,
.config = try termio.Termio.DerivedConfig.init(alloc, config),
.reader = .{ .exec = io_exec },
.backend = .{ .exec = io_exec },
.writer = io_writer,
.renderer_state = &self.renderer_state,
.renderer_wakeup = render_thread.wakeup,

View File

@ -5,15 +5,15 @@
const stream_handler = @import("termio/stream_handler.zig");
pub usingnamespace @import("termio/message.zig");
pub const reader = @import("termio/reader.zig");
pub const backend = @import("termio/backend.zig");
pub const writer = @import("termio/writer.zig");
pub const Exec = @import("termio/Exec.zig");
pub const Options = @import("termio/Options.zig");
pub const Termio = @import("termio/Termio.zig");
pub const Thread = @import("termio/Thread.zig");
pub const Backend = backend.Backend;
pub const DerivedConfig = Termio.DerivedConfig;
pub const Mailbox = writer.Mailbox;
pub const Reader = reader.Reader;
pub const StreamHandler = stream_handler.StreamHandler;
pub const Writer = writer.Writer;

View File

@ -26,7 +26,7 @@ const windows = internal_os.windows;
const log = std.log.scoped(.io_exec);
/// The subprocess state for our exec reader.
/// The subprocess state for our exec backend.
subprocess: Subprocess,
/// Initialize the exec state. This will NOT start it, this only sets
@ -45,7 +45,7 @@ pub fn deinit(self: *Exec) void {
self.subprocess.deinit();
}
/// Call to initialize the terminal state as necessary for this reader.
/// Call to initialize the terminal state as necessary for this backend.
/// This is called before any termio begins. This should not be called
/// after termio begins because it may put the internal terminal state
/// into a bad state.
@ -121,8 +121,8 @@ pub fn threadEnter(
);
read_thread.setName("io-reader") catch {};
// Setup our threadata reader state to be our own
td.reader = .{ .exec = .{
// Setup our threadata backend state to be our own
td.backend = .{ .exec = .{
.start = process_start,
.abnormal_runtime_threshold_ms = io.config.abnormal_runtime_threshold_ms,
.wait_after_command = io.config.wait_after_command,
@ -136,7 +136,7 @@ pub fn threadEnter(
// Start our process watcher
process.wait(
td.loop,
&td.reader.exec.process_wait_c,
&td.backend.exec.process_wait_c,
termio.Termio.ThreadData,
td,
processExit,
@ -144,8 +144,8 @@ pub fn threadEnter(
}
pub fn threadExit(self: *Exec, td: *termio.Termio.ThreadData) void {
assert(td.reader == .exec);
const exec = &td.reader.exec;
assert(td.backend == .exec);
const exec = &td.backend.exec;
if (exec.exited) self.subprocess.externalExit();
self.subprocess.stop();
@ -282,8 +282,8 @@ fn processExit(
const exit_code = r catch unreachable;
const td = td_.?;
assert(td.reader == .exec);
const execdata = &td.reader.exec;
assert(td.backend == .exec);
const execdata = &td.backend.exec;
execdata.exited = true;
// Determine how long the process was running for.
@ -366,7 +366,7 @@ pub fn queueWrite(
linefeed: bool,
) !void {
_ = self;
const exec = &td.reader.exec;
const exec = &td.backend.exec;
// If our process is exited then we send our surface a message
// about it but we don't queue any more writes.

View File

@ -25,8 +25,8 @@ full_config: *const Config,
/// The derived configuration for this termio implementation.
config: termio.Termio.DerivedConfig,
/// The reader for the terminal.
reader: termio.Reader,
/// The backend for termio that implements where reads/writes are sourced.
backend: termio.Backend,
/// The writer for the terminal. This is how messages are delivered.
/// If you're using termio.Thread this MUST be "mailbox".

View File

@ -34,7 +34,7 @@ const log = std.log.scoped(.io_exec);
alloc: Allocator,
/// This is the implementation responsible for io.
reader: termio.Reader,
backend: termio.Backend,
/// The derived configuration for this termio implementation.
config: DerivedConfig,
@ -168,9 +168,9 @@ pub fn init(self: *Termio, alloc: Allocator, opts: termio.Options) !void {
// Set our default cursor style
term.screen.cursor.cursor_style = opts.config.cursor_style;
// Setup our reader.
var reader = opts.reader;
reader.initTerminal(&term);
// Setup our backend.
var backend = opts.backend;
backend.initTerminal(&term);
// Setup our terminal size in pixels for certain requests.
const screen_size = opts.screen_size.subPadding(opts.padding);
@ -216,7 +216,7 @@ pub fn init(self: *Termio, alloc: Allocator, opts: termio.Options) !void {
.renderer_mailbox = opts.renderer_mailbox,
.surface_mailbox = opts.surface_mailbox,
.grid_size = opts.grid_size,
.reader = opts.reader,
.backend = opts.backend,
.writer = opts.writer,
.terminal_stream = .{
.handler = handler,
@ -232,7 +232,7 @@ pub fn init(self: *Termio, alloc: Allocator, opts: termio.Options) !void {
}
pub fn deinit(self: *Termio) void {
self.reader.deinit();
self.backend.deinit();
self.terminal.deinit(self.alloc);
self.config.deinit();
self.writer.deinit(self.alloc);
@ -258,15 +258,15 @@ pub fn threadEnter(self: *Termio, thread: *termio.Thread, data: *ThreadData) !vo
.writer = &self.writer,
// Placeholder until setup below
.reader = .{ .manual = {} },
.backend = .{ .manual = {} },
};
// Setup our reader
try self.reader.threadEnter(alloc, self, data);
// Setup our backend
try self.backend.threadEnter(alloc, self, data);
}
pub fn threadExit(self: *Termio, data: *ThreadData) void {
self.reader.threadExit(data);
self.backend.threadExit(data);
}
/// Send a message using the writer. Depending on the writer type in
@ -300,7 +300,7 @@ pub inline fn queueWrite(
data: []const u8,
linefeed: bool,
) !void {
try self.reader.queueWrite(self.alloc, td, data, linefeed);
try self.backend.queueWrite(self.alloc, td, data, linefeed);
}
/// Update the configuration.
@ -320,7 +320,7 @@ pub fn changeConfig(self: *Termio, td: *ThreadData, config: *DerivedConfig) !voi
// renderer mutex so this is safe to do despite being executed
// from another thread.
self.terminal_stream.handler.changeConfig(&self.config);
td.reader.changeConfig(&self.config);
td.backend.changeConfig(&self.config);
// Update the configuration that we know about.
//
@ -363,7 +363,7 @@ pub fn resize(
) !void {
// Update the size of our pty.
const padded_size = screen_size.subPadding(padding);
try self.reader.resize(grid_size, padded_size);
try self.backend.resize(grid_size, padded_size);
// Update our cached grid size
self.grid_size = grid_size;
@ -466,7 +466,7 @@ pub fn childExitedAbnormally(self: *Termio, exit_code: u32, runtime_ms: u64) !vo
self.renderer_state.mutex.lock();
defer self.renderer_state.mutex.unlock();
const t = self.renderer_state.terminal;
try self.reader.childExitedAbnormally(self.alloc, t, exit_code, runtime_ms);
try self.backend.childExitedAbnormally(self.alloc, t, exit_code, runtime_ms);
}
/// Process output from the pty. This is the manual API that users can
@ -550,12 +550,12 @@ pub const ThreadData = struct {
/// Mailboxes for different threads
surface_mailbox: apprt.surface.Mailbox,
/// Data associated with the reader implementation (i.e. pty/exec state)
reader: termio.reader.ThreadData,
/// Data associated with the backend implementation (i.e. pty/exec state)
backend: termio.backend.ThreadData,
writer: *termio.Writer,
pub fn deinit(self: *ThreadData) void {
self.reader.deinit(self.alloc);
self.backend.deinit(self.alloc);
self.* = undefined;
}
};

View File

@ -34,19 +34,20 @@ pub const Config = union(Kind) {
exec: termio.Exec.Config,
};
/// Reader implementations
pub const Reader = union(Kind) {
/// Backend implementations. A backend is responsible for owning the pty
/// behavior and providing read/write capabilities.
pub const Backend = union(Kind) {
manual: void,
exec: termio.Exec,
pub fn deinit(self: *Reader) void {
pub fn deinit(self: *Backend) void {
switch (self.*) {
.manual => {},
.exec => |*exec| exec.deinit(),
}
}
pub fn initTerminal(self: *Reader, t: *terminal.Terminal) void {
pub fn initTerminal(self: *Backend, t: *terminal.Terminal) void {
switch (self.*) {
.manual => {},
.exec => |*exec| exec.initTerminal(t),
@ -54,7 +55,7 @@ pub const Reader = union(Kind) {
}
pub fn threadEnter(
self: *Reader,
self: *Backend,
alloc: Allocator,
io: *termio.Termio,
td: *termio.Termio.ThreadData,
@ -65,7 +66,7 @@ pub const Reader = union(Kind) {
}
}
pub fn threadExit(self: *Reader, td: *termio.Termio.ThreadData) void {
pub fn threadExit(self: *Backend, td: *termio.Termio.ThreadData) void {
switch (self.*) {
.manual => {},
.exec => |*exec| exec.threadExit(td),
@ -73,7 +74,7 @@ pub const Reader = union(Kind) {
}
pub fn resize(
self: *Reader,
self: *Backend,
grid_size: renderer.GridSize,
screen_size: renderer.ScreenSize,
) !void {
@ -84,7 +85,7 @@ pub const Reader = union(Kind) {
}
pub fn queueWrite(
self: *Reader,
self: *Backend,
alloc: Allocator,
td: *termio.Termio.ThreadData,
data: []const u8,
@ -97,7 +98,7 @@ pub const Reader = union(Kind) {
}
pub fn childExitedAbnormally(
self: *Reader,
self: *Backend,
gpa: Allocator,
t: *terminal.Terminal,
exit_code: u32,