From e820d5dd4e52a787e7f53f6409bb6ff334c3ef7b Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sat, 10 Aug 2019 00:33:08 -0500 Subject: [PATCH] #35 wip --- src/jexer/TApplication.java | 15 +++++++-------- src/jexer/TCalendar.java | 8 ++++++-- src/jexer/TDirectoryList.java | 4 +++- src/jexer/TMessageBox.java | 7 ++++--- src/jexer/TText.java | 9 +++++---- src/jexer/bits/StringUtils.java | 14 +++++++------- src/jexer/ttree/TTreeViewWidget.java | 3 ++- src/jexer/ttree/TTreeViewWindow.java | 3 ++- 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 7c96258..bffe38e 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -48,6 +48,7 @@ import java.util.ResourceBundle; import jexer.bits.Cell; import jexer.bits.CellAttributes; import jexer.bits.ColorTheme; +import jexer.bits.StringUtils; import jexer.event.TCommandEvent; import jexer.event.TInputEvent; import jexer.event.TKeypressEvent; @@ -518,9 +519,7 @@ public class TApplication implements Runnable { } } // while (!application.quit) - assert (dirty == true); - - // Flush the screen contents + // Flush the screen contents if (debugThreads) { System.err.printf("%d %s backend.flushScreen()\n", System.currentTimeMillis(), Thread.currentThread()); @@ -1842,7 +1841,7 @@ public class TApplication implements Runnable { menuMnemonicColor = theme.getColor("tmenu.mnemonic"); } // Draw the menu title - getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', + getScreen().hLineXY(x, 0, StringUtils.width(menu.getTitle()) + 2, ' ', menuColor); getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); // Draw the highlight character @@ -1854,7 +1853,7 @@ public class TApplication implements Runnable { // Reset the screen clipping so we can draw the next title. getScreen().resetClipping(); } - x += menu.getTitle().length() + 2; + x += StringUtils.width(menu.getTitle()) + 2; } for (TMenu menu: subMenus) { @@ -2729,7 +2728,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { if ((mouse.getAbsoluteX() >= menu.getTitleX()) && (mouse.getAbsoluteX() < menu.getTitleX() - + menu.getTitle().length() + 2) + + StringUtils.width(menu.getTitle()) + 2) ) { menu.setActive(true); activeMenu = menu; @@ -2757,7 +2756,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { if ((mouse.getAbsoluteX() >= menu.getTitleX()) && (mouse.getAbsoluteX() < menu.getTitleX() - + menu.getTitle().length() + 2) + + StringUtils.width(menu.getTitle()) + 2) ) { menu.setActive(true); activeMenu = menu; @@ -3042,7 +3041,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { menu.setX(x); menu.setTitleX(x); - x += menu.getTitle().length() + 2; + x += StringUtils.width(menu.getTitle()) + 2; // Don't let the menu window exceed the screen width int rightEdge = menu.getX() + menu.getWidth(); diff --git a/src/jexer/TCalendar.java b/src/jexer/TCalendar.java index b5ecf4d..580fb9f 100644 --- a/src/jexer/TCalendar.java +++ b/src/jexer/TCalendar.java @@ -33,6 +33,7 @@ import java.util.GregorianCalendar; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import static jexer.TKeypress.*; @@ -238,10 +239,13 @@ public class TCalendar extends TWidget { // Draw the title String title = String.format("%tB %tY", displayCalendar, displayCalendar); - int titleLeft = (getWidth() - title.length() - 2) / 2; + // This particular title is always single-width (see format string + // above), but for completeness let's treat it the same as every + // other window title string. + int titleLeft = (getWidth() - StringUtils.width(title) - 2) / 2; putCharXY(titleLeft, 0, ' ', titleColor); putStringXY(titleLeft + 1, 0, title, titleColor); - putCharXY(titleLeft + title.length() + 1, 0, ' ', + putCharXY(titleLeft + StringUtils.width(title) + 1, 0, ' ', titleColor); // Arrows diff --git a/src/jexer/TDirectoryList.java b/src/jexer/TDirectoryList.java index e00d14f..322ff5c 100644 --- a/src/jexer/TDirectoryList.java +++ b/src/jexer/TDirectoryList.java @@ -34,6 +34,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import jexer.bits.StringUtils; + /** * TDirectoryList shows the files within a directory. */ @@ -223,7 +225,7 @@ public class TDirectoryList extends TList { */ private String renderFile(final File file) { String name = file.getName(); - if (name.length() > 20) { + if (StringUtils.width(name) > 20) { name = name.substring(0, 17) + "..."; } return String.format("%-20s %5dk", name, (file.length() / 1024)); diff --git a/src/jexer/TMessageBox.java b/src/jexer/TMessageBox.java index c93ad83..6f1e8a6 100644 --- a/src/jexer/TMessageBox.java +++ b/src/jexer/TMessageBox.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import static jexer.TKeypress.*; @@ -187,11 +188,11 @@ public class TMessageBox extends TWindow { // Determine width and height String [] lines = caption.split("\n"); - int width = title.length() + 12; + int width = StringUtils.width(title) + 12; setHeight(6 + lines.length); for (String line: lines) { - if (line.length() + 4 > width) { - width = line.length() + 4; + if (StringUtils.width(line) + 4 > width) { + width = StringUtils.width(line) + 4; } } setWidth(width); diff --git a/src/jexer/TText.java b/src/jexer/TText.java index 60f0e58..4791fdc 100644 --- a/src/jexer/TText.java +++ b/src/jexer/TText.java @@ -33,6 +33,7 @@ import java.util.LinkedList; import java.util.List; import jexer.bits.CellAttributes; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import static jexer.TKeypress.kbDown; @@ -184,7 +185,7 @@ public class TText extends TScrollableWidget { int topY = 0; for (int i = begin; i < lines.size(); i++) { String line = lines.get(i); - if (hScroller.getValue() < line.length()) { + if (hScroller.getValue() < StringUtils.width(line)) { line = line.substring(hScroller.getValue()); } else { line = ""; @@ -323,7 +324,7 @@ public class TText extends TScrollableWidget { * @param line new line to add */ public void addLine(final String line) { - if (text.length() == 0) { + if (StringUtils.width(text) == 0) { text = line; } else { text += "\n\n"; @@ -338,8 +339,8 @@ public class TText extends TScrollableWidget { private void computeBounds() { maxLineWidth = 0; for (String line : lines) { - if (line.length() > maxLineWidth) { - maxLineWidth = line.length(); + if (StringUtils.width(line) > maxLineWidth) { + maxLineWidth = StringUtils.width(line); } } diff --git a/src/jexer/bits/StringUtils.java b/src/jexer/bits/StringUtils.java index d71fd31..f5e2d47 100644 --- a/src/jexer/bits/StringUtils.java +++ b/src/jexer/bits/StringUtils.java @@ -80,14 +80,14 @@ public class StringUtils { // We have just transitioned from a word to // whitespace. See if we have enough space to add // the word to the line. - if (word.length() + line.length() > n) { + if (width(word.toString()) + width(line.toString()) > n) { // This word will exceed the line length. Wrap // at it instead. result.add(line.toString()); line = new StringBuilder(); } if ((word.toString().startsWith(" ")) - && (line.length() == 0) + && (width(line.toString()) == 0) ) { line.append(word.substring(1)); } else { @@ -112,14 +112,14 @@ public class StringUtils { } } // for (int j = 0; j < rawLines[i].length(); j++) - if (word.length() + line.length() > n) { + if (width(word.toString()) + width(line.toString()) > n) { // This word will exceed the line length. Wrap at it // instead. result.add(line.toString()); line = new StringBuilder(); } if ((word.toString().startsWith(" ")) - && (line.length() == 0) + && (width(line.toString()) == 0) ) { line.append(word.substring(1)); } else { @@ -148,7 +148,7 @@ public class StringUtils { List lines = left(str, n); for (String line: lines) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < n - line.length(); i++) { + for (int i = 0; i < n - width(line); i++) { sb.append(' '); } sb.append(line); @@ -175,8 +175,8 @@ public class StringUtils { List lines = left(str, n); for (String line: lines) { StringBuilder sb = new StringBuilder(); - int l = (n - line.length()) / 2; - int r = n - line.length() - l; + int l = (n - width(line)) / 2; + int r = n - width(line) - l; for (int i = 0; i < l; i++) { sb.append(' '); } diff --git a/src/jexer/ttree/TTreeViewWidget.java b/src/jexer/ttree/TTreeViewWidget.java index ceb50ab..9ffb724 100644 --- a/src/jexer/ttree/TTreeViewWidget.java +++ b/src/jexer/ttree/TTreeViewWidget.java @@ -34,6 +34,7 @@ import jexer.TKeypress; import jexer.TScrollableWidget; import jexer.TVScroller; import jexer.TWidget; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import static jexer.TKeypress.*; @@ -279,7 +280,7 @@ public class TTreeViewWidget extends TScrollableWidget { selectedRow++; } - int lineWidth = item.getText().length() + int lineWidth = StringUtils.width(item.getText()) + item.getPrefix().length() + 4; if (lineWidth > maxLineWidth) { maxLineWidth = lineWidth; diff --git a/src/jexer/ttree/TTreeViewWindow.java b/src/jexer/ttree/TTreeViewWindow.java index e570900..f418383 100644 --- a/src/jexer/ttree/TTreeViewWindow.java +++ b/src/jexer/ttree/TTreeViewWindow.java @@ -34,6 +34,7 @@ import jexer.THScroller; import jexer.TScrollableWindow; import jexer.TVScroller; import jexer.TWidget; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; @@ -301,7 +302,7 @@ public class TTreeViewWindow extends TScrollableWindow { selectedRow++; } - int lineWidth = item.getText().length() + int lineWidth = StringUtils.width(item.getText()) + item.getPrefix().length() + 4; if (lineWidth > maxLineWidth) { maxLineWidth = lineWidth; -- 2.27.0