From c2905dd144cc589a4126cdbd42cbe14e6282e9f3 Mon Sep 17 00:00:00 2001 From: ibro Date: Thu, 18 Apr 2024 14:36:44 +0100 Subject: [PATCH 1/2] add write and writeAsync to Utils.subprocess --- src/utils/exec.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/utils/exec.ts b/src/utils/exec.ts index c5448362..e9a96acd 100644 --- a/src/utils/exec.ts +++ b/src/utils/exec.ts @@ -20,6 +20,7 @@ function proc(arg: Args | string | string[]) { return Gio.Subprocess.new( cmd, + Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE, ); @@ -54,6 +55,11 @@ export function subprocess( ) { const p = proc(argsOrCmd); + const stdin = new Gio.DataOutputStream({ + base_stream: p.get_stdin_pipe(), + close_base_stream: true, + }); + const stdout = new Gio.DataInputStream({ base_stream: p.get_stdout_pipe(), close_base_stream: true, @@ -77,7 +83,26 @@ export function subprocess( readStream(stdout, onOut ?? out); readStream(stderr, onErr ?? err); - return p; + + return Object.assign(p, { + write(str: string): void { + stdin.write_all(new TextEncoder().encode(str), null); + }, + writeAsync(str: string): Promise { + return new Promise((resolve, reject) => { + stdin.write_all_async( + new TextEncoder().encode(str), + GLib.PRIORITY_DEFAULT, + null, + (stdin, res) => { + stdin.write_all_finish(res)[0] + ? resolve() + : reject(); + } + ); + }) + }, + }); } export function exec(args: Args): Out | Err From 4d90999894fe5748001d59c9690619daaa20efca Mon Sep 17 00:00:00 2001 From: ibro Date: Thu, 18 Apr 2024 14:56:04 +0100 Subject: [PATCH 2/2] fix styling --- src/utils/exec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/exec.ts b/src/utils/exec.ts index e9a96acd..2daaa18f 100644 --- a/src/utils/exec.ts +++ b/src/utils/exec.ts @@ -96,11 +96,11 @@ export function subprocess( null, (stdin, res) => { stdin.write_all_finish(res)[0] - ? resolve() - : reject(); - } + ? resolve() + : reject(); + }, ); - }) + }); }, }); }