- java.lang.Object
-
- net.morimekta.terminal.Terminal
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class Terminal extends Object implements Closeable
Terminal interface. It sets proper TTY mode and reads complex characters from the input, and writes lines dependent on terminal mode.
-
-
Constructor Summary
Constructors Constructor Description Terminal()
Construct a default terminal.Terminal(net.morimekta.io.tty.TTY tty)
Construct a default terminal.Terminal(net.morimekta.io.tty.TTYMode mode)
Construct a terminal with given mode.Terminal(net.morimekta.io.tty.TTY tty, InputStream in, PrintStream out, net.morimekta.io.tty.TTYMode mode, net.morimekta.io.tty.TTYModeSwitcher switcher)
Constructor visible for testing.Terminal(net.morimekta.io.tty.TTY tty, net.morimekta.io.tty.TTYMode mode)
Construct a terminal with a terminal mode and custom line printer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description net.morimekta.strings.chr.CharReader
charReader()
void
close()
boolean
confirm(String what)
Make a user confirmation.boolean
confirm(String what, boolean def)
Make a user confirmation.void
executeAbortable(ExecutorService exec, 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(ExecutorService exec, 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.LinePrinter
lp()
Get a line printer that prints lines to the terminal output according to terminal mode.void
pressToContinue(String message)
Show a "press any key to continue" message.PrintStream
printStream()
Get a print stream that writes to the terminal according to the output mode of the terminal.PrintWriter
printWriter()
Get a print writer that should behave as close to a direct writer.PrintStream
rawOut()
String
readLine(String message)
Read a line from terminal.protected void
sleep(long millis)
Sleep a specified number of milliseconds.net.morimekta.io.tty.TTY
tty()
<T> void
waitAbortable(Future<T> task)
Wait for future task to be done or canceled.Terminal
withMode(net.morimekta.io.tty.TTYMode mode)
Get a terminal with the given TTY mode.
-
-
-
Constructor Detail
-
Terminal
public Terminal()
Construct a default terminal.
-
Terminal
public Terminal(net.morimekta.io.tty.TTY tty)
Construct a default terminal.- Parameters:
tty
- The terminal device.
-
Terminal
public Terminal(net.morimekta.io.tty.TTYMode mode)
Construct a terminal with given mode.- Parameters:
mode
- The terminal mode.- Throws:
UncheckedIOException
- If unable to set TTY mode.
-
Terminal
public Terminal(net.morimekta.io.tty.TTY tty, net.morimekta.io.tty.TTYMode mode)
Construct a terminal with a terminal mode and custom line printer.- Parameters:
tty
- The terminal device.mode
- The terminal mode.- Throws:
UncheckedIOException
- If unable to set TTY mode.
-
Terminal
public Terminal(net.morimekta.io.tty.TTY tty, InputStream in, PrintStream out, net.morimekta.io.tty.TTYMode mode, net.morimekta.io.tty.TTYModeSwitcher switcher)
Constructor visible for testing.- Parameters:
tty
- The terminal device.in
- The input stream.out
- The output stream.mode
- TTY mode.switcher
- TTY mode switcher.
-
-
Method Detail
-
charReader
public net.morimekta.strings.chr.CharReader charReader()
- Returns:
- Character reader to read from input.
-
rawOut
public PrintStream rawOut()
- Returns:
- Print stream to write raw to standard out.
-
printWriter
public PrintWriter printWriter()
Get a print writer that should behave as close to a direct writer. The various println methods are repurposed to behave according to the TTY mode.Use this writer to actually utilize the output directly. Use this when you know and handle the TTY mode correctly.
- Returns:
- The output print writer.
-
printStream
public PrintStream printStream()
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.
-
lp
public LinePrinter lp()
Get a line printer that prints lines to the terminal output according to terminal mode.- Returns:
- The line printer.
-
tty
public net.morimekta.io.tty.TTY tty()
- Returns:
- Get the terminal device.
-
withMode
public Terminal withMode(net.morimekta.io.tty.TTYMode mode)
Get a terminal with the given TTY mode. This will also reset the console position, so it starts on a new line, if anything was already written. The created terminal should be closed before this is used again.- Parameters:
mode
- Required mode.- Returns:
- The terminal.
-
confirm
public boolean confirm(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' will pass (return true), if 'n', and 'backspace' will return false. Enter is considered invalid input. 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(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', and 'backspace' will return false. Enter will return the default value. 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.
-
pressToContinue
public void pressToContinue(String message)
Show a "press any key to continue" message. If the user interrupts, an exception is thrown, any other key just returns.- Parameters:
message
- Message shown when waiting.
-
readLine
public String readLine(String message)
Read a line from terminal.- Parameters:
message
- The message to be shown before line input.- Returns:
- The read line.
- Throws:
UncheckedIOException
- if interrupted or reached end of input.
-
executeAbortable
public <T> T executeAbortable(ExecutorService exec, Callable<T> callable) throws IOException, InterruptedException, 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:
IOException
- If aborted or read failure.InterruptedException
- If interrupted while waiting.ExecutionException
- If execution failed.
-
executeAbortable
public void executeAbortable(ExecutorService exec, Runnable callable) throws IOException, InterruptedException, 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:
IOException
- If aborted or read failure.InterruptedException
- If interrupted while waiting.ExecutionException
- If execution failed.
-
waitAbortable
public <T> void waitAbortable(Future<T> task) throws IOException, InterruptedException
Wait for future task to be done or canceled. React to terminal induced abort (ctrl-C) and cancel the task if so. Note that this method will basically swallow all user input and- Type Parameters:
T
- The task generic type.- Parameters:
task
- The task to wait for.- Throws:
IOException
- On aborted or read failure.InterruptedException
- On thread interrupted.
-
finish
public void finish()
Finish the current set of lines and continue below.
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
sleep
protected void sleep(long millis) throws InterruptedException
Sleep a specified number of milliseconds. This method is exposed to help with testing.- Parameters:
millis
- Millis to sleep.- Throws:
InterruptedException
- If sleep was interrupted.
-
-