X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbits%2FMnemonicString.java;h=58575b570cf0f84330aa93859e8c36d974b798e2;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=f35eb50dc94d47fa8458e2bee93c27c5eebef4d2;hpb=051e29138b18fb4b731a72f8727475b10e4c74e4;p=fanfix.git diff --git a/src/jexer/bits/MnemonicString.java b/src/jexer/bits/MnemonicString.java index f35eb50..58575b5 100644 --- a/src/jexer/bits/MnemonicString.java +++ b/src/jexer/bits/MnemonicString.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"), @@ -43,13 +43,19 @@ public class MnemonicString { /** * Keyboard shortcut to activate this item. */ - private char shortcut; + private int shortcut; /** * Location of the highlighted character. */ private int shortcutIdx = -1; + /** + * Screen location of the highlighted character (number of text cells + * required to display from the beginning to shortcutIdx). + */ + private int screenShortcutIdx = -1; + /** * The raw (uncolored) string. */ @@ -68,34 +74,40 @@ public class MnemonicString { public MnemonicString(final String label) { // Setup the menu shortcut - String newLabel = ""; + StringBuilder newLabel = new StringBuilder(); boolean foundAmp = false; boolean foundShortcut = false; int scanShortcutIdx = 0; - for (int i = 0; i < label.length(); i++) { - char c = label.charAt(i); + int scanScreenShortcutIdx = 0; + for (int i = 0; i < label.length();) { + int c = label.codePointAt(i); + i += Character.charCount(c); + if (c == '&') { if (foundAmp) { - newLabel += '&'; + newLabel.append('&'); scanShortcutIdx++; + scanScreenShortcutIdx++; } else { foundAmp = true; } } else { - newLabel += c; + newLabel.append(Character.toChars(c)); if (foundAmp) { if (!foundShortcut) { shortcut = c; foundAmp = false; foundShortcut = true; shortcutIdx = scanShortcutIdx; + screenShortcutIdx = scanScreenShortcutIdx; } } else { scanShortcutIdx++; + scanScreenShortcutIdx += StringUtils.width(c); } } } - this.rawLabel = newLabel; + this.rawLabel = newLabel.toString(); } // ------------------------------------------------------------------------ @@ -107,7 +119,7 @@ public class MnemonicString { * * @return the highlighted character */ - public char getShortcut() { + public int getShortcut() { return shortcut; } @@ -120,6 +132,16 @@ public class MnemonicString { return shortcutIdx; } + /** + * Get the screen location of the highlighted character. + * + * @return the number of text cells required to display from the + * beginning of the label to shortcutIdx + */ + public int getScreenShortcutIdx() { + return screenShortcutIdx; + } + /** * Get the raw (uncolored) string. *