Class Terminal

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, LinePrinter

    public class Terminal
    extends CharReader
    implements java.io.Closeable, LinePrinter
    Terminal interface. It sets proper TTY mode and reads complex characters from the input, and writes lines dependent on terminal mode.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      boolean confirm​(java.lang.String what)
      Make a user confirmation.
      boolean confirm​(java.lang.String what, boolean def)
      Make a user confirmation.
      void executeAbortable​(java.util.concurrent.ExecutorService exec, java.lang.Runnable callable)
      Execute runnable, which may not be interruptable by itself, but listen to terminal input and abort the task if CTRL-C is pressed.
      <T> T executeAbortable​(java.util.concurrent.ExecutorService exec, java.util.concurrent.Callable<T> callable)
      Execute callable, which may not be interruptable by itself, but listen to terminal input and abort the task if CTRL-C is pressed.
      void finish()
      Finish the current set of lines and continue below.
      void format​(java.lang.String format, java.lang.Object... args)  
      protected java.io.OutputStream getOutputStream()  
      STTY getTTY()  
      void print​(java.lang.String message)  
      void print​(Char ch)  
      java.io.PrintStream printer()
      Get a print stream that writes to the terminal according to the output mode of the terminal.
      void println()  
      void println​(java.lang.String message)
      Print a new line to the terminal.
      protected void sleep​(long millis)  
      <T> void waitAbortable​(java.util.concurrent.Future<T> task)
      Wait for future task to be done or canceled.
      • Methods inherited from class java.lang.Object

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

      • Terminal

        public Terminal()
        Construct a default RAW terminal.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
      • Terminal

        public Terminal​(STTY tty)
        Construct a default RAW terminal.
        Parameters:
        tty - The terminal device.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
      • Terminal

        public Terminal​(STTY tty,
                        STTYMode mode)
        Construct a terminal with given mode.
        Parameters:
        tty - The terminal device.
        mode - The terminal mode.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
      • Terminal

        public Terminal​(STTY tty,
                        LinePrinter lp)
        Construct a terminal with a custom line printer.
        Parameters:
        tty - The terminal device.
        lp - The line printer.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
      • Terminal

        public Terminal​(STTY tty,
                        STTYMode mode,
                        LinePrinter lp)
        Construct a terminal with a terminal mode and custom line printer.
        Parameters:
        tty - The terminal device.
        mode - The terminal mode.
        lp - The line printer.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
      • Terminal

        public Terminal​(STTY tty,
                        java.io.InputStream in,
                        java.io.OutputStream out,
                        LinePrinter lp,
                        STTYModeSwitcher switcher)
        Constructor visible for testing.
        Parameters:
        tty - The terminal device.
        in - The input stream.
        out - The output stream.
        lp - The line printer or null.
        switcher - TTY mode switcher.
        Throws:
        java.io.UncheckedIOException - If unable to set TTY mode.
    • Method Detail

      • getTTY

        public STTY getTTY()
        Returns:
        Get the terminal device.
      • printer

        public java.io.PrintStream printer()
        Get a print stream that writes to the terminal according to the output mode of the terminal. Handy for e.g. printing stack traces etc while in raw mode.
        Returns:
        A wrapping print stream.
      • confirm

        public boolean confirm​(java.lang.String what)
        Make a user confirmation. E.g.: boolean really = term.confirm("Do you o'Really?"); Will print out "Do you o'Really? [Y/n]: ". If the user press 'y' and 'enter' will pass (return true), if 'n', and 'backspace' will return false. Invalid characters will print a short error message.
        Parameters:
        what - What to confirm. Basically the message before '[Y/n]'.
        Returns:
        Confirmation result.
      • confirm

        public boolean confirm​(java.lang.String what,
                               boolean def)
        Make a user confirmation. E.g.: boolean really = term.confirm("Do you o'Really?", false); Will print out "Do you o'Really? [y/N]: ". If the user press 'y' will pass (return true), if 'n', 'enter' and 'backspace' will return false. Invalid characters will print a short error message.
        Parameters:
        what - What to confirm. Basically the message before '[Y/n]'.
        def - the default response on 'enter'.
        Returns:
        Confirmation result.
      • executeAbortable

        public <T> T executeAbortable​(java.util.concurrent.ExecutorService exec,
                                      java.util.concurrent.Callable<T> callable)
                               throws java.io.IOException,
                                      java.lang.InterruptedException,
                                      java.util.concurrent.ExecutionException
        Execute callable, which may not be interruptable by itself, but listen to terminal input and abort the task if CTRL-C is pressed.
        Type Parameters:
        T - The return type of the callable.
        Parameters:
        exec - The executor to run task on.
        callable - The callable function.
        Returns:
        The result of the callable.
        Throws:
        java.io.IOException - If aborted or read failure.
        java.lang.InterruptedException - If interrupted while waiting.
        java.util.concurrent.ExecutionException - If execution failed.
      • executeAbortable

        public void executeAbortable​(java.util.concurrent.ExecutorService exec,
                                     java.lang.Runnable callable)
                              throws java.io.IOException,
                                     java.lang.InterruptedException,
                                     java.util.concurrent.ExecutionException
        Execute runnable, which may not be interruptable by itself, but listen to terminal input and abort the task if CTRL-C is pressed.
        Parameters:
        exec - The executor to run task on.
        callable - The runnable function.
        Throws:
        java.io.IOException - If aborted or read failure.
        java.lang.InterruptedException - If interrupted while waiting.
        java.util.concurrent.ExecutionException - If execution failed.
      • waitAbortable

        public <T> void waitAbortable​(java.util.concurrent.Future<T> task)
                               throws java.io.IOException,
                                      java.lang.InterruptedException
        Wait for future task to be done or canceled. React to terminal induced abort (ctrl-C) and cancel the task if so.
        Type Parameters:
        T - The task generic type.
        Parameters:
        task - The task to wait for.
        Throws:
        java.io.IOException - On aborted or read failure.
        java.lang.InterruptedException - On thread interrupted.
      • format

        public void format​(java.lang.String format,
                           java.lang.Object... args)
      • print

        public void print​(Char ch)
      • print

        public void print​(java.lang.String message)
      • println

        public void println​(java.lang.String message)
        Description copied from interface: LinePrinter
        Print a new line to the terminal.
        Specified by:
        println in interface LinePrinter
        Parameters:
        message - The message to write.
      • println

        public void println()
      • finish

        public void finish()
        Finish the current set of lines and continue below.
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • getOutputStream

        protected java.io.OutputStream getOutputStream()
      • sleep

        protected void sleep​(long millis)
                      throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException