2023-11-06 15:36:41 -05:00
|
|
|
/**
|
|
|
|
* Wrap the runtime-specific hook into stdin
|
|
|
|
*/
|
2023-11-08 17:02:59 -05:00
|
|
|
import { ITerminalIO, ITerminalSize } from '../common/mod.ts';
|
2023-11-08 15:53:14 -05:00
|
|
|
|
2023-11-09 10:46:12 -05:00
|
|
|
function getSizeFromTput(): ITerminalSize {
|
|
|
|
const rows = parseInt(
|
|
|
|
Bun.spawnSync(['tput', 'lines']).stdout.toString().trim(),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
const cols = parseInt(
|
|
|
|
Bun.spawnSync(['tput', 'cols']).stdout.toString().trim(),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
rows: (rows > 0) ? rows : 25,
|
|
|
|
cols: (cols > 0) ? cols : 80,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:07:34 -05:00
|
|
|
const BunTerminalIO: ITerminalIO = {
|
|
|
|
inputLoop: async function* inputLoop() {
|
|
|
|
for await (const chunk of Bun.stdin.stream()) {
|
|
|
|
yield chunk;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getSize: function getSize(): ITerminalSize {
|
2023-11-09 10:46:12 -05:00
|
|
|
return getSizeFromTput();
|
2023-11-08 18:07:34 -05:00
|
|
|
},
|
|
|
|
write: async function write(s: string): Promise<void> {
|
|
|
|
const buffer = new TextEncoder().encode(s);
|
2023-11-08 17:02:59 -05:00
|
|
|
|
2023-11-08 18:07:34 -05:00
|
|
|
await Bun.write(Bun.stdout, buffer);
|
|
|
|
},
|
2023-11-08 15:53:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default BunTerminalIO;
|