From 79c275b1c9685f25afacb0d849233e470b6f52ff Mon Sep 17 00:00:00 2001 From: Scott Bilas Date: Mon, 19 Aug 2024 00:22:23 +0100 Subject: [PATCH] Utils and rename --- src/Core.Terminal/ControlBuilderExtensions.cs | 9 ++++++++- src/Core/ArrayExtensions.cs | 7 +++++++ src/Showkeys.Cli/DriveCommand.cs | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Core.Terminal/ControlBuilderExtensions.cs b/src/Core.Terminal/ControlBuilderExtensions.cs index 2a5a965..e77964f 100644 --- a/src/Core.Terminal/ControlBuilderExtensions.cs +++ b/src/Core.Terminal/ControlBuilderExtensions.cs @@ -82,9 +82,16 @@ public static ControlBuilder ResetScrollMargin(this ControlBuilder @this) => public static ControlBuilder ReverseLineFeed(this ControlBuilder @this) => @this.Print(ESC).Print(['M']); // this is reverse index (M), not reverse line feed (H), but this is supported much better and does the same thing according to docs - public static AutoSaveRestoreCursorState AutoSaveRestoreCursorState(this ControlBuilder @this) => + public static AutoSaveRestoreCursorState SaveRestoreCursor(this ControlBuilder @this) => new(@this); + public static AutoSaveRestoreCursorState SaveRestoreCursor(this ControlBuilder @this, int y, int x) + { + var saver = @this.SaveRestoreCursor(); + @this.MoveCursorTo(y, x); + return saver; + } + // note that this will cause the cursor to move to home position. can't save/restore cursor pos because it is tied // to cursor state, and restoring state will kill the new constrain mode. public static ControlBuilder SetCursorConstrainMode(this ControlBuilder @this, CursorConstrainMode mode) => @this diff --git a/src/Core/ArrayExtensions.cs b/src/Core/ArrayExtensions.cs index 68813a7..eaf793c 100644 --- a/src/Core/ArrayExtensions.cs +++ b/src/Core/ArrayExtensions.cs @@ -2,6 +2,13 @@ namespace OkTools.Core; public static class ArrayExtensions { + public static ArraySegment WithOffset(this T[] @this, int offset) => + new(@this, offset, @this.Length - offset); + public static ArraySegment WithLength(this T[] @this, int length) => + new(@this, 0, length); + public static ArraySegment Slice(this T[] @this, int offset, int length) => + new(@this, offset, length); + public static void ShiftLeft(this T[] @this, int count) { Array.Copy(@this, count, @this, 0, @this.Length - count); diff --git a/src/Showkeys.Cli/DriveCommand.cs b/src/Showkeys.Cli/DriveCommand.cs index 1e545bd..63a08f3 100644 --- a/src/Showkeys.Cli/DriveCommand.cs +++ b/src/Showkeys.Cli/DriveCommand.cs @@ -154,7 +154,7 @@ public async Task RunAsync(bool save) case { Char: 'l', Modifiers: ConsoleModifiers.Control }: OutControl(cb => { - using var _ = cb.AutoSaveRestoreCursorState(); + using var _ = cb.SaveRestoreCursor(); for (var line = _scrollTop; line <= _scrollBottom; ++line) { cb.MoveCursorTo(line, 0);