@Test public void masking() throws IOException, InterruptedException { PipedOutputStream outputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(outputStream); KeyOperation deletePrevChar = new KeyOperation(Key.CTRL_H, Operation.DELETE_PREV_CHAR); Console console = getTestConsole(pipedInputStream); console.setPrompt(new Prompt("", new Character('\u0000'))); console.setConsoleCallback( new ConsoleCallback() { @Override public int readConsoleOutput(ConsoleOperation output) { assertEquals("mypasswor", output.getBuffer()); return 0; } }); console.start(); outputStream.write(("mypassword").getBytes()); outputStream.write(deletePrevChar.getFirstValue()); outputStream.write(("\n").getBytes()); Thread.sleep(100); console.stop(); }
private void init() { try { if (!operation.getControlOperator().isRedirectionOut()) { console.getShell().out().print(ANSI.getAlternateBufferScreen()); console.getShell().out().println("print alternate screen..."); console.getShell().out().flush(); } if (console.getShell().in().getStdIn().available() > 0) { java.util.Scanner s = new java.util.Scanner(console.getShell().in().getStdIn()).useDelimiter("\\A"); String fileContent = s.hasNext() ? s.next() : ""; console.getShell().out().println("FILECONTENT: "); console.getShell().out().print(fileContent); console.getShell().out().flush(); } else console.getShell().out().println("console.in() == null"); readFromFile(); // detach after init if hasRedirectOut() if (operation.getControlOperator().isRedirectionOut()) { attached = false; } } catch (IOException ioe) { } }
@Override public void processOperation(CommandOperation operation) throws IOException { if (operation.getInput()[0] == 'q') { console.getShell().out().print(ANSI.getMainBufferScreen()); attached = false; } else if (operation.getInput()[0] == 'a') { readFromFile(); } else { } }
@Test public void paste() throws IOException, InterruptedException { final PipedOutputStream outputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(outputStream); String pasteLine1 = "connect" + Config.getLineSeparator() + "admin" + Config.getLineSeparator() + "admin!"; String pasteLine2 = "234" + Config.getLineSeparator() + "exit" + Config.getLineSeparator(); final Console console = getTestConsole(pipedInputStream); console.setConsoleCallback( new AeshConsoleCallback() { boolean password = false; @Override public int execute(ConsoleOperation output) { if (output.getBuffer().equals("admin")) { console.setPrompt(new Prompt("", new Character('\u0000'))); password = true; return 0; } if (password) { assertEquals("admin!234", output.getBuffer()); password = false; } return 0; } }); console.start(); outputStream.write(pasteLine1.getBytes()); outputStream.write(pasteLine2.getBytes()); Thread.sleep(500); console.stop(); }
public static void main(String[] args) throws IOException { // Settings.getInstance().setAnsiConsole(false); SettingsBuilder builder = new SettingsBuilder(); builder.readInputrc(false).logging(true); builder.logfile( System.getProperty("user.dir") + System.getProperty("file.separator") + "aesh_example.log"); // Settings.getInstance().setAliasEnabled(true); // Settings.getInstance().setAliasFile(new // File(System.getProperty("user.dir")+Config.getPathSeparator()+"aesh_aliases.txt")); // Settings.getInstance().setPersistAlias(true); // Settings.getInstance().enableOperatorParser(false); // Settings.getInstance().setHistoryDisabled(true); // Settings.getInstance().setHistoryPersistent(false); List<TerminalCharacter> chars = new ArrayList<TerminalCharacter>(); chars.add(new TerminalCharacter('[', new TerminalColor(Color.BLUE, Color.DEFAULT))); chars.add( new TerminalCharacter( 't', new TerminalColor(Color.RED, Color.DEFAULT), CharacterType.ITALIC)); chars.add( new TerminalCharacter( 'e', new TerminalColor(Color.RED, Color.DEFAULT), CharacterType.INVERT)); chars.add( new TerminalCharacter( 's', new TerminalColor(Color.RED, Color.DEFAULT), CharacterType.CROSSED_OUT)); chars.add( new TerminalCharacter( 't', new TerminalColor(Color.RED, Color.DEFAULT), CharacterType.BOLD)); chars.add( new TerminalCharacter( ']', new TerminalColor(Color.BLUE, Color.DEFAULT), CharacterType.FAINT)); chars.add( new TerminalCharacter( '$', new TerminalColor(Color.GREEN, Color.DEFAULT), CharacterType.UNDERLINE)); chars.add(new TerminalCharacter(' ', new TerminalColor(Color.DEFAULT, Color.DEFAULT))); final Prompt prompt = new Prompt(chars); // String prompt = ANSI.redText()+"[test@foo]"+ANSI.reset()+"$ "; // a simple interruptHook builder.interruptHook( new InterruptHook() { @Override public void handleInterrupt(Console console) { console.getShell().out().println("^C"); console.clearBufferAndDisplayPrompt(); } }); final Console exampleConsole = new Console(builder.create()); Completion completer = new Completion() { @Override public void complete(CompleteOperation co) { // very simple completor List<String> commands = new ArrayList<String>(); if (co.getBuffer().equals("fo") || co.getBuffer().equals("foo")) { commands.add("foo"); commands.add("foobaa"); commands.add("foobar"); commands.add("foobaxxxxxx"); commands.add("foobbx"); commands.add("foobcx"); commands.add("foobdx"); } if (co.getBuffer().equals("p")) { commands.add("profile=foo"); co.setOffset(0); } /* if(co.getBuffer().equals("p")) { commands.add("profile=bar"); co.setOffset(0); } */ if (co.getBuffer().equals("profile=")) commands.add("profile=foo"); if (co.getBuffer().equals("profile=")) commands.add("profile=bar"); if (co.getBuffer().equals("--")) { commands.add("--help-"); } if ("--help-me".startsWith(co.getBuffer())) { commands.add("--help-me"); } if (co.getBuffer().equals("fooba")) { commands.add("foobaa"); commands.add("foobar"); commands.add("foobaxxxxxx"); } if (co.getBuffer().equals("foobar")) { commands.add("foobar"); } if (co.getBuffer().equals("bar")) { commands.add("bar/"); } if (co.getBuffer().equals("h")) { commands.add("help.history"); commands.add("help"); co.setOffset(0); } if (co.getBuffer().equals("help")) { commands.add("help.history"); commands.add("help"); } if (co.getBuffer().equals("help.")) { commands.add("help.history"); } if (co.getBuffer().equals("deploy")) { commands.add("deploy /home/blabla/foo/bar/alkdfe/en/to/tre"); } if (co.getBuffer().equals("testing")) { commands.add("testing YAY"); } if (co.getBuffer().equals("val") || co.getBuffer().equals("value ")) { commands.add("value 1"); commands.add("value 2"); commands.add("value 10"); commands.add("value 20"); } if (co.getBuffer().equals("valu")) commands.add("value 10"); co.setCompletionCandidates(commands); } }; exampleConsole.addCompletion(completer); final ConsoleCallback consoleCallback = new ConsoleCallback() { @Override public int readConsoleOutput(ConsoleOperation output) { try { // To change body of implemented methods use File | Settings | File Templates. exampleConsole.getShell().out().print("======>\"" + output.getBuffer() + "\"\n"); if (masking) { exampleConsole .getShell() .out() .print("got password: "******", stopping masking"); masking = false; exampleConsole.setPrompt(prompt); } else if (output.getBuffer().equalsIgnoreCase("quit") || output.getBuffer().equalsIgnoreCase("exit") || output.getBuffer().equalsIgnoreCase("reset")) { exampleConsole.stop(); } else if (output.getBuffer().equalsIgnoreCase("password")) { masking = true; exampleConsole.setPrompt(new Prompt("password: "******"blah")) { exampleConsole.getShell().err().print("blah. command not found.\n"); exampleConsole.getShell().out().print("BAH" + Config.getLineSeparator()); } else if (output.getBuffer().equals("clear")) exampleConsole.clear(); else if (output.getBuffer().startsWith("man")) { // exampleConsole.attachProcess(test); ConsoleCommand test = new ExampleConsoleCommand(exampleConsole, output); exampleConsole.attachProcess(test); } else if (output.getBuffer().startsWith("login")) { exampleConsole.setConsoleCallback(passwordCallback); exampleConsole.setPrompt(new Prompt("Username: "******"Exception: " + ioe.getMessage()); return -1; } } }; exampleConsole.setConsoleCallback(consoleCallback); exampleConsole.start(); exampleConsole.setPrompt(prompt); passwordCallback = new ConsoleCallback() { private boolean hasUsername = false; @Override public int readConsoleOutput(ConsoleOperation output) { if (hasUsername) { password = output.getBuffer(); hasPassword = true; exampleConsole .getShell() .out() .print( "Username: "******", password: "******"Password: "******"[FOO]» ", new TerminalColor(Color.RED, Color.DEFAULT), new TerminalTextStyle(CharacterType.BOLD)))); } catch (InterruptedException e) { e.printStackTrace(); } }
private void readFromFile() throws IOException { if (console.getShell().in().getStdIn().available() > 0) { console.getShell().out().println("FROM STDOUT: "); } else console.getShell().out().println("here should we present some text... press 'q' to quit"); }