*
* The MIT License (MIT)
*
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
package jexer.teditor;
import jexer.bits.CellAttributes;
+import jexer.bits.StringUtils;
/**
* A Word represents text that was entered by the user. It can be either
*/
public class Word {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The color to render this word as on screen.
*/
*/
private StringBuilder text = new StringBuilder(3);
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a word with one character.
+ *
+ * @param ch the first character of the word
+ * @param defaultColor the color for unhighlighted text
+ * @param highlighter the highlighter to use
+ */
+ public Word(final int ch, final CellAttributes defaultColor,
+ final Highlighter highlighter) {
+
+ this.defaultColor = defaultColor;
+ this.highlighter = highlighter;
+ text.append(Character.toChars(ch));
+ }
+
+ /**
+ * Construct a word with an empty string.
+ *
+ * @param defaultColor the color for unhighlighted text
+ * @param highlighter the highlighter to use
+ */
+ public Word(final CellAttributes defaultColor,
+ final Highlighter highlighter) {
+
+ this.defaultColor = defaultColor;
+ this.highlighter = highlighter;
+ }
+
+ // ------------------------------------------------------------------------
+ // Word -------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get the color used to display this word on screen.
*
* @return the number of cells needed to display this word
*/
public int getDisplayLength() {
- // For now, just use the text length. In the future, this will be a
- // grapheme count.
-
- // TODO: figure out how to handle the tab character. Do we have a
- // global tab stops list and current word position?
- return text.length();
+ return StringUtils.width(text.toString());
}
/**
return false;
}
- /**
- * Construct a word with one character.
- *
- * @param ch the first character of the word
- * @param defaultColor the color for unhighlighted text
- * @param highlighter the highlighter to use
- */
- public Word(final char ch, final CellAttributes defaultColor,
- final Highlighter highlighter) {
-
- this.defaultColor = defaultColor;
- this.highlighter = highlighter;
- text.append(ch);
- }
-
- /**
- * Construct a word with an empty string.
- *
- * @param defaultColor the color for unhighlighted text
- * @param highlighter the highlighter to use
- */
- public Word(final CellAttributes defaultColor,
- final Highlighter highlighter) {
-
- this.defaultColor = defaultColor;
- this.highlighter = highlighter;
- }
-
/**
* Perform highlighting.
*/
* Add a character to this word. If this is a whitespace character
* adding to a non-whitespace word, create a new word and return that;
* similarly if this a non-whitespace character adding to a whitespace
- * word, create a new word and return that.
+ * word, create a new word and return that. Note package private access:
+ * this is only called by Line to figure out highlighting boundaries.
*
* @param ch the new character to add
* @return either this word (if it was added), or a new word that
* contains ch
*/
- public Word addChar(final char ch) {
+ public Word addChar(final int ch) {
if (text.length() == 0) {
- text.append(ch);
+ text.append(Character.toChars(ch));
return this;
}
&& Character.isWhitespace(ch)
) {
// Adding to a whitespace word, keep at it.
- text.append(ch);
+ text.append(Character.toChars(ch));
return this;
}
if (!Character.isWhitespace(text.charAt(0))
&& !Character.isWhitespace(ch)
) {
// Adding to a non-whitespace word, keep at it.
- text.append(ch);
+ text.append(Character.toChars(ch));
return this;
}