Skip to content

Commit

Permalink
chore: added ClientTextComponentUtils.ellipsize()
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Jul 19, 2024
1 parent 8ea9402 commit ec6c646
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import dev.ftb.mods.ftblibrary.util.CustomComponentParser;
import dev.ftb.mods.ftblibrary.util.StringUtils;
import dev.ftb.mods.ftblibrary.util.TextComponentParser;
import net.minecraft.client.gui.Font;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.*;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static net.minecraft.network.chat.CommonComponents.ELLIPSIS;

public class ClientTextComponentUtils {
private static final Function<String, Component> DEFAULT_STRING_TO_COMPONENT = ClientTextComponentUtils::defaultStringToComponent;

Expand Down Expand Up @@ -59,4 +62,15 @@ private static Component defaultStringToComponent(String s) {

return parse(I18n.get(s));
}

public static FormattedText ellipsize(Font font, FormattedText text, int maxWidth) {
final int strWidth = font.width(text);
final int ellipsisWidth = font.width(ELLIPSIS);
if (strWidth > maxWidth) {
return ellipsisWidth >= maxWidth ?
font.substrByWidth(text, maxWidth) :
FormattedText.composite(font.substrByWidth(text, maxWidth - ellipsisWidth), ELLIPSIS);
}
return text;
}
}

0 comments on commit ec6c646

Please sign in to comment.