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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user