+ position += Character.charCount(ch);
+ screenPosition += StringUtils.width(ch);
+ }
+ scanLine();
+ }
+
+ /**
+ * Determine string position from screen position.
+ *
+ * @param screenPosition the position on screen
+ * @return the equivalent position in text
+ */
+ private int screenToTextPosition(final int screenPosition) {
+ if (screenPosition == 0) {
+ return 0;
+ }
+
+ int n = 0;
+ for (int i = 0; i < rawText.length(); i++) {
+ n += StringUtils.width(rawText.codePointAt(i));
+ if (n >= screenPosition) {
+ return i + 1;
+ }
+ }
+ // screenPosition exceeds the available text length.
+ throw new IndexOutOfBoundsException("screenPosition " + screenPosition +
+ " exceeds available text length " + rawText.length());
+ }
+
+ /**
+ * Trim trailing whitespace from line, repositioning cursor if needed.
+ */
+ public void trimRight() {
+ if (rawText.length() == 0) {
+ return;
+ }
+ if (!Character.isWhitespace(rawText.charAt(rawText.length() - 1))) {
+ return;
+ }
+ while ((rawText.length() > 0)
+ && Character.isWhitespace(rawText.charAt(rawText.length() - 1))
+ ) {
+ rawText.deleteCharAt(rawText.length() - 1);
+ }
+ if (position >= rawText.length()) {
+ end();