X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FWord.java;h=eada29cff83ed8b1c61c59d1741b5646d1f645b8;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=d4532bbfa2d720ead178d4a2efdabc35989c104a;hpb=71a389c9810382e014682dde52e94d3f34e385fa;p=fanfix.git diff --git a/src/jexer/teditor/Word.java b/src/jexer/teditor/Word.java index d4532bb..eada29c 100644 --- a/src/jexer/teditor/Word.java +++ b/src/jexer/teditor/Word.java @@ -3,7 +3,7 @@ * * 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"), @@ -29,6 +29,7 @@ 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 @@ -40,6 +41,10 @@ import jexer.bits.CellAttributes; */ public class Word { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * The color to render this word as on screen. */ @@ -61,6 +66,42 @@ public class Word { */ 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. * @@ -99,7 +140,7 @@ public class Word { // 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()); } /** @@ -118,34 +159,6 @@ public class Word { 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. */ @@ -172,9 +185,9 @@ public class Word { * @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; } @@ -193,14 +206,14 @@ public class Word { && 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; }