X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbits%2FColorTheme.java;h=baf7685b4ef2331a2cb07a41e67700bc745a4c00;hb=a0d734e68fc28e441d74075e4d8d0166bbcde180;hp=caf7b434d59e16912025113754817dcdb4f8e96a;hpb=9ff1c0e327bfa10c154cdb051d9c15a8fa96e7f8;p=fanfix.git diff --git a/src/jexer/bits/ColorTheme.java b/src/jexer/bits/ColorTheme.java index caf7b43..baf7685 100644 --- a/src/jexer/bits/ColorTheme.java +++ b/src/jexer/bits/ColorTheme.java @@ -118,6 +118,54 @@ public final class ColorTheme { load(new FileReader(filename)); } + /** + * Set a color based on a text string. Color text string is of the form: + * [ bold ] [ blink ] { foreground on background } + * + * @param key the color key string + * @param text the text string + */ + public void setColorFromString(final String key, final String text) { + boolean bold = false; + boolean blink = false; + String foreColor; + String backColor; + String token; + + StringTokenizer tokenizer = new StringTokenizer(text); + token = tokenizer.nextToken(); + while (token.equals("bold") || token.equals("blink")) { + if (token.equals("bold")) { + bold = true; + token = tokenizer.nextToken(); + } + if (token.equals("blink")) { + blink = true; + token = tokenizer.nextToken(); + } + } + + // What's left is "blah on blah" + foreColor = token.toLowerCase(); + + if (!tokenizer.nextToken().toLowerCase().equals("on")) { + // Invalid line. + return; + } + backColor = tokenizer.nextToken().toLowerCase(); + + CellAttributes color = new CellAttributes(); + if (bold) { + color.setBold(true); + } + if (blink) { + color.setBlink(true); + } + color.setForeColor(Color.getColor(foreColor)); + color.setBackColor(Color.getColor(backColor)); + colors.put(key, color); + } + /** * Read color theme mappings from a Reader. The reader is closed at the * end. @@ -129,56 +177,19 @@ public final class ColorTheme { BufferedReader bufferedReader = new BufferedReader(reader); String line = bufferedReader.readLine(); for (; line != null; line = bufferedReader.readLine()) { - String key; - boolean bold = false; - boolean blink = false; - String foreColor; - String backColor; - String token; - // Look for lines that resemble: // "key = blah on blah" // "key = bold blah on blah" // "key = blink bold blah on blah" // "key = bold blink blah on blah" // "key = blink blah on blah" - StringTokenizer tokenizer = new StringTokenizer(line); - key = tokenizer.nextToken(); - if (!tokenizer.nextToken().equals("=")) { - // Skip this line + if (line.indexOf('=') == -1) { + // Invalid line. continue; } - token = tokenizer.nextToken(); - while (token.equals("bold") || token.equals("blink")) { - if (token.equals("bold")) { - bold = true; - token = tokenizer.nextToken(); - } - if (token.equals("blink")) { - blink = true; - token = tokenizer.nextToken(); - } - } - - // What's left is "blah on blah" or "blah" - foreColor = token.toLowerCase(); - - if (!tokenizer.nextToken().toLowerCase().equals("on")) { - // Skip this line - continue; - } - backColor = tokenizer.nextToken().toLowerCase(); - - CellAttributes color = new CellAttributes(); - if (bold) { - color.setBold(true); - } - if (blink) { - color.setBlink(true); - } - color.setForeColor(Color.getColor(foreColor)); - color.setBackColor(Color.getColor(backColor)); - colors.put(key, color); + String key = line.substring(0, line.indexOf(':')).trim(); + String text = line.substring(line.indexOf(':') + 1); + setColorFromString(key, text); } // All done. bufferedReader.close(); @@ -495,4 +506,14 @@ public final class ColorTheme { } + /** + * Make human-readable description of this Cell. + * + * @return displayable String + */ + @Override + public String toString() { + return colors.toString(); + } + }