TextView / ScrollToEnd
See original GitHub issueBuild: 0.89.4
New to using this project so there could be a better way to do what I’m intending, I was unable to figure it out without using some of the internals. Basically, I’m using a TextView to display the contents of a running log. As new text is added from an async Task
I want the text view to scroll to the end.
Flow would be:
- Scroll to the last line minus the frame height so that you see the last line but also the last screen of information (scrolling to just the last line with
ScrollTo
shows only the last line in my testing). - Position the cursor on the last line.
I had attempted to add this in my code, wasn’t sure how to get the current number of lines from the TextView
so I was counting line breaks which I’d prefer to avoid since it already exists. In the TextView
control the model.Count
has that value and it additionally has the current frame height which I couldn’t figure out how to get at from my own code. I created this function I put in TextView
, I’m curious if it would be useful in some form (or if there’s a way to accomplish this in my code without adding a function to TextView).
/// <summary>
/// Will scroll the <see cref="TextView"/> to the last line and position the cursor there.
/// </summary>
public void ScrollToEnd ()
{
// Subtract off the frame's height from the current number of lines so that
// when we scroll to the bottom we show the last screen worth of lines and not
// just the last line.
topRow = Math.Max (1, model.Count - Frame.Height);
SetNeedsDisplay ();
// Set the cursor to the last row and then position it.
currentRow = (model.Count > 0) ? currentRow = model.Count - 1 : 0;
PositionCursor ();
}
I tested this in cmd and Powershell on Windows and via Putty on Ubuntu 18.04.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top GitHub Comments
MoveHome
andMoveEnd
sound good to me as well. My personal preference would be to seeMoveUp
andMoveDown
be public as well in regards to giving the caller more options to get theTextView
to do what they need it to (I won’t put that in the PR unless that’s approved). But I also understand that sometimes the original designers are trying to save us from ourselves and have intentions that are bigger picture than my use case. 😄I tested your code and it works well. I’ll work on a PR. 😃
Don’t forget to add documentation to the public methods.