Skip to content

Commit 9a8a080

Browse files
committed
Expose ChildProcessId from PTY backends and TerminalControl
1 parent 21ea569 commit 9a8a080

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

SharpConsoleUI/Controls/Terminal/IPtyBackend.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ internal interface IPtyBackend : IDisposable
1717

1818
/// <summary>Write keyboard/mouse bytes to the PTY.</summary>
1919
void Write(byte[] buf, int count);
20+
21+
/// <summary>The OS process ID of the child process running inside the PTY.</summary>
22+
int ChildProcessId { get; }
2023
}

SharpConsoleUI/Controls/Terminal/LinuxPtyBackend.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public LinuxPtyBackend(string exe, string[]? args, int rows, int cols, string? w
3333
PtyNative.close(slave); // parent closes its copy of the slave fd
3434
}
3535

36+
public int ChildProcessId => _shimProc.Id;
37+
3638
public int Read(byte[] buf, int count) => PtyNative.read(_masterFd, buf, count);
3739

3840
public void Write(byte[] buf, int count) => PtyNative.write(_masterFd, buf, count);

SharpConsoleUI/Controls/Terminal/TerminalControl.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public sealed class TerminalControl
3939
/// <summary>Window title derived from the launched executable name.</summary>
4040
public string Title { get; }
4141

42+
/// <summary>The OS process ID of the child process running inside the PTY.</summary>
43+
public int ProcessId => _pty.ChildProcessId;
44+
4245
/// <summary>Raised on the PTY read thread when the spawned process exits.</summary>
4346
public event EventHandler? ProcessExited;
4447

SharpConsoleUI/Controls/Terminal/WindowsPtyBackend.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal sealed class WindowsPtyBackend : IPtyBackend
1616
{
1717
private IntPtr _hPcon = IntPtr.Zero;
1818
private IntPtr _hProcess = IntPtr.Zero;
19+
private int _processId;
1920
private Stream? _inputStream; // parent writes keyboard input here
2021
private Stream? _outputStream; // parent reads terminal output from here
2122
private int _disposed = 0;
@@ -91,6 +92,7 @@ public WindowsPtyBackend(string exe, string[]? args, int rows, int cols, string?
9192
}
9293

9394
_hProcess = pi.hProcess;
95+
_processId = pi.dwProcessId;
9496
WinPtyNative.CloseHandle(pi.hThread); // we don't need the thread handle
9597
}
9698
finally
@@ -102,6 +104,8 @@ public WindowsPtyBackend(string exe, string[]? args, int rows, int cols, string?
102104

103105
// ── IPtyBackend ──────────────────────────────────────────────────────────
104106

107+
public int ChildProcessId => _processId;
108+
105109
public int Read(byte[] buf, int count)
106110
{
107111
if (_outputStream == null) return 0;

0 commit comments

Comments
 (0)