Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of terminal resizing #254

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sharprompt.Tests/PropertyMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Basic()
var metadata = PropertyMetadataFactory.Create(new BasicModel());

Assert.NotNull(metadata);
Assert.Equal(1, metadata.Count);
Assert.Single(metadata);

Assert.Equal(typeof(string), metadata[0].Type);
Assert.Equal(FormType.Input, metadata[0].DetermineFormType());
Expand All @@ -27,7 +27,7 @@ public void Basic()
Assert.False(metadata[0].IsCollection);
Assert.Null(metadata[0].DefaultValue);
Assert.Null(metadata[0].Order);
Assert.Equal(1, metadata[0].Validators.Count);
Assert.Single(metadata[0].Validators);
}

[Fact]
Expand All @@ -36,7 +36,7 @@ public void Basic_DefaultValue()
var metadata = PropertyMetadataFactory.Create(new BasicModel { Value = "sample" });

Assert.NotNull(metadata);
Assert.Equal(1, metadata.Count);
Assert.Single(metadata);

Assert.Equal(typeof(string), metadata[0].Type);
Assert.Equal("sample", metadata[0].DefaultValue);
Expand Down Expand Up @@ -182,7 +182,7 @@ public void BindIgnore()
var metadata = PropertyMetadataFactory.Create(new BindIgnoreModel());

Assert.NotNull(metadata);
Assert.Equal(1, metadata.Count);
Assert.Single(metadata);
}

[Fact]
Expand All @@ -191,7 +191,7 @@ public void ReadOnly()
var metadata = PropertyMetadataFactory.Create(new ReadOnlyModel());

Assert.NotNull(metadata);
Assert.Equal(1, metadata.Count);
Assert.Single(metadata);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions Sharprompt.Tests/Sharprompt.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Sharprompt/Drivers/DefaultConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ConsoleKeyInfo ReadKey()
{
var keyInfo = Console.ReadKey(true);

if (keyInfo.Key == ConsoleKey.C && keyInfo.Modifiers == ConsoleModifiers.Control)
if (keyInfo is { Key: ConsoleKey.C, Modifiers: ConsoleModifiers.Control })
{
CancellationCallback.Invoke();
}
Expand Down
2 changes: 2 additions & 0 deletions Sharprompt/Forms/FormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected FormBase()

protected Dictionary<ConsoleKey, Func<ConsoleKeyInfo, bool>> KeyHandlerMaps { get; set; } = new();

protected int Width => _consoleDriver.WindowWidth;

protected int Height => _consoleDriver.WindowHeight;

public void Dispose() => _formRenderer.Dispose();
Expand Down
2 changes: 2 additions & 0 deletions Sharprompt/Forms/MultiSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public MultiSelectForm(MultiSelectOptions<T> options)

protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
{
_paginator.UpdatePageSize(Math.Min(_options.PageSize, Height - 2));

offscreenBuffer.WritePrompt(_options.Message);
offscreenBuffer.Write(_paginator.FilterKeyword);

Expand Down
2 changes: 2 additions & 0 deletions Sharprompt/Forms/SelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public SelectForm(SelectOptions<T> options)

protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
{
_paginator.UpdatePageSize(Math.Min(_options.PageSize, Height - 2));

offscreenBuffer.WritePrompt(_options.Message);
offscreenBuffer.Write(_paginator.FilterKeyword);

Expand Down
30 changes: 17 additions & 13 deletions Sharprompt/Internal/OffscreenBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,28 @@ public void RenderToConsole()

_cursorBottom = _consoleDriver.CursorTop;

if (_pushedCursor is not null)
if (_pushedCursor is null)
{
return;
}

var physicalLeft = _pushedCursor.Left % _consoleDriver.BufferWidth;
var physicalTop = _pushedCursor.Top + (_pushedCursor.Left / _consoleDriver.BufferWidth);

var consoleTop = _cursorBottom - WrittenLineCount + physicalTop;

if (_pushedCursor.Left > 0 && physicalLeft == 0)
{
var physicalLeft = _pushedCursor.Left % _consoleDriver.BufferWidth;
var physicalTop = _pushedCursor.Top + (_pushedCursor.Left / _consoleDriver.BufferWidth);
_consoleDriver.WriteLine();

var consoleTop = _cursorBottom - WrittenLineCount + physicalTop;
if (_pushedCursor.Left > 0 && physicalLeft == 0)
if (consoleTop == _consoleDriver.BufferHeight)
{
_consoleDriver.WriteLine();
if (consoleTop == _consoleDriver.BufferHeight)
{
_cursorBottom--;
consoleTop--;
}
_cursorBottom--;
consoleTop--;
}

_consoleDriver.SetCursorPosition(physicalLeft, consoleTop);
}

_consoleDriver.SetCursorPosition(physicalLeft, consoleTop);
}

public void ClearConsole(int cursorBottom, int writtenLineCount)
Expand Down
22 changes: 20 additions & 2 deletions Sharprompt/Internal/Paginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public Paginator(IEnumerable<T> items, int pageSize, Optional<T> defaultValue, F
}

private readonly T[] _items;
private readonly int _pageSize;
private readonly Func<T, string> _textSelector;

private int _pageSize;
private T[] _filteredItems = Array.Empty<T>();
private int _selectedIndex = -1;

Expand Down Expand Up @@ -94,11 +94,24 @@ public void UpdateFilter(string keyword)
FilterKeyword = keyword;

_selectedIndex = -1;
CurrentPage = 0;

UpdateFilteredItems();
}

public void UpdatePageSize(int newPageSize)
{
if (_pageSize == newPageSize)
{
return;
}

TryGetSelectedItem(out var selectedItem);

_pageSize = newPageSize <= 0 ? _items.Length : Math.Min(newPageSize, _items.Length);

InitializeDefaults(Optional<T>.Create(selectedItem));
}

public IEnumerator<T> GetEnumerator() => (IEnumerator<T>)_filteredItems.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand All @@ -109,6 +122,11 @@ private void UpdateFilteredItems()
.ToArray();

PageCount = (_filteredItems.Length - 1) / _pageSize + 1;

if (CurrentPage >= PageCount)
{
CurrentPage = 0;
}
}

private void InitializeDefaults(Optional<T> defaultValue)
Expand Down
4 changes: 2 additions & 2 deletions Sharprompt/Internal/RenderScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public RenderScope(OffscreenBuffer offscreenBuffer, IConsoleDriver consoleDriver
{
_offscreenBuffer = offscreenBuffer;
_consoleDriver = consoleDriver;
_cursorBottom = cursorBottom;
_writtenLineCount = writtenLineCount;
_cursorBottom = Math.Min(cursorBottom, _consoleDriver.WindowHeight - 1);
_writtenLineCount = Math.Min(writtenLineCount, _consoleDriver.WindowHeight - 1);

_offscreenBuffer.ClearBuffer();
}
Expand Down