From e06f061459f8982b22d79bc5e249d2baf4e7bde9 Mon Sep 17 00:00:00 2001 From: Szymon Kaliski Date: Wed, 31 Jan 2024 11:17:57 +0100 Subject: [PATCH] more readable size using an object with named fields --- index.d.ts | 8 +++++-- index.test.ts | 61 ++++++++++++++++++++++++++++++++++++++++----------- src/lib.rs | 21 ++++++++++-------- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4643087..12f8396 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,9 +3,13 @@ /* auto-generated by NAPI-RS */ +export interface Size { + cols: number + rows: number +} export class Pty { fd: number pid: number - constructor(command: string, args: Array, envs: Record, dir: string, size: [cols: number, rows: number], onExit: (err: null | Error, exitCode: number) => void) - resize(size: [cols: number, rows: number]): void + constructor(command: string, args: Array, envs: Record, dir: string, size: Size, onExit: (err: null | Error, exitCode: number) => void) + resize(size: Size): void } diff --git a/index.test.ts b/index.test.ts index 9988cfc..6ad32e0 100644 --- a/index.test.ts +++ b/index.test.ts @@ -12,7 +12,7 @@ describe('PTY', () => { [message], {}, CWD, - [80, 24], + { rows: 24, cols: 80 }, (err, exitCode) => { expect(err).toBeNull(); expect(exitCode).toBe(0); @@ -33,7 +33,7 @@ describe('PTY', () => { ['-c', 'exit 17'], {}, CWD, - [80, 24], + { rows: 24, cols: 80 }, (err, exitCode) => { expect(err).toBeNull(); expect(exitCode).toBe(17); @@ -45,7 +45,14 @@ describe('PTY', () => { test('can be written to', (done) => { const message = 'hello cat'; - const pty = new Pty('/bin/cat', [], {}, CWD, [80, 24], () => {}); + const pty = new Pty( + '/bin/cat', + [], + {}, + CWD, + { rows: 24, cols: 80 }, + () => {}, + ); const readStream = fs.createReadStream('', { fd: pty.fd }); const writeStream = fs.createWriteStream('', { fd: pty.fd }); @@ -59,7 +66,14 @@ describe('PTY', () => { }); test('can be resized', (done) => { - const pty = new Pty('/bin/sh', [], {}, CWD, [80, 24], () => {}); + const pty = new Pty( + '/bin/sh', + [], + {}, + CWD, + { rows: 24, cols: 80 }, + () => {}, + ); const readStream = fs.createReadStream('', { fd: pty.fd }); const writeStream = fs.createWriteStream('', { fd: pty.fd }); @@ -71,7 +85,7 @@ describe('PTY', () => { if (buffer.includes('done1\r\n')) { expect(buffer).toContain('24 80'); - pty.resize([100, 60]); + pty.resize({ rows: 60, cols: 100 }); buffer = ''; writeStream.write("stty size; echo 'done2'\n"); } @@ -86,11 +100,18 @@ describe('PTY', () => { }); test('respects working directory', (done) => { - const pty = new Pty('/bin/pwd', [], {}, CWD, [80, 24], (err, exitCode) => { - expect(err).toBeNull(); - expect(exitCode).toBe(0); - done(); - }); + const pty = new Pty( + '/bin/pwd', + [], + {}, + CWD, + { rows: 24, cols: 80 }, + (err, exitCode) => { + expect(err).toBeNull(); + expect(exitCode).toBe(0); + done(); + }, + ); const readStream = fs.createReadStream('', { fd: pty.fd }); @@ -110,7 +131,7 @@ describe('PTY', () => { ENV_VARIABLE: message, }, CWD, - [80, 24], + { rows: 24, cols: 80 }, (err, exitCode) => { expect(err).toBeNull(); expect(exitCode).toBe(0); @@ -130,7 +151,14 @@ describe('PTY', () => { test('works with Bun.read & Bun.write', (done) => { const message = 'hello bun'; - const pty = new Pty('/bin/cat', [], {}, CWD, [80, 24], () => {}); + const pty = new Pty( + '/bin/cat', + [], + {}, + CWD, + { rows: 24, cols: 80 }, + () => {}, + ); const file = Bun.file(pty.fd); @@ -150,7 +178,14 @@ describe('PTY', () => { test("doesn't break when executing non-existing binary", (done) => { try { - new Pty('/bin/this-does-not-exist', [], {}, CWD, [80, 24], () => {}); + new Pty( + '/bin/this-does-not-exist', + [], + {}, + CWD, + { rows: 24, cols: 80 }, + () => {}, + ); } catch (e) { expect(e.message).toContain('No such file or directory'); diff --git a/src/lib.rs b/src/lib.rs index cd204d2..a7ff449 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,12 @@ struct Pty { pub pid: u32, } +#[napi(object)] +struct Size { + pub cols: u16, + pub rows: u16, +} + #[allow(dead_code)] fn set_controlling_terminal(fd: c_int) -> Result<(), Error> { let res = unsafe { @@ -76,12 +82,12 @@ impl Pty { args: Vec, envs: HashMap, dir: String, - #[napi(ts_arg_type = "[cols: number, rows: number]")] size: (u16, u16), + size: Size, #[napi(ts_arg_type = "(err: null | Error, exitCode: number) => void")] on_exit: JsFunction, ) -> Result { let window_size = Winsize { - ws_col: size.0, - ws_row: size.1, + ws_col: size.cols, + ws_row: size.rows, ws_xpixel: 0, ws_ypixel: 0, }; @@ -195,13 +201,10 @@ impl Pty { #[napi] #[allow(dead_code)] - pub fn resize( - &mut self, - #[napi(ts_arg_type = "[cols: number, rows: number]")] size: (u16, u16), - ) -> Result<(), NAPI_ERROR> { + pub fn resize(&mut self, size: Size) -> Result<(), NAPI_ERROR> { let window_size = Winsize { - ws_col: size.0, - ws_row: size.1, + ws_col: size.cols, + ws_row: size.rows, ws_xpixel: 0, ws_ypixel: 0, };