2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.teditor
;
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.StringUtils
;
35 * A Word represents text that was entered by the user. It can be either
36 * whitespace or non-whitespace.
38 * Very dumb highlighting is supported, it has no sense of parsing (not even
39 * comments). For now this only highlights some Java keywords and
44 // ------------------------------------------------------------------------
45 // Variables --------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * The color to render this word as on screen.
51 private CellAttributes color
= new CellAttributes();
54 * The default color for the TEditor class.
56 private CellAttributes defaultColor
= null;
59 * The text highlighter to use.
61 private Highlighter highlighter
= null;
64 * The actual text of this word. Average word length is 6 characters,
65 * with a lot of shorter ones, so start with 3.
67 private StringBuilder text
= new StringBuilder(3);
69 // ------------------------------------------------------------------------
70 // Constructors -----------------------------------------------------------
71 // ------------------------------------------------------------------------
74 * Construct a word with one character.
76 * @param ch the first character of the word
77 * @param defaultColor the color for unhighlighted text
78 * @param highlighter the highlighter to use
80 public Word(final int ch
, final CellAttributes defaultColor
,
81 final Highlighter highlighter
) {
83 this.defaultColor
= defaultColor
;
84 this.highlighter
= highlighter
;
85 text
.append(Character
.toChars(ch
));
89 * Construct a word with an empty string.
91 * @param defaultColor the color for unhighlighted text
92 * @param highlighter the highlighter to use
94 public Word(final CellAttributes defaultColor
,
95 final Highlighter highlighter
) {
97 this.defaultColor
= defaultColor
;
98 this.highlighter
= highlighter
;
101 // ------------------------------------------------------------------------
102 // Word -------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Get the color used to display this word on screen.
110 public CellAttributes
getColor() {
111 return new CellAttributes(color
);
115 * Set the color used to display this word on screen.
117 * @param color the color
119 public void setColor(final CellAttributes color
) {
124 * Get the text to display.
128 public String
getText() {
129 return text
.toString();
133 * Get the on-screen display length.
135 * @return the number of cells needed to display this word
137 public int getDisplayLength() {
138 // TODO: figure out how to handle the tab character. Do we have a
139 // global tab stops list and current word position?
140 return StringUtils
.width(text
.toString());
144 * See if this is a whitespace word. Note that empty string is
145 * considered whitespace.
147 * @return true if this word is whitespace
149 public boolean isWhitespace() {
150 if (text
.length() == 0) {
153 if (Character
.isWhitespace(text
.charAt(0))) {
160 * Perform highlighting.
162 public void applyHighlight() {
163 color
.setTo(defaultColor
);
164 if (highlighter
== null) {
167 String key
= text
.toString();
168 CellAttributes newColor
= highlighter
.getColor(key
);
169 if (newColor
!= null) {
170 color
.setTo(newColor
);
175 * Add a character to this word. If this is a whitespace character
176 * adding to a non-whitespace word, create a new word and return that;
177 * similarly if this a non-whitespace character adding to a whitespace
178 * word, create a new word and return that. Note package private access:
179 * this is only called by Line to figure out highlighting boundaries.
181 * @param ch the new character to add
182 * @return either this word (if it was added), or a new word that
185 public Word
addChar(final int ch
) {
186 if (text
.length() == 0) {
187 text
.append(Character
.toChars(ch
));
191 // Give the highlighter the option to split here.
192 if (highlighter
!= null) {
193 if (highlighter
.shouldSplit(ch
)
194 || highlighter
.shouldSplit(text
.charAt(0))
196 Word newWord
= new Word(ch
, defaultColor
, highlighter
);
201 // Highlighter didn't care, so split at whitespace.
202 if (Character
.isWhitespace(text
.charAt(0))
203 && Character
.isWhitespace(ch
)
205 // Adding to a whitespace word, keep at it.
206 text
.append(Character
.toChars(ch
));
209 if (!Character
.isWhitespace(text
.charAt(0))
210 && !Character
.isWhitespace(ch
)
212 // Adding to a non-whitespace word, keep at it.
213 text
.append(Character
.toChars(ch
));
217 // Switching from whitespace to non-whitespace or vice versa, so
219 Word newWord
= new Word(ch
, defaultColor
, highlighter
);