Skip to content

Commit 89b3b0d

Browse files
CaptureScreen: scroll buffer with selection, add alternate movement keys.
1 parent 7ba3996 commit 89b3b0d

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

PSReadLine/ConsoleLib.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ public void WriteLine(string value)
536536
}
537537

538538
public void WriteBufferLines(CHAR_INFO[] buffer, ref int top)
539+
{
540+
WriteBufferLines(buffer, ref top, true);
541+
}
542+
543+
public void WriteBufferLines(CHAR_INFO[] buffer, ref int top, bool ensureBottomLineVisible)
539544
{
540545
var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output);
541546

@@ -565,7 +570,8 @@ public void WriteBufferLines(CHAR_INFO[] buffer, ref int top)
565570
bufferSize, bufferCoord, ref writeRegion);
566571

567572
// Now make sure the bottom line is visible
568-
if (bottom >= (Console.WindowTop + Console.WindowHeight))
573+
if (ensureBottomLineVisible &&
574+
(bottom >= (Console.WindowTop + Console.WindowHeight)))
569575
{
570576
Console.CursorTop = bottom;
571577
}

PSReadLine/PublicAPI.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public interface IConsole
4747
void WriteLine(string s);
4848
void Write(string s);
4949
void WriteBufferLines(CHAR_INFO[] buffer, ref int top);
50+
void WriteBufferLines(CHAR_INFO[] buffer, ref int top, bool ensureBottomLineVisible);
5051
void ScrollBuffer(int lines);
5152
CHAR_INFO[] ReadBufferLines(int top, int count);
5253
}

PSReadLine/ScreenCapture.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static void InvertLines(int start, int count)
2020
buffer[i].ForegroundColor = (ConsoleColor)((int)buffer[i].ForegroundColor ^ 7);
2121
buffer[i].BackgroundColor = (ConsoleColor)((int)buffer[i].BackgroundColor ^ 7);
2222
}
23-
_singleton._console.WriteBufferLines(buffer, ref start);
23+
_singleton._console.WriteBufferLines(buffer, ref start, false);
2424
}
2525

2626
/// <summary>
@@ -33,6 +33,13 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
3333
int selectionTop = _singleton._console.CursorTop;
3434
int selectionHeight = 1;
3535
int currentY = selectionTop;
36+
Internal.IConsole console = _singleton._console;
37+
38+
// We'll keep the current selection line (currentY) at least 4 lines
39+
// away from the top or bottom of the window.
40+
const int margin = 5;
41+
Func<bool> tooCloseToTop = () => { return (currentY - console.WindowTop) < margin; };
42+
Func<bool> tooCloseToBottom = () => { return ((console.WindowTop + console.WindowHeight) - currentY) < margin; };
3643

3744
// Current lines starts out selected
3845
InvertLines(selectionTop, selectionHeight);
@@ -42,7 +49,11 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
4249
var k = ReadKey();
4350
switch (k.Key)
4451
{
52+
case ConsoleKey.K:
4553
case ConsoleKey.UpArrow:
54+
if (tooCloseToTop())
55+
ScrollDisplayUpLine();
56+
4657
if (currentY > 0)
4758
{
4859
currentY -= 1;
@@ -67,8 +78,12 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
6778
}
6879
break;
6980

81+
case ConsoleKey.J:
7082
case ConsoleKey.DownArrow:
71-
if (currentY < (_singleton._console.BufferHeight - 1))
83+
if (tooCloseToBottom())
84+
ScrollDisplayDownLine();
85+
86+
if (currentY < (console.BufferHeight - 1))
7287
{
7388
currentY += 1;
7489
if ((k.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
@@ -103,6 +118,7 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
103118
case ConsoleKey.Enter:
104119
InvertLines(selectionTop, selectionHeight);
105120
DumpScreenToClipboard(selectionTop, selectionHeight);
121+
ScrollDisplayToCursor();
106122
return;
107123

108124
case ConsoleKey.Escape:
@@ -124,6 +140,7 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
124140
}
125141
}
126142
InvertLines(selectionTop, selectionHeight);
143+
ScrollDisplayToCursor();
127144
}
128145

129146
private const string CmdColorTable = @"

UnitTestPSReadLine/UnitTestReadLine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ public void Write(string s)
191191
}
192192

193193
public void WriteBufferLines(CHAR_INFO[] bufferToWrite, ref int top)
194+
{
195+
WriteBufferLines(bufferToWrite, ref top, true);
196+
}
197+
198+
public void WriteBufferLines(CHAR_INFO[] bufferToWrite, ref int top, bool ensureBottomLineVisible)
194199
{
195200
var startPos = top * BufferWidth;
196201
for (int i = 0; i < bufferToWrite.Length; i++)

0 commit comments

Comments
 (0)