Repository navigation
Description
What is the problem this feature would solve?
Now that I've had a good look at Terminal with Effect-TS/effect-smol#2185 I've been thinking how to add a resizeObserver to the terminal for truly responsive CLI applications. At the moment if I wanted to do this, I would need to tap into process.stdout.on("resize") in an Effect.sync and manage a Ref or Queue outside of the Terminal implementation
What is the feature you are proposing to solve the problem?
I think the simplest way would be to add a SubscriptionRef to the Terminal which can be used to .get() for the column and row as well as be added to a new size: Stream<{col:number, row:number}, never, never> which could be used to update the render (especially if used with Effect-TS/effect-smol#2205)
Something along the lines of:
const make: (
shouldQuit?: (input: Terminal.UserInput) => boolean,
) => Effect.Effect<Terminal.Terminal, never, Scope.Scope> = Effect.fnUntraced(
function* (
shouldQuit: (input: Terminal.UserInput) => boolean = defaultShouldQuit,
) {
const stdin = process.stdin;
const stdout = process.stdout;
const sizeRef = yield* SubscriptionRef.make<TerminalSize>({
columns: stdout.columns ?? 0,
rows: stdout.rows ?? 0,
});
yield* Effect.acquireRelease(
Effect.sync(() => {
const handler = () =>
Effect.runSync(
SubscriptionRef.set(sizeRef, {
columns: stdout.columns ?? 0,
rows: stdout.rows ?? 0,
})
)
};
stdout.on("resize", handler);
return handler;
}),
(handler) => Effect.sync(() => stdout.off("resize", handler)),
);
const columns = SubscriptionRef.get(sizeRef).pipe(Effect.map((s) => s.columns));
const rows = SubscriptionRef.get(sizeRef).pipe(Effect.map((s) => s.rows));
const size = SubscriptionRef.changes(sizeRef);
return Terminal.make({
size,
columns,
rows,
});
},
);The part I'm less sure about is when the column and row values will get updated with the SubscriptionRef. I guess since its an effect, each time you yield* terminal.column it would get the current point-in-time value from the size so it would still have the current behavior.
What alternatives have you considered?
I quickly looked at if Queue or PubSub would be better data structures for the size, but since SubscriptionRef.change returns a Stream, I figured this was the simplest approach without needing to manage a fiber. Though at the same time maybe the stdout.on should be handled in a .forkEffect() with its own fiber 🤔 This is where my knowledge starts to butt up against what would be a good effect approach for this
Activity