wniemiec.io.java.Terminal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of terminal Show documentation
Show all versions of terminal Show documentation
Utility for executing commands in a terminal.
The newest version!
/**
* Copyright (c) William Niemiec.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package wniemiec.io.java;
import java.io.IOException;
import java.util.List;
/**
* Utility for executing commands in a terminal.
*/
public class Terminal {
//-------------------------------------------------------------------------
// Attributes
//-------------------------------------------------------------------------
private final InputTerminal inputTerminal;
private final OutputTerminal outputTerminal;
//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
/**
* Terminal manager.
*
* @param inputTerminal Input manager
* @param outputTerminal Output manager
*
* @throws IllegalArgumentException If any argument is null
*/
public Terminal(InputTerminal inputTerminal, OutputTerminal outputTerminal) {
validateConstructorArgs(inputTerminal, outputTerminal);
this.inputTerminal = inputTerminal;
this.outputTerminal = outputTerminal;
}
//-------------------------------------------------------------------------
// Methods
//-------------------------------------------------------------------------
private void validateConstructorArgs(InputTerminal input, OutputTerminal output) {
if (input == null) {
throw new IllegalArgumentException("Input terminal cannot be null");
}
if (output == null) {
throw new IllegalArgumentException("Output terminal cannot be null");
}
}
/**
* Runs commands in terminal.
*
* @param commands Terminal commands
*
* @throws IOException If terminal cannot be executed
*/
public void exec(String... commands) throws IOException {
if (thereAreNoCommands(commands)) {
return;
}
inputTerminal.exec(commands);
}
private boolean thereAreNoCommands(String... commands) {
return (commands == null)
|| (commands.length == 0);
}
/**
* Erases output.
*/
public void clean() {
outputTerminal.clear();
}
//-------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------
/**
* Retrieves terminal message history.
*
* @return Terminal history
*/
public List getHistory() {
return outputTerminal.getHistory();
}
/**
* Retrieves terminal error message history.
*
* @return Terminal history
*/
public List getErrorHistory() {
return outputTerminal.getErrorHistory();
}
}