Render group templates with new styles
- Can now render ST group with hardcoded main template name - Add additional style tags - Add sample to list various styles
This commit is contained in:
@@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.stringtemplate.v4.ST;
|
||||
import org.stringtemplate.v4.STErrorListener;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
import org.stringtemplate.v4.STGroupString;
|
||||
import org.stringtemplate.v4.misc.STMessage;
|
||||
|
||||
/**
|
||||
@@ -32,6 +33,7 @@ import org.stringtemplate.v4.misc.STMessage;
|
||||
*/
|
||||
public class TemplateExecutor {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(TemplateExecutor.class);
|
||||
private final static STErrorListener ERROR_LISTENER = new LoggingSTErrorListener();
|
||||
private final ThemeResolver themeResolver;
|
||||
private StringToStyleExpressionRenderer renderer;
|
||||
@@ -61,6 +63,31 @@ public class TemplateExecutor {
|
||||
return themeResolver.evaluateExpression(templateRendered);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render template group with a given attributes expecting to find instance
|
||||
* named {@code main}.
|
||||
*
|
||||
* @param template the ST template
|
||||
* @param attributes the ST template attributes
|
||||
* @return a rendered template
|
||||
*/
|
||||
public AttributedString renderGroup(String template, Map<String, Object> attributes) {
|
||||
STGroup group = new STGroupString(template);
|
||||
group.setListener(ERROR_LISTENER);
|
||||
group.registerRenderer(String.class, renderer);
|
||||
|
||||
ST st = group.getInstanceOf("main");
|
||||
if (st == null) {
|
||||
throw new IllegalArgumentException("template instance 'main' not found from a group");
|
||||
}
|
||||
if (attributes != null) {
|
||||
attributes.entrySet().stream().forEach(e -> st.add(e.getKey(), e.getValue()));
|
||||
}
|
||||
String templateRendered = st.render();
|
||||
log.debug("Rendered template {}", templateRendered);
|
||||
return themeResolver.evaluateExpression(templateRendered);
|
||||
}
|
||||
|
||||
private static class LoggingSTErrorListener implements STErrorListener {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(LoggingSTErrorListener.class);
|
||||
|
||||
@@ -49,8 +49,48 @@ public abstract class ThemeSettings {
|
||||
*/
|
||||
public final static String TAG_LIST_VALUE = "list-value";
|
||||
|
||||
/**
|
||||
* Styling for some arbitrary content indicating {@code INFO} level.
|
||||
*/
|
||||
public final static String TAG_LEVEL_INFO = "level-info";
|
||||
|
||||
/**
|
||||
* Styling for some arbitrary content indicating {@code WARN} level.
|
||||
*/
|
||||
public final static String TAG_LEVEL_WARN = "level-warn";
|
||||
|
||||
/**
|
||||
* Styling for some arbitrary content indicating {@code ERROR} level.
|
||||
*/
|
||||
public final static String TAG_LEVEL_ERROR = "level-error";
|
||||
|
||||
/**
|
||||
* Styling for something i.e. in selectors when item is selectable.
|
||||
*/
|
||||
public final static String TAG_ITEM_ENABLED = "item-enabled";
|
||||
|
||||
/**
|
||||
* Styling for something i.e. in selectors when item can't be selected.
|
||||
*/
|
||||
public final static String TAG_ITEM_DISABLED = "item-disabled";
|
||||
|
||||
/**
|
||||
* Styling for something i.e. in selectors when item is selected.
|
||||
*/
|
||||
public final static String TAG_ITEM_SELECTED = "item-selected";
|
||||
|
||||
/**
|
||||
* Styling for something i.e. in selectors when item is not selected.
|
||||
*/
|
||||
public final static String TAG_ITEM_UNSELECTED = "item-unselected";
|
||||
|
||||
/**
|
||||
* Styling for selector i.e. arrow in selectors.
|
||||
*/
|
||||
public final static String TAG_ITEM_SELECTOR = "item-selector";
|
||||
|
||||
public String title() {
|
||||
return "bold";
|
||||
return "bold,fg:bright-white";
|
||||
}
|
||||
|
||||
public String value() {
|
||||
@@ -58,13 +98,45 @@ public abstract class ThemeSettings {
|
||||
}
|
||||
|
||||
public String listKey() {
|
||||
return null;
|
||||
return "default";
|
||||
}
|
||||
|
||||
public String listValue() {
|
||||
return "bold,fg:green";
|
||||
}
|
||||
|
||||
public String listLevelInfo() {
|
||||
return "fg:green";
|
||||
}
|
||||
|
||||
public String listLevelWarn() {
|
||||
return "fg:yellow";
|
||||
}
|
||||
|
||||
public String listLevelError() {
|
||||
return "fg:red";
|
||||
}
|
||||
|
||||
public String itemEnabled() {
|
||||
return "bold";
|
||||
}
|
||||
|
||||
public String itemDisabled() {
|
||||
return "faint";
|
||||
}
|
||||
|
||||
public String itemSelected() {
|
||||
return "fg:green";
|
||||
}
|
||||
|
||||
public String itemUnselected() {
|
||||
return "bold";
|
||||
}
|
||||
|
||||
public String itemSelector() {
|
||||
return "bold,fg:bright-cyan";
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a theme setting from a given tag.
|
||||
*
|
||||
@@ -81,6 +153,22 @@ public abstract class ThemeSettings {
|
||||
return listKey();
|
||||
case TAG_LIST_VALUE:
|
||||
return listValue();
|
||||
case TAG_LEVEL_INFO:
|
||||
return listLevelInfo();
|
||||
case TAG_LEVEL_WARN:
|
||||
return listLevelWarn();
|
||||
case TAG_LEVEL_ERROR:
|
||||
return listLevelError();
|
||||
case TAG_ITEM_ENABLED:
|
||||
return itemEnabled();
|
||||
case TAG_ITEM_DISABLED:
|
||||
return itemDisabled();
|
||||
case TAG_ITEM_SELECTED:
|
||||
return itemSelected();
|
||||
case TAG_ITEM_UNSELECTED:
|
||||
return itemUnselected();
|
||||
case TAG_ITEM_SELECTOR:
|
||||
return itemSelector();
|
||||
}
|
||||
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ import java.util.Map;
|
||||
|
||||
import org.jline.utils.AttributedString;
|
||||
import org.jline.utils.AttributedStringBuilder;
|
||||
import org.jline.utils.AttributedStyle;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.jline.utils.AttributedStyle.BOLD;
|
||||
|
||||
public class TemplateExecutorTests {
|
||||
|
||||
@@ -74,7 +74,10 @@ public class TemplateExecutorTests {
|
||||
Map<String, Object> attributes = new HashMap<>();
|
||||
attributes.put("foo", "bar");
|
||||
AttributedString result = executor.render(template, attributes);
|
||||
AttributedString equalTo = new AttributedStringBuilder().append("bar", BOLD).toAttributedString();
|
||||
AttributedString equalTo = new AttributedStringBuilder()
|
||||
.append("bar",
|
||||
AttributedStyle.DEFAULT.foreground(AttributedStyle.BRIGHT + AttributedStyle.WHITE).bold())
|
||||
.toAttributedString();
|
||||
assertThat(result).isEqualTo(equalTo);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ThemeResolverTests {
|
||||
}
|
||||
});
|
||||
ThemeResolver themeResolver = new ThemeResolver(themeRegistry, "default");
|
||||
assertThat(themeResolver.resolveTag(ThemeSettings.TAG_TITLE)).isEqualTo("bold");
|
||||
assertThat(themeResolver.resolveTag(ThemeSettings.TAG_TITLE)).isEqualTo("bold,fg:bright-white");
|
||||
assertThat(themeResolver.resolveStyle("bold")).isEqualTo(AttributedStyle.BOLD);
|
||||
assertThat(themeResolver.evaluateExpression("@{bold foo}"))
|
||||
.isEqualTo(new AttributedString("foo", AttributedStyle.BOLD));
|
||||
|
||||
@@ -29,12 +29,13 @@ public class ThemeSettingsTests {
|
||||
public void test() {
|
||||
ThemeSettings themeSettings = ThemeSettings.themeSettings();
|
||||
String resolveTag = themeSettings.resolveTag(ThemeSettings.TAG_TITLE);
|
||||
assertThat(resolveTag).isEqualTo("bold");
|
||||
assertThat(resolveTag).isEqualTo("bold,fg:bright-white");
|
||||
|
||||
StyleSource styleSource = new MemoryStyleSource();
|
||||
StyleResolver styleResolver = new StyleResolver(styleSource, "default");
|
||||
AttributedStyle style = styleResolver.resolve(resolveTag);
|
||||
assertThat(style).isEqualTo(AttributedStyle.BOLD);
|
||||
assertThat(style)
|
||||
.isEqualTo(AttributedStyle.DEFAULT.foreground(AttributedStyle.BRIGHT + AttributedStyle.WHITE).bold());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user