Skip to content

Commit

Permalink
Expose a SystemVirtualTerminal.DangerousRestoreSettings method.
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
alexrp committed Dec 6, 2021
1 parent c4a67f5 commit 443eff9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/Drivers/TerminalDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public void DisableRawMode()
lock (_rawLock)
SetRawMode(IsRawMode = false);
}

public abstract void RestoreSettings();
}

abstract class TerminalDriver<THandle> : TerminalDriver
Expand Down
6 changes: 6 additions & 0 deletions src/core/Drivers/Unix/LinuxTerminalDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ protected override void SetRawModeCore(bool raw, bool flush)
throw new TerminalException($"Could not change raw mode setting: {new Win32Exception().Message}");
}

public override void RestoreSettings()
{
if (_original is termios tios)
_ = tcsetattr(TerminalOut.Handle, TCSAFLUSH, tios);
}

public override int OpenTerminalHandle(string name)
{
return open(name, O_RDWR | O_NOCTTY | O_CLOEXEC);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Drivers/Unix/MacOSTerminalDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ protected override void SetRawModeCore(bool raw, bool flush)
throw new TerminalException($"Could not change raw mode setting: {new Win32Exception().Message}");
}

public override void RestoreSettings()
{
if (_original is termios tios)
_ = tcsetattr(TerminalOut.Handle, TCSAFLUSH, tios);
}

public override int OpenTerminalHandle(string name)
{
return open(name, O_RDWR | O_NOCTTY | O_CLOEXEC);
Expand Down
9 changes: 9 additions & 0 deletions src/core/Drivers/Windows/WindowsTerminalDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ protected override void SetRawMode(bool raw)
SetRawModeCore(raw, true);
}

public override void RestoreSettings()
{
if (_original is var (inMode, outMode))
{
_ = SetConsoleMode(TerminalIn.Handle, inMode);
_ = SetConsoleMode(TerminalOut.Handle, outMode);
}
}

public override unsafe bool IsHandleValid(SafeHandle handle, bool write)
{
if (handle.IsInvalid)
Expand Down
9 changes: 9 additions & 0 deletions src/core/SystemVirtualTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ public override void DisableRawMode()
{
_driver.DisableRawMode();
}

[EditorBrowsable(EditorBrowsableState.Never)]
public void DangerousRestoreSettings()
{
// This method is named as it is and hidden from IntelliSense because restoring the original terminal settings
// and then calling any I/O method in our API could have completely unpredictable results. Nevertheless, we
// expose the method for people who know what they are doing.
_driver.RestoreSettings();
}
}

0 comments on commit 443eff9

Please sign in to comment.