From 1b1206070bb94d706c14400b8332a81646b77a25 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Fri, 18 Aug 2017 16:05:17 -0400 Subject: [PATCH] #19 expose home/end for TField --- src/jexer/TField.java | 59 +++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 785a276..c93cecb 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -316,23 +316,12 @@ public class TField extends TWidget { return; } if (keypress.equals(kbHome)) { - position = 0; - windowStart = 0; + home(); return; } if (keypress.equals(kbEnd)) { - position = text.length(); - if (fixed == true) { - if (position >= getWidth()) { - position = text.length() - 1; - } - } else { - windowStart = text.length() - getWidth() + 1; - if (windowStart < 0) { - windowStart = 0; - } - } + end(); return; } @@ -467,4 +456,48 @@ public class TField extends TWidget { } } + /** + * Position the cursor at the first column. The field may adjust the + * window start to show as much of the field as possible. + */ + public void home() { + position = 0; + windowStart = 0; + } + + /** + * Set the editing position to the last filled character. The field may + * adjust the window start to show as much of the field as possible. + */ + public void end() { + position = text.length(); + if (fixed == true) { + if (position >= getWidth()) { + position = text.length() - 1; + } + } else { + windowStart = text.length() - getWidth() + 1; + if (windowStart < 0) { + windowStart = 0; + } + } + } + + /** + * Set the editing position. The field may adjust the window start to + * show as much of the field as possible. + * + * @param position the new position + * @throws IndexOutOfBoundsException if position is outside the range of + * the available text + */ + public void setPosition(final int position) { + if ((position < 0) || (position >= text.length())) { + throw new IndexOutOfBoundsException("Max length is " + + text.length() + ", requested position " + position); + } + this.position = position; + normalizeWindowStart(); + } + } -- 2.27.0