Skip to content

Commit

Permalink
Fixed invalid cursor position (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Aug 5, 2021
1 parent 4701c69 commit 2288bf2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
14 changes: 1 addition & 13 deletions Sharprompt/Drivers/DefaultConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion Sharprompt/Forms/ConfirmForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions Sharprompt/Forms/FormRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public void Render(Action<OffscreenBuffer> template)
{
template(_offscreenBuffer);

_offscreenBuffer.PushCursor();

if (ErrorMessage != null)
{
_offscreenBuffer.WriteErrorMessage(ErrorMessage);
Expand Down
6 changes: 5 additions & 1 deletion Sharprompt/Forms/InputForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Sharprompt/Forms/ListForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions Sharprompt/Forms/PasswordForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions Sharprompt/Internal/OffscreenBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

using Sharprompt.Drivers;

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 2288bf2

Please sign in to comment.