diff --git a/Sharprompt/Drivers/DefaultConsoleDriver.cs b/Sharprompt/Drivers/DefaultConsoleDriver.cs index 53e5dd2..35206a7 100644 --- a/Sharprompt/Drivers/DefaultConsoleDriver.cs +++ b/Sharprompt/Drivers/DefaultConsoleDriver.cs @@ -66,19 +66,7 @@ public void Write(string value, ConsoleColor color) public void WriteLine() => Console.WriteLine(); - public void SetCursorPosition(int left, int top) - { - if (top < 0) - { - top = 0; - } - else if (top >= Console.BufferHeight) - { - top = Console.BufferHeight - 1; - } - - Console.SetCursorPosition(left, top); - } + public void SetCursorPosition(int left, int top) => Console.SetCursorPosition(left, top); public bool KeyAvailable => Console.KeyAvailable; diff --git a/Sharprompt/Forms/ConfirmForm.cs b/Sharprompt/Forms/ConfirmForm.cs index 772fbff..d0dc803 100644 --- a/Sharprompt/Forms/ConfirmForm.cs +++ b/Sharprompt/Forms/ConfirmForm.cs @@ -106,7 +106,11 @@ protected override void InputTemplate(OffscreenBuffer offscreenBuffer) offscreenBuffer.Write(_options.DefaultValue.Value ? "(Y/n) " : "(y/N) "); } - offscreenBuffer.Write(_inputBuffer.ToString()); + offscreenBuffer.Write(_inputBuffer.ToString(0, _startIndex)); + + offscreenBuffer.PushCursor(); + + offscreenBuffer.Write(_inputBuffer.ToString(_startIndex, _inputBuffer.Length - _startIndex)); } protected override void FinishTemplate(OffscreenBuffer offscreenBuffer, bool result) diff --git a/Sharprompt/Forms/FormRenderer.cs b/Sharprompt/Forms/FormRenderer.cs index 3fce2c7..1231cd6 100644 --- a/Sharprompt/Forms/FormRenderer.cs +++ b/Sharprompt/Forms/FormRenderer.cs @@ -24,8 +24,6 @@ public void Render(Action template) { template(_offscreenBuffer); - _offscreenBuffer.PushCursor(); - if (ErrorMessage != null) { _offscreenBuffer.WriteErrorMessage(ErrorMessage); diff --git a/Sharprompt/Forms/InputForm.cs b/Sharprompt/Forms/InputForm.cs index 23fa4a8..d9800b9 100644 --- a/Sharprompt/Forms/InputForm.cs +++ b/Sharprompt/Forms/InputForm.cs @@ -113,7 +113,11 @@ protected override void InputTemplate(OffscreenBuffer offscreenBuffer) offscreenBuffer.Write($"({_defaultValue.Value}) "); } - offscreenBuffer.Write(_inputBuffer.ToString()); + offscreenBuffer.Write(_inputBuffer.ToString(0, _startIndex)); + + offscreenBuffer.PushCursor(); + + offscreenBuffer.Write(_inputBuffer.ToString(_startIndex, _inputBuffer.Length - _startIndex)); } protected override void FinishTemplate(OffscreenBuffer offscreenBuffer, T result) diff --git a/Sharprompt/Forms/ListForm.cs b/Sharprompt/Forms/ListForm.cs index 2517f0a..10999d4 100644 --- a/Sharprompt/Forms/ListForm.cs +++ b/Sharprompt/Forms/ListForm.cs @@ -128,10 +128,12 @@ protected override void InputTemplate(OffscreenBuffer offscreenBuffer) { offscreenBuffer.WritePrompt(_options.Message); - offscreenBuffer.Write(_inputBuffer.ToString()); + offscreenBuffer.Write(_inputBuffer.ToString(0, _startIndex)); offscreenBuffer.PushCursor(); + offscreenBuffer.Write(_inputBuffer.ToString(_startIndex, _inputBuffer.Length - _startIndex)); + foreach (var inputItem in _inputItems) { offscreenBuffer.WriteLine(); diff --git a/Sharprompt/Forms/PasswordForm.cs b/Sharprompt/Forms/PasswordForm.cs index aabc05d..419525d 100644 --- a/Sharprompt/Forms/PasswordForm.cs +++ b/Sharprompt/Forms/PasswordForm.cs @@ -57,6 +57,8 @@ protected override void InputTemplate(OffscreenBuffer offscreenBuffer) { offscreenBuffer.WritePrompt(_options.Message); offscreenBuffer.Write(new string('*', _inputBuffer.Length)); + + offscreenBuffer.PushCursor(); } protected override void FinishTemplate(OffscreenBuffer offscreenBuffer, string result) diff --git a/Sharprompt/Internal/OffscreenBuffer.cs b/Sharprompt/Internal/OffscreenBuffer.cs index 21f6154..f1a2063 100644 --- a/Sharprompt/Internal/OffscreenBuffer.cs +++ b/Sharprompt/Internal/OffscreenBuffer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using Sharprompt.Drivers; @@ -71,11 +72,6 @@ public void WriteErrorMessage(string errorMessage) public void PushCursor() { - if (_pushedCursor != null) - { - return; - } - _pushedCursor = new Cursor { Left = _outputBuffer.Last().Sum(x => x.Width), @@ -134,8 +130,13 @@ public void ClearBuffer() private void RequestCancellation() { - _consoleDriver.SetCursorPosition(0, _cursorBottom); _consoleDriver.Reset(); + _consoleDriver.SetCursorPosition(0, _cursorBottom); + + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + _consoleDriver.WriteLine(); + } Environment.Exit(1); }