From 57486aac9c11fc492369987d2b8f5ba9b2b58549 Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Tue, 14 Jul 2020 11:52:27 +0200 Subject: [PATCH] Support PgUp/PgDn --- MApplication/DisplayTextViewPort.cs | 26 +++++++++++++++++-- MApplication/Program.cs | 39 +++++++++++------------------ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/MApplication/DisplayTextViewPort.cs b/MApplication/DisplayTextViewPort.cs index 9ba49af..68f37fb 100644 --- a/MApplication/DisplayTextViewPort.cs +++ b/MApplication/DisplayTextViewPort.cs @@ -134,8 +134,8 @@ namespace MApplication _ when CursorRelativeLine == Height - 1 => With( out changed1, - startingLine: Math.Min(CursorAbsoluteLine + 1, Text.Lines.Count) - Height + 1, - cursorAbsoluteLine: Math.Min(CursorAbsoluteLine + 1, Text.Lines.Count)), + startingLine: Math.Min(CursorAbsoluteLine + 1, Text.Lines.Count - 1) - Height + 1, + cursorAbsoluteLine: Math.Min(CursorAbsoluteLine + 1, Text.Lines.Count - 1)), _ => With( out changed1, cursorAbsoluteLine: CursorAbsoluteLine + 1), @@ -145,6 +145,28 @@ namespace MApplication return result; } + internal DisplayTextViewPort PageDown(out bool needsRedraw) + { + var result1 = With( + out var changed1, + startingLine: Math.Min(StartingLine + Height, Text.Lines.Count - 1), + cursorAbsoluteLine: Math.Min(CursorAbsoluteLine + Height, Text.Lines.Count - 1)); + var result = result1.SnapToLine(out var changed2); + needsRedraw = changed1 || changed2; + return result; + } + + internal DisplayTextViewPort PageUp(out bool needsRedraw) + { + var result1 = With( + out var changed1, + startingLine: Math.Max(StartingLine - Height, 0), + cursorAbsoluteLine: Math.Max(CursorAbsoluteLine - Height, 0)); + var result = result1.SnapToLine(out var changed2); + needsRedraw = changed1 || changed2; + return result; + } + public DisplayTextViewPort CursorHome(out bool needsRedraw) { return With( diff --git a/MApplication/Program.cs b/MApplication/Program.cs index 4bdac12..f1a1843 100644 --- a/MApplication/Program.cs +++ b/MApplication/Program.cs @@ -47,35 +47,24 @@ namespace MApplication _outputView.MoveCursorTo(viewPort.CursorRelativeColumn, viewPort.CursorRelativeLine); var key = Console.ReadKey(intercept: true); - switch (key.Key) + viewPort = key.Key switch { - case ConsoleKey.LeftArrow: - viewPort = viewPort.CursorLeft(out needsRedraw); - break; - - case ConsoleKey.RightArrow: - viewPort = viewPort.CursorRight(out needsRedraw); - break; - - case ConsoleKey.UpArrow: - viewPort = viewPort.CursorUp(out needsRedraw); - break; - - case ConsoleKey.DownArrow: - viewPort = viewPort.CursorDown(out needsRedraw); - break; - - case ConsoleKey.Home: - viewPort = viewPort.CursorHome(out needsRedraw); - break; - - case ConsoleKey.End: - viewPort = viewPort.CursorEnd(out needsRedraw); - break; + ConsoleKey.LeftArrow => viewPort.CursorLeft(out needsRedraw), + ConsoleKey.RightArrow => viewPort.CursorRight(out needsRedraw), + ConsoleKey.UpArrow => viewPort.CursorUp(out needsRedraw), + ConsoleKey.DownArrow => viewPort.CursorDown(out needsRedraw), + ConsoleKey.Home => viewPort.CursorHome(out needsRedraw), + ConsoleKey.End => viewPort.CursorEnd(out needsRedraw), + ConsoleKey.PageUp => viewPort.PageUp(out needsRedraw), + ConsoleKey.PageDown => viewPort.PageDown(out needsRedraw), + _ => viewPort, + }; + if (key.Key == ConsoleKey.Q) + { + return; } } } - } class Program