001package squidpony.panel;
002
003/**
004 * An interface that lets non-display code request some special rendering for a {@code T} value, and an implementation
005 * can handle this appropriately in display code. Much of the time, IMarkup will not be directly used in games, and it
006 * may have much of its value in library code that can use it to declare that some value should be shown in larger text,
007 * or in a certain color, without specifying the exact details of what shade of what color or precisely how much larger
008 * the text should be. However, there may be good uses for highly-specific custom implementations of IMarkup for types
009 * present only in one game, where {@code T} may be {@code Creature} or some other class specific to a game, and the
010 * IMarkup can be used to get a special String to describe or display that Creature with any color/size markup or even
011 * a String that changes over time. The {@link #getMarkup(Object)} method yields the appropriate text or markup to
012 * describe its parameter, and the {@link #closeMarkup()} method yields text that ends any block of markup that is
013 * ongoing. For the example of an IMarkup for colors, getMarkup would be given a color parameter and would color any
014 * text in that color or a variant on it until the markup is closed with closeMarkup, or possibly until the color is
015 * specified again via getMarkup. For the example of IMarkup for Creatures, getMarkup would produce a String that
016 * describes the Creature it is given as a parameter (if it uses colors/sizes as well, it should close itself to be
017 * self-contained), and closeMarkup would produce the empty String.
018 * Created by Tommy Ettinger on 1/23/2016.
019 */
020public interface IMarkup<T> {
021    /**
022     * Implementations should use this method to get a String that describes the given value, or begins some section of
023     * markup that uses a quality specified by value, such as a color or text size.
024     * @param value an object of type T that can be described by this IMarkup implementation
025     * @return a String either describing the value or starting a section of markup using the value.
026     */
027    String getMarkup(T value);
028
029    /**
030     * Implementations should use this method to get a String that ends any section of markup currently ongoing.
031     * This may be the empty String in many cases.
032     * @return the requisite String to end any ongoing sections of markup
033     */
034    String closeMarkup();
035
036    // escape() is currently commented out because I don't know if it is needed.
037    /*
038     * If the Strings used as markup may occur in normal text, you can call this on the text before applying markup to
039     * escape the not-actually-markup Strings in the text. If your variety of markup doesn't have a concept of escaping,
040     * this can return initialText as-is.
041     * @param initialText the text to process, before applying markup
042     * @return a String made from initialText, with any pre-existing Strings that could be read as markup escaped
043     * /
044    String escape(String initialText);
045     */
046    /**
047     * Probably not that useful on its own, but may be good as an example.
048     * @see squidpony.panel.IMarkup IMarkup has more complete documentation on how it should be used
049     */
050    class StringMarkup implements IMarkup<String>
051    {
052        @Override
053        public String getMarkup(String value) {
054            return "[" + value + "]";
055        }
056
057        @Override
058        public String closeMarkup() {
059            return "[]";
060        }
061
062        /*
063        @Override
064        public String escape(String initialText)
065        {
066            return initialText.replace("[", "[[");
067        }
068        */
069    }
070}