Class GrailsConsole

  • All Implemented Interfaces:
    ConsoleLogger
    Direct Known Subclasses:
    GrailsEclipseConsole

    public class GrailsConsole
    extends java.lang.Object
    implements ConsoleLogger
    Utility class for delivering console output in a nicely formatted way.
    Since:
    2.0
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected GrailsConsole()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addShutdownHook()  
      void addStatus​(java.lang.String msg)
      Keeps doesn't replace the status message
      protected java.io.OutputStream ansiWrap​(java.io.OutputStream out)
      Hook method that allows controlling whether or not output streams should be wrapped by AnsiConsole.wrapOutputStream.
      void append​(java.lang.String msg)  
      void beforeShutdown()  
      protected void bindSystemOutAndErr​(java.io.PrintStream systemOut, java.io.PrintStream systemErr)  
      void cleanlyExit​(int status)  
      protected jline.console.ConsoleReader createConsoleReader​(java.io.InputStream systemIn)  
      static GrailsConsole createInstance()  
      protected jline.Terminal createTerminal()
      Creates the instance of Terminal used directly in GrailsConsole.
      void echoStatus()
      Replays the last status message
      void error​(java.lang.String msg)
      Prints an error message
      void error​(java.lang.String label, java.lang.String message)
      Log an error with a specific error label
      void error​(java.lang.String msg, java.lang.Throwable error)
      Use to log an error
      void error​(java.lang.Throwable error)
      Use to log an error
      void flush()
      Makes sure that the console has been reset to the default state and that the out stream has been flushed.
      java.util.Stack<java.lang.String> getCategory()  
      java.lang.Character getDefaultInputMask()  
      java.io.PrintStream getErr()  
      java.io.InputStream getInput()  
      static GrailsConsole getInstance()  
      java.lang.String getLastMessage()  
      java.io.PrintStream getOut()  
      jline.console.ConsoleReader getReader()  
      jline.Terminal getTerminal()  
      void indicateProgress()
      Indicates progress with the default progress indicator
      void indicateProgress​(int number)
      Indicates progress by number
      void indicateProgress​(int number, int total)
      Indicate progress for a number and total
      void indicateProgressPercentage​(long number, long total)
      Indicates progress as a percentage for the given number and total
      void info​(java.lang.String msg)
      Synonym for #log
      protected void initialize​(java.io.InputStream systemIn, java.io.PrintStream systemOut, java.io.PrintStream systemErr)  
      boolean isAnsiEnabled()  
      boolean isInteractiveEnabled()  
      boolean isStacktrace()  
      boolean isVerbose()  
      boolean isWindows()  
      void log​(java.lang.String msg)
      Logs a message below the current status message
      protected void persistHistory()  
      protected jline.console.history.History prepareHistory()
      Prepares a history file to be used by the ConsoleReader.
      protected void redirectSystemOutAndErr​(boolean force)  
      void reinitialize​(java.io.InputStream systemIn, java.io.PrintStream systemOut, java.io.PrintStream systemErr)
      Use in testing when System.out, System.err or System.in change
      static void removeInstance()  
      void removeShutdownHook()  
      void resetCompleters()  
      void restoreOriginalSystemOutAndErr()  
      protected void restoreTerminal()  
      java.lang.String secureUserInput​(java.lang.String msg)
      Like userInput(String) except that the user's entered characters will be replaced with '*' on the CLI, masking the input (i.e.
      void setAnsiEnabled​(boolean ansiEnabled)  
      void setDefaultInputMask​(java.lang.Character defaultInputMask)  
      void setErr​(java.io.PrintStream err)  
      static void setInstance​(GrailsConsole newConsole)  
      void setLastMessage​(java.lang.String lastMessage)  
      void setOut​(java.io.PrintStream out)  
      void setStacktrace​(boolean stacktrace)  
      void setVerbose​(boolean verbose)  
      java.lang.String showPrompt()
      Shows the prompt to request user input
      void updateStatus​(java.lang.String msg)
      Updates the current state message
      java.lang.String userInput​(java.lang.String msg)
      Replacement for AntBuilder.input() to eliminate dependency of GrailsScriptRunner on the Ant libraries.
      java.lang.String userInput​(java.lang.String message, java.lang.String[] validResponses)
      Replacement for AntBuilder.input() to eliminate dependency of GrailsScriptRunner on the Ant libraries.
      java.lang.String userInput​(java.lang.String message, java.util.List<java.lang.String> validResponses)  
      void verbose​(java.lang.String msg)
      Outputs a verbose message
      void warn​(java.lang.String msg)
      Prints a warn message
      void warning​(java.lang.String msg)
      Prints an error message
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ENABLE_INTERACTIVE

        public static final java.lang.String ENABLE_INTERACTIVE
        See Also:
        Constant Field Values
      • LINE_SEPARATOR

        public static final java.lang.String LINE_SEPARATOR
      • CATEGORY_SEPARATOR

        public static final java.lang.String CATEGORY_SEPARATOR
        See Also:
        Constant Field Values
      • STACKTRACE_FILTERED_MESSAGE

        public static final java.lang.String STACKTRACE_FILTERED_MESSAGE
        See Also:
        Constant Field Values
      • STACKTRACE_MESSAGE

        public static final java.lang.String STACKTRACE_MESSAGE
        See Also:
        Constant Field Values
      • SECURE_MASK_CHAR

        public static final java.lang.Character SECURE_MASK_CHAR
    • Constructor Detail

      • GrailsConsole

        protected GrailsConsole()
                         throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • addShutdownHook

        public void addShutdownHook()
      • removeShutdownHook

        public void removeShutdownHook()
      • reinitialize

        public void reinitialize​(java.io.InputStream systemIn,
                                 java.io.PrintStream systemOut,
                                 java.io.PrintStream systemErr)
                          throws java.io.IOException
        Use in testing when System.out, System.err or System.in change
        Throws:
        java.io.IOException
      • initialize

        protected void initialize​(java.io.InputStream systemIn,
                                  java.io.PrintStream systemOut,
                                  java.io.PrintStream systemErr)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • bindSystemOutAndErr

        protected void bindSystemOutAndErr​(java.io.PrintStream systemOut,
                                           java.io.PrintStream systemErr)
      • getErr

        public java.io.PrintStream getErr()
      • setErr

        public void setErr​(java.io.PrintStream err)
      • setOut

        public void setOut​(java.io.PrintStream out)
      • isInteractiveEnabled

        public boolean isInteractiveEnabled()
      • createConsoleReader

        protected jline.console.ConsoleReader createConsoleReader​(java.io.InputStream systemIn)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • createTerminal

        protected jline.Terminal createTerminal()
        Creates the instance of Terminal used directly in GrailsConsole. Note that there is also another terminal instance created implicitly inside of ConsoleReader. That instance is controlled by the jline.terminal system property.
      • resetCompleters

        public void resetCompleters()
      • prepareHistory

        protected jline.console.history.History prepareHistory()
                                                        throws java.io.IOException
        Prepares a history file to be used by the ConsoleReader. This file will live in the home directory of the user.
        Throws:
        java.io.IOException
      • ansiWrap

        protected java.io.OutputStream ansiWrap​(java.io.OutputStream out)
        Hook method that allows controlling whether or not output streams should be wrapped by AnsiConsole.wrapOutputStream. Unfortunately, Eclipse consoles will look to the AnsiWrap like they do not understand ansi, even if we were to implement support in Eclipse to' handle it and the wrapped stream will not pass the ansi chars on to Eclipse).
      • isWindows

        public boolean isWindows()
      • removeInstance

        public static void removeInstance()
      • beforeShutdown

        public void beforeShutdown()
      • restoreTerminal

        protected void restoreTerminal()
      • persistHistory

        protected void persistHistory()
      • setInstance

        public static void setInstance​(GrailsConsole newConsole)
      • redirectSystemOutAndErr

        protected void redirectSystemOutAndErr​(boolean force)
      • createInstance

        public static GrailsConsole createInstance()
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • setAnsiEnabled

        public void setAnsiEnabled​(boolean ansiEnabled)
      • setVerbose

        public void setVerbose​(boolean verbose)
        Parameters:
        verbose - Sets whether verbose output should be used
      • setStacktrace

        public void setStacktrace​(boolean stacktrace)
        Parameters:
        stacktrace - Sets whether to show stack traces on errors
      • isVerbose

        public boolean isVerbose()
        Returns:
        Whether verbose output is being used
      • isStacktrace

        public boolean isStacktrace()
        Returns:
        Whether to show stack traces
      • getInput

        public java.io.InputStream getInput()
        Returns:
        The input stream being read from
      • getLastMessage

        public java.lang.String getLastMessage()
        Returns:
        The last message logged
      • setLastMessage

        public void setLastMessage​(java.lang.String lastMessage)
      • getReader

        public jline.console.ConsoleReader getReader()
      • getTerminal

        public jline.Terminal getTerminal()
      • getOut

        public java.io.PrintStream getOut()
      • getCategory

        public java.util.Stack<java.lang.String> getCategory()
      • indicateProgress

        public void indicateProgress()
        Indicates progress with the default progress indicator
        Specified by:
        indicateProgress in interface ConsoleLogger
      • indicateProgress

        public void indicateProgress​(int number,
                                     int total)
        Indicate progress for a number and total
        Specified by:
        indicateProgress in interface ConsoleLogger
        Parameters:
        number - The current number
        total - The total number
      • indicateProgressPercentage

        public void indicateProgressPercentage​(long number,
                                               long total)
        Indicates progress as a percentage for the given number and total
        Specified by:
        indicateProgressPercentage in interface ConsoleLogger
        Parameters:
        number - The number
        total - The total
      • indicateProgress

        public void indicateProgress​(int number)
        Indicates progress by number
        Specified by:
        indicateProgress in interface ConsoleLogger
        Parameters:
        number - The number
      • updateStatus

        public void updateStatus​(java.lang.String msg)
        Updates the current state message
        Specified by:
        updateStatus in interface ConsoleLogger
        Parameters:
        msg - The message
      • addStatus

        public void addStatus​(java.lang.String msg)
        Keeps doesn't replace the status message
        Specified by:
        addStatus in interface ConsoleLogger
        Parameters:
        msg - The message
      • error

        public void error​(java.lang.String msg)
        Prints an error message
        Specified by:
        error in interface ConsoleLogger
        Parameters:
        msg - The error message
      • warning

        public void warning​(java.lang.String msg)
        Prints an error message
        Specified by:
        warning in interface ConsoleLogger
        Parameters:
        msg - The error message
      • warn

        public void warn​(java.lang.String msg)
        Prints a warn message
        Specified by:
        warn in interface ConsoleLogger
        Parameters:
        msg - The message
      • isAnsiEnabled

        public boolean isAnsiEnabled()
      • error

        public void error​(java.lang.String msg,
                          java.lang.Throwable error)
        Use to log an error
        Specified by:
        error in interface ConsoleLogger
        Parameters:
        msg - The message
        error - The error
      • error

        public void error​(java.lang.Throwable error)
        Use to log an error
        Specified by:
        error in interface ConsoleLogger
        Parameters:
        error - The error
      • log

        public void log​(java.lang.String msg)
        Logs a message below the current status message
        Specified by:
        log in interface ConsoleLogger
        Parameters:
        msg - The message to log
      • append

        public void append​(java.lang.String msg)
      • info

        public void info​(java.lang.String msg)
        Synonym for #log
        Specified by:
        info in interface ConsoleLogger
        Parameters:
        msg - The message to log
      • verbose

        public void verbose​(java.lang.String msg)
        Description copied from interface: ConsoleLogger
        Outputs a verbose message
        Specified by:
        verbose in interface ConsoleLogger
        Parameters:
        msg - The message
      • echoStatus

        public void echoStatus()
        Replays the last status message
      • userInput

        public java.lang.String userInput​(java.lang.String msg)
        Replacement for AntBuilder.input() to eliminate dependency of GrailsScriptRunner on the Ant libraries. Prints a message and returns whatever the user enters (once they press <return>).
        Parameters:
        msg - The message/question to display.
        Returns:
        The line of text entered by the user. May be a blank string.
      • secureUserInput

        public java.lang.String secureUserInput​(java.lang.String msg)
        Like userInput(String) except that the user's entered characters will be replaced with '*' on the CLI, masking the input (i.e. suitable for capturing passwords etc.).
        Parameters:
        msg - The message/question to display.
        Returns:
        The line of text entered by the user. May be a blank string.
      • showPrompt

        public java.lang.String showPrompt()
        Shows the prompt to request user input
        Returns:
        The user input prompt
      • userInput

        public java.lang.String userInput​(java.lang.String message,
                                          java.util.List<java.lang.String> validResponses)
      • userInput

        public java.lang.String userInput​(java.lang.String message,
                                          java.lang.String[] validResponses)
        Replacement for AntBuilder.input() to eliminate dependency of GrailsScriptRunner on the Ant libraries. Prints a message and list of valid responses, then returns whatever the user enters (once they press <return>). If the user enters something that is not in the array of valid responses, the message is displayed again and the method waits for more input. It will display the message a maximum of three times before it gives up and returns null.
        Parameters:
        message - The message/question to display.
        validResponses - An array of responses that the user is allowed to enter. Displayed after the message.
        Returns:
        The line of text entered by the user, or null if the user never entered a valid string.
      • error

        public void error​(java.lang.String label,
                          java.lang.String message)
        Description copied from interface: ConsoleLogger
        Log an error with a specific error label
        Specified by:
        error in interface ConsoleLogger
        Parameters:
        label - The label
        message - The message
      • restoreOriginalSystemOutAndErr

        public void restoreOriginalSystemOutAndErr()
      • cleanlyExit

        public void cleanlyExit​(int status)
      • flush

        public void flush()
        Makes sure that the console has been reset to the default state and that the out stream has been flushed.
      • getDefaultInputMask

        public java.lang.Character getDefaultInputMask()
      • setDefaultInputMask

        public void setDefaultInputMask​(java.lang.Character defaultInputMask)