Skip to content

Commit

Permalink
Add $m: prefix to custom usercolors/badges
Browse files Browse the repository at this point in the history
  • Loading branch information
tduva committed Jan 12, 2021
1 parent b697439 commit 938988e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/chatty/gui/Highlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,10 @@ public boolean matches(Type type, String text, String channel, Addressbook ab) {
return matches(type, text, null, channel, ab, null, null, MsgTags.EMPTY);
}

public boolean matches(User user) {
return matches(Type.ANY, "", null, null, null, user, null, MsgTags.EMPTY);
}

/**
* Check whether a message matches this item.
*
Expand Down
14 changes: 13 additions & 1 deletion src/chatty/gui/colors/UsercolorItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package chatty.gui.colors;

import chatty.Helper;
import chatty.gui.Highlighter;
import chatty.util.colors.HtmlColors;
import java.awt.Color;
import java.util.Arrays;
Expand All @@ -14,7 +15,7 @@
* @author tduva
*/
public class UsercolorItem extends ColorItem {

private static final Set<String> statusDef = new HashSet<>(Arrays.asList(
"$mod", "$sub", "$admin", "$staff", "$turbo", "$broadcaster", "$bot",
"$globalmod", "$anymod", "$vip"));
Expand All @@ -25,6 +26,7 @@ public class UsercolorItem extends ColorItem {
public static final int TYPE_ALL = 3;
public static final int TYPE_CATEGORY = 4;
public static final int TYPE_DEFAULT_COLOR = 5;
public static final int TYPE_MATCH = 6;
public static final int TYPE_UNDEFINED = -1;

public final Color color;
Expand All @@ -33,6 +35,7 @@ public class UsercolorItem extends ColorItem {
public final Color idColor;
public final int type;
public final String category;
public final Highlighter.HighlightItem match;

public UsercolorItem(String id, Color color) {
super(id, color, true, null, false);
Expand All @@ -57,11 +60,20 @@ public UsercolorItem(String id, Color color) {
category = null;
}

if (id.startsWith("$m:") && id.length() > "$m:".length()) {
match = new Highlighter.HighlightItem(id.substring("$m:".length()));
}
else {
match = null;
}

// Save the type
if (idColor != null) {
type = TYPE_COLOR;
} else if (id.startsWith("$cat:") && id.length() > 5) {
type = TYPE_CATEGORY;
} else if (match != null) {
type = TYPE_MATCH;
} else if (statusDef.contains(id)) {
type = TYPE_STATUS;
} else if (Helper.isValidChannel(id)) {
Expand Down
7 changes: 7 additions & 0 deletions src/chatty/gui/colors/UsercolorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import chatty.Helper;
import chatty.User;
import chatty.gui.Highlighter;
import chatty.util.colors.HtmlColors;
import chatty.util.irc.MsgTags;
import chatty.util.settings.Settings;
import java.awt.Color;
import java.util.ArrayList;
Expand Down Expand Up @@ -125,6 +127,11 @@ else if (item.type == UsercolorItem.TYPE_CATEGORY) {
return item.color;
}
}
else if (item.type == UsercolorItem.TYPE_MATCH) {
if (item.match.matches(user)) {
return item.color;
}
}
else if (item.type == UsercolorItem.TYPE_DEFAULT_COLOR) {
if (user.hasDefaultColor()) {
return item.color;
Expand Down
15 changes: 11 additions & 4 deletions src/chatty/gui/components/help/help-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ <h2>
as hardcoded into Chatty, may be different from other programs), e.g. <code>$color:Blue</code> for blue,
which can be used to replace colors</li>
<li><code>$defaultColor</code> - Users that don't have a color set and have the default assigned color (somewhat random)</li>
<li><code>$m:&lt;item&gt;</code> - Specify user-related <a href="#Highlight_Meta_Matching">Highlight Matching</a> prefixes, such as
<code>$m:config:b|partner</code> to match users with a partner badge</li>
</ul>

<p>The order of the
Expand Down Expand Up @@ -230,11 +232,16 @@ <h3>Custom Badges Properties</h3>
<dt>Restriction</dt>
<dd>This is similiar to the <a href="#Usercolors">Usercolors</a> settings,
where you can enter special restrictions the user has to match in order
for the icon to be displayed. What makes the most sense in this case is
probably just entering a username (e.g. <code>serenity</code>) or an
addressbook category (e.g. <code>$cat:vip</code>, which would refer to
the category <code>vip</code>). If you keep this empty, then no restriction
for the icon to be displayed. If you keep this empty, then no restriction
is applied.</dd>
<dd>The most common restrictions would be (although most <a href="#Usercolors">Usercolors</a>
restrictions should work):
<ul>
<li>Just the username (e.g. <code>serenity</code>)</li>
<li>An addressbook category (e.g. <code>$cat:vip</code>, which would refer to
the category <code>vip</code>)</li>
</ul>
</dd>
<dd>Specifying a <em>Type</em> other than <code>Addon</code> carries an
implicit restriction, since e.g. the default moderator icons are only
displayed if the user is a moderator.</dd>
Expand Down
12 changes: 1 addition & 11 deletions src/chatty/gui/components/settings/UsercolorSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@
public class UsercolorSettings extends SettingsPanel {

private static final String INFO_TEXT = "<html><body style='width:105px'>"
+ "Add items to the list to assign them colors. The order matters, items "
+ "on the top are matched first.<br /><br />"
+ "Special Items:<br />"
+ "$mod - Moderators<br />"
+ "$sub - Subscribers<br />"
+ "$turbo - Turbo Users<br />"
+ "$admin - Admin<br />"
+ "$staff - Staff<br />"
+ "$all - All Users<br />"
+ "<br />"
+ "[help:Usercolors And more..]";
+SettingsUtil.getInfo("info-usercolors.html", null);

private final ItemColorEditor<UsercolorItem> data;
private Color defaultBackgroundColor;
Expand Down
10 changes: 10 additions & 0 deletions src/chatty/gui/components/settings/info-usercolors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Add items to the list to assign them colors. The order matters, items on the top are matched first.<br /><br />
Special Items:<br />
<code>$mod</code> - Moderators<br />
<code>$sub</code> - Subscribers<br />
<code>$turbo</code> - Turbo Users<br />
<code>$admin</code> - Admin<br />
<code>$staff</code> - Staff<br />
<code>$all</code> - All Users<br />
<br />
[help:Usercolors And more..]
18 changes: 17 additions & 1 deletion src/chatty/util/api/usericons/Usericon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package chatty.util.api.usericons;

import chatty.Helper;
import chatty.gui.Highlighter;
import chatty.util.colors.HtmlColors;
import chatty.util.ImageCache;
import chatty.util.StringUtil;
Expand Down Expand Up @@ -104,7 +105,7 @@ public static Type getTypeFromId(int typeId) {
* restriction doesn't have to be parsed everytime.
*/
public enum MatchType {
CATEGORY, UNDEFINED, ALL, STATUS, NAME, COLOR
CATEGORY, UNDEFINED, ALL, STATUS, NAME, COLOR, MATCH
}

/**
Expand Down Expand Up @@ -203,6 +204,11 @@ public enum MatchType {
*/
public final String category;

/**
* Match user-related prefixes based on the Highlighting format.
*/
public final Highlighter.HighlightItem match;

/**
* This is {@code true} if the channel restriction should be reversed, which
* means all channels BUT the one specified should match.
Expand Down Expand Up @@ -329,6 +335,13 @@ public Usericon(Builder builder) {
category = null;
}

if (restrict.startsWith("$m:") && restrict.length() > "$m:".length()) {
match = new Highlighter.HighlightItem(restrict.substring("$m:".length()));
}
else {
match = null;
}

if (restrict.startsWith("#") && restrict.length() == 7) {
colorRestriction = HtmlColors.decode(restrict, null);
} else if (restrict.startsWith("$color:") && restrict.length() > 7) {
Expand All @@ -340,6 +353,8 @@ public Usericon(Builder builder) {
// Save the type
if (restrict.startsWith("$cat:") && restrict.length() > 5) {
matchType = MatchType.CATEGORY;
} else if (match != null) {
matchType = MatchType.MATCH;
} else if (colorRestriction != null) {
matchType = MatchType.COLOR;
} else if (statusDef.contains(restrict)) {
Expand All @@ -354,6 +369,7 @@ public Usericon(Builder builder) {
} else {
matchType = MatchType.UNDEFINED;
category = null;
match = null;
this.restriction = null;
restrictionValue = null;
colorRestriction = null;
Expand Down
4 changes: 4 additions & 0 deletions src/chatty/util/api/usericons/UsericonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ else if (!usernameR || !useridR) {
if (user.hasCategory(icon.category)) {
return true;
}
} else if (icon.matchType == Usericon.MatchType.MATCH) {
if (icon.match.matches(user)) {
return true;
}
} else if (icon.matchType == Usericon.MatchType.STATUS) {
if (Helper.matchUserStatus(icon.restrictionValue, user)) {
return true;
Expand Down

0 comments on commit 938988e

Please sign in to comment.