Skip to content

Commit

Permalink
more readable size using an object with named fields
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonkaliski committed Jan 31, 2024
1 parent 852d90e commit e06f061
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
8 changes: 6 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, envs: Record<string, string>, 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<string>, envs: Record<string, string>, dir: string, size: Size, onExit: (err: null | Error, exitCode: number) => void)
resize(size: Size): void
}
61 changes: 48 additions & 13 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('PTY', () => {
[message],
{},
CWD,
[80, 24],
{ rows: 24, cols: 80 },
(err, exitCode) => {
expect(err).toBeNull();
expect(exitCode).toBe(0);
Expand All @@ -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);
Expand All @@ -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 });
Expand All @@ -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 });
Expand All @@ -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");
}
Expand All @@ -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 });

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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');

Expand Down
21 changes: 12 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,12 +82,12 @@ impl Pty {
args: Vec<String>,
envs: HashMap<String, String>,
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<Self, 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,
};
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit e06f061

Please sign in to comment.