Skip to content

Commit f0b6ab4

Browse files
authored
Better handling of terminal resizing (#254)
* Better handling of terminal resizing * Fixed dotnet-format error
1 parent af3d6d9 commit f0b6ab4

File tree

9 files changed

+54
-26
lines changed

9 files changed

+54
-26
lines changed

Sharprompt.Tests/PropertyMetadataTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Basic()
1717
var metadata = PropertyMetadataFactory.Create(new BasicModel());
1818

1919
Assert.NotNull(metadata);
20-
Assert.Equal(1, metadata.Count);
20+
Assert.Single(metadata);
2121

2222
Assert.Equal(typeof(string), metadata[0].Type);
2323
Assert.Equal(FormType.Input, metadata[0].DetermineFormType());
@@ -27,7 +27,7 @@ public void Basic()
2727
Assert.False(metadata[0].IsCollection);
2828
Assert.Null(metadata[0].DefaultValue);
2929
Assert.Null(metadata[0].Order);
30-
Assert.Equal(1, metadata[0].Validators.Count);
30+
Assert.Single(metadata[0].Validators);
3131
}
3232

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

3838
Assert.NotNull(metadata);
39-
Assert.Equal(1, metadata.Count);
39+
Assert.Single(metadata);
4040

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

184184
Assert.NotNull(metadata);
185-
Assert.Equal(1, metadata.Count);
185+
Assert.Single(metadata);
186186
}
187187

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

193193
Assert.NotNull(metadata);
194-
Assert.Equal(1, metadata.Count);
194+
Assert.Single(metadata);
195195
}
196196

197197
[Fact]

Sharprompt.Tests/Sharprompt.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
11-
<PackageReference Include="xunit" Version="2.4.2" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
11+
<PackageReference Include="xunit" Version="2.5.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
<PrivateAssets>all</PrivateAssets>
1515
</PackageReference>

Sharprompt/Drivers/DefaultConsoleDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public ConsoleKeyInfo ReadKey()
7676
{
7777
var keyInfo = Console.ReadKey(true);
7878

79-
if (keyInfo.Key == ConsoleKey.C && keyInfo.Modifiers == ConsoleModifiers.Control)
79+
if (keyInfo is { Key: ConsoleKey.C, Modifiers: ConsoleModifiers.Control })
8080
{
8181
CancellationCallback.Invoke();
8282
}

Sharprompt/Forms/FormBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ protected FormBase()
2929

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

32+
protected int Width => _consoleDriver.WindowWidth;
33+
3234
protected int Height => _consoleDriver.WindowHeight;
3335

3436
public void Dispose() => _formRenderer.Dispose();

Sharprompt/Forms/MultiSelectForm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public MultiSelectForm(MultiSelectOptions<T> options)
4242

4343
protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
4444
{
45+
_paginator.UpdatePageSize(Math.Min(_options.PageSize, Height - 2));
46+
4547
offscreenBuffer.WritePrompt(_options.Message);
4648
offscreenBuffer.Write(_paginator.FilterKeyword);
4749

Sharprompt/Forms/SelectForm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public SelectForm(SelectOptions<T> options)
3131

3232
protected override void InputTemplate(OffscreenBuffer offscreenBuffer)
3333
{
34+
_paginator.UpdatePageSize(Math.Min(_options.PageSize, Height - 2));
35+
3436
offscreenBuffer.WritePrompt(_options.Message);
3537
offscreenBuffer.Write(_paginator.FilterKeyword);
3638

Sharprompt/Internal/OffscreenBuffer.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,28 @@ public void RenderToConsole()
7474

7575
_cursorBottom = _consoleDriver.CursorTop;
7676

77-
if (_pushedCursor is not null)
77+
if (_pushedCursor is null)
78+
{
79+
return;
80+
}
81+
82+
var physicalLeft = _pushedCursor.Left % _consoleDriver.BufferWidth;
83+
var physicalTop = _pushedCursor.Top + (_pushedCursor.Left / _consoleDriver.BufferWidth);
84+
85+
var consoleTop = _cursorBottom - WrittenLineCount + physicalTop;
86+
87+
if (_pushedCursor.Left > 0 && physicalLeft == 0)
7888
{
79-
var physicalLeft = _pushedCursor.Left % _consoleDriver.BufferWidth;
80-
var physicalTop = _pushedCursor.Top + (_pushedCursor.Left / _consoleDriver.BufferWidth);
89+
_consoleDriver.WriteLine();
8190

82-
var consoleTop = _cursorBottom - WrittenLineCount + physicalTop;
83-
if (_pushedCursor.Left > 0 && physicalLeft == 0)
91+
if (consoleTop == _consoleDriver.BufferHeight)
8492
{
85-
_consoleDriver.WriteLine();
86-
if (consoleTop == _consoleDriver.BufferHeight)
87-
{
88-
_cursorBottom--;
89-
consoleTop--;
90-
}
93+
_cursorBottom--;
94+
consoleTop--;
9195
}
92-
93-
_consoleDriver.SetCursorPosition(physicalLeft, consoleTop);
9496
}
97+
98+
_consoleDriver.SetCursorPosition(physicalLeft, consoleTop);
9599
}
96100

97101
public void ClearConsole(int cursorBottom, int writtenLineCount)

Sharprompt/Internal/Paginator.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public Paginator(IEnumerable<T> items, int pageSize, Optional<T> defaultValue, F
1818
}
1919

2020
private readonly T[] _items;
21-
private readonly int _pageSize;
2221
private readonly Func<T, string> _textSelector;
2322

23+
private int _pageSize;
2424
private T[] _filteredItems = Array.Empty<T>();
2525
private int _selectedIndex = -1;
2626

@@ -94,11 +94,24 @@ public void UpdateFilter(string keyword)
9494
FilterKeyword = keyword;
9595

9696
_selectedIndex = -1;
97-
CurrentPage = 0;
9897

9998
UpdateFilteredItems();
10099
}
101100

101+
public void UpdatePageSize(int newPageSize)
102+
{
103+
if (_pageSize == newPageSize)
104+
{
105+
return;
106+
}
107+
108+
TryGetSelectedItem(out var selectedItem);
109+
110+
_pageSize = newPageSize <= 0 ? _items.Length : Math.Min(newPageSize, _items.Length);
111+
112+
InitializeDefaults(Optional<T>.Create(selectedItem));
113+
}
114+
102115
public IEnumerator<T> GetEnumerator() => (IEnumerator<T>)_filteredItems.GetEnumerator();
103116

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

111124
PageCount = (_filteredItems.Length - 1) / _pageSize + 1;
125+
126+
if (CurrentPage >= PageCount)
127+
{
128+
CurrentPage = 0;
129+
}
112130
}
113131

114132
private void InitializeDefaults(Optional<T> defaultValue)

Sharprompt/Internal/RenderScope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public RenderScope(OffscreenBuffer offscreenBuffer, IConsoleDriver consoleDriver
1010
{
1111
_offscreenBuffer = offscreenBuffer;
1212
_consoleDriver = consoleDriver;
13-
_cursorBottom = cursorBottom;
14-
_writtenLineCount = writtenLineCount;
13+
_cursorBottom = Math.Min(cursorBottom, _consoleDriver.WindowHeight - 1);
14+
_writtenLineCount = Math.Min(writtenLineCount, _consoleDriver.WindowHeight - 1);
1515

1616
_offscreenBuffer.ClearBuffer();
1717
}

0 commit comments

Comments
 (0)