Skip to content

Commit

Permalink
Issue aeshell#105
Browse files Browse the repository at this point in the history
  • Loading branch information
felipewmartins committed Jun 6, 2015
1 parent c568546 commit 21378a9
Showing 1 changed file with 63 additions and 70 deletions.
133 changes: 63 additions & 70 deletions src/main/groovy/org/jboss/aesh/mterm/util/AeshUtil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,86 @@
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.jboss.aesh.mterm.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Set;

import org.jboss.aesh.console.AeshConsole;
import org.jboss.aesh.console.AeshConsoleBuilder;
import org.jboss.aesh.console.Config;
import org.jboss.aesh.console.command.Command;
import org.jboss.aesh.console.command.registry.AeshCommandRegistryBuilder;
import org.jboss.aesh.console.command.registry.CommandRegistry;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
import org.jboss.aesh.extensions.cat.Cat;
import org.jboss.aesh.extensions.cd.Cd;
import org.jboss.aesh.extensions.clear.Clear;
import org.jboss.aesh.extensions.echo.Echo;
import org.jboss.aesh.extensions.exit.Exit;
import org.jboss.aesh.extensions.ls.Ls;
import org.jboss.aesh.extensions.mkdir.Mkdir;
import org.jboss.aesh.extensions.pwd.Pwd;
import org.jboss.aesh.extensions.rm.Rm;
import org.jboss.aesh.extensions.mv.Mv;
import org.jboss.aesh.extensions.touch.Touch;
import org.jboss.aesh.parser.Parser;
package org.jboss.aesh.mterm.util

import org.jboss.aesh.console.AeshConsole
import org.jboss.aesh.console.AeshConsoleBuilder
import org.jboss.aesh.console.Config
import org.jboss.aesh.console.command.Command
import org.jboss.aesh.console.command.registry.AeshCommandRegistryBuilder
import org.jboss.aesh.console.command.registry.CommandRegistry
import org.jboss.aesh.console.settings.Settings
import org.jboss.aesh.console.settings.SettingsBuilder
import org.jboss.aesh.extensions.cat.Cat
import org.jboss.aesh.extensions.cd.Cd
import org.jboss.aesh.extensions.clear.Clear
import org.jboss.aesh.extensions.echo.Echo
import org.jboss.aesh.extensions.exit.Exit
import org.jboss.aesh.extensions.ls.Ls
import org.jboss.aesh.extensions.mkdir.Mkdir
import org.jboss.aesh.extensions.pwd.Pwd
import org.jboss.aesh.extensions.rm.Rm
import org.jboss.aesh.extensions.mv.Mv
import org.jboss.aesh.extensions.touch.Touch
import org.jboss.aesh.parser.Parser

/**
* @author Helio Frota 00hf11 at gmail.com
*/
public enum AeshUtil {

INSTANCE;
INSTANCE

private PipedOutputStream pos;
private PipedInputStream pis;
private ByteArrayOutputStream stream;
private Settings settings;
private AeshConsole aeshConsole;
private CommandRegistry registry;
private String executedCommand;
private PipedOutputStream pos
private PipedInputStream pis
private ByteArrayOutputStream stream
private Settings settings
private AeshConsole aeshConsole
private CommandRegistry registry
private String executedCommand

@SuppressWarnings("unchecked")
public void start(PrintStream ps) {
pos = new PipedOutputStream();
void start(PrintStream ps) {
pos = new PipedOutputStream()
try {
pis = new PipedInputStream(pos);
pis = new PipedInputStream(pos)
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace()
}
stream = new ByteArrayOutputStream();
stream = new ByteArrayOutputStream()

settings =
new SettingsBuilder().inputStream(pis)
.outputStreamError(new PrintStream(stream))
.outputStream(new PrintStream(stream)).create();
.outputStream(new PrintStream(stream)).create()

try {
add(Cd.class, Ls.class, Mkdir.class, Pwd.class, Rm.class, Mv.class, Touch.class, Cat.class,
Clear.class, Echo.class, Exit.class);
Clear.class, Echo.class, Exit.class)
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace()
}
}

/**
* Resets the stream.
*/
public void reset() {
getStream().reset();
void reset() {
getStream().reset()
}

/**
* Gets the result of the command.
*
* @return String
*/
public String getResult() {
String result = Parser.stripAwayAnsiCodes(getStream().toString());
executedCommand = result.split("\n")[0];
result = result.replaceAll(executedCommand, "");
return result;
String getResult() {
String result = Parser.stripAwayAnsiCodes(getStream().toString())
executedCommand = result.split('\n')[0]
result = result.replaceAll(executedCommand, '')
return result
}

/**
Expand All @@ -105,40 +98,40 @@ public enum AeshUtil {
* @param command String
* @throws IOException exception
*/
public void run(String command) throws IOException {
getPos().write(command.getBytes());
getPos().write(Config.getLineSeparator().getBytes());
getPos().flush();
pause();
void run(String command) throws IOException {
getPos().write(command.getBytes())
getPos().write(Config.getLineSeparator().getBytes())
getPos().flush()
pause()
}

private void pause() {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
e.printStackTrace()
}
}

private void add(Class<? extends Command>... commands) throws IOException {

registry = new AeshCommandRegistryBuilder().commands(commands).create();
registry = new AeshCommandRegistryBuilder().commands(commands).create()

AeshConsoleBuilder consoleBuilder =
new AeshConsoleBuilder().settings(settings).commandRegistry(registry);
new AeshConsoleBuilder().settings(settings).commandRegistry(registry)

aeshConsole = consoleBuilder.create();
aeshConsole.start();
getStream().flush();
aeshConsole = consoleBuilder.create()
aeshConsole.start()
getStream().flush()
}

private PipedOutputStream getPos() {
return pos;
return pos
}

private ByteArrayOutputStream getStream() {
return stream;
return stream
}

/**
Expand All @@ -147,7 +140,7 @@ public enum AeshUtil {
* @return Set<String>
*/
public Set<String> getRegisteredCommands() {
return registry.getAllCommandNames();
return registry.getAllCommandNames()
}

/**
Expand All @@ -156,29 +149,29 @@ public enum AeshUtil {
* @return String
*/
public String getCurrentDirectory() {
return aeshConsole.getAeshContext().getCurrentWorkingDirectory().getName();
return aeshConsole.getAeshContext().getCurrentWorkingDirectory().getName()
}

/**
* Checks if aesh is running.
* @return
*/
public boolean isRunning() {
return aeshConsole.isRunning();
return aeshConsole.isRunning()
}

/**
* Stops the aesh console.
*/
public void stop() {
aeshConsole.stop();
aeshConsole.stop()
}

/**
* Gets the last executed command.
*/
public String getExecutedCommand() {
return executedCommand;
return executedCommand
}

}

0 comments on commit 21378a9

Please sign in to comment.