public JLineProcessor( boolean ansi, Shell shell, final ConsoleReader reader, PrintStream out, String lineSeparator) { // this.console = new Console(shell, this); this.writer = out; this.useAlternate = false; this.terminal = reader.getTerminal(); this.reader = reader; this.lineSeparator = lineSeparator; this.done = new CountDownLatch(1); this.ansi = ansi; // Update the mode according to the notification console.addModeListener(new Runnable() { @Override public void run() { reader.setKeyMap(console.getMode().getKeyMap()); } }); }
public void testInsert() { console.init(); console.toInsert(); console.on(Operation.MENU_COMPLETE); console.on(Operation.MENU_COMPLETE_BACKWARD); console.on(Operation.REVERSE_SEARCH_HISTORY); console.on(Operation.FORWARD_SEARCH_HISTORY); console.on(Operation.QUOTED_INSERT); console.on(Operation.YANK); console.on(Operation.UNDO); }
public void testForwardChar() throws Exception { console.init(); console.on(Operation.VI_EDITING_MODE); console.on(KeyStrokes.of("abc")); console.on(Operation.BACKWARD_CHAR); console.on(Operation.BACKWARD_CHAR); console.on(Operation.FORWARD_CHAR); console.on(Operation.FORWARD_CHAR); assertEquals(2, getCurrentCursor()); }
/** * Returns the right cursor bound depending on the current mode. * * @return the current bound */ int getCursorBound() { if (console.getMode() == Mode.EMACS) { return buffer.getSize(); } else { return Math.max(0, buffer.getSize() - 1); } }
@Override void perform(Editor editor, EditorBuffer buffer) throws IOException { editor.console.setMode(this); }
@Override String execute(Editor editor, EditorBuffer buffer, int[] sequence, boolean flush) throws IOException { editor.historyCursor = -1; editor.historyBuffer = null; String line = buffer.getLine(); editor.lineParser.append(line); if (editor.console.getMode() == Mode.VI_MOVE) { editor.console.setMode(Mode.VI_INSERT); } if (editor.lineParser.crlf()) { editor.console.driver.writeCRLF(); editor.console.driver.flush(); String request = editor.visitor.getRaw(); if (request.length() > 0) { editor.addToHistory(request); } return request; } else { buffer.append('\n'); editor.console.driver.write("> "); if (flush) { buffer.flush(); } return null; } } };
private void doTest(Mode status, Mode expectedStatus) { console.on(KeyStrokes.of("foo")); console.setMode(status); assertEquals("foo", getCurrentLine()); assertEquals(3, getCurrentCursor()); console.on(Operation.INTERRUPT); assertEquals(expectedStatus, console.getMode()); assertEquals("", getCurrentLine()); assertEquals(0, getCurrentCursor()); }
private void doTest(Mode expected) { final AtomicReference<String> calls = new AtomicReference<String>(); shell.addProcess(new SyncProcess() { @Override public void run(String request, ShellProcessContext context) throws Exception { calls.set(request); context.end(ShellResponse.ok()); } }); console.on(KeyStrokes.of("abc def")); console.on(Operation.ACCEPT_LINE); assertEquals("abc def", calls.get()); assertEquals(expected, console.getMode()); } }
console.on(KeyStrokes.of("abc")); console.on(Operation.ACCEPT_LINE); assertEquals("", getCurrentLine()); assertEquals(0, getCurrentCursor()); console.on(KeyStrokes.of("def")); console.on(Operation.ACCEPT_LINE); assertEquals("", getCurrentLine()); assertEquals(0, getCurrentCursor()); console.on(KeyStrokes.of("ghi")); console.setMode(mode); console.on(Operation.PREVIOUS_HISTORY); assertEquals("def", getCurrentLine()); assertEquals(3, getCurrentCursor()); console.on(Operation.NEXT_HISTORY); assertEquals("ghi", getCurrentLine()); assertEquals(3, getCurrentCursor()); console.on(Operation.BEGINNING_OF_HISTORY); assertEquals("abc", getCurrentLine()); assertEquals(3, getCurrentCursor()); console.on(Operation.END_OF_HISTORY); assertEquals("def", getCurrentLine()); assertEquals(3, getCurrentCursor());
public void on(KeyStroke[] keyStrokes) { for (KeyStroke keyStroke : keyStrokes) { on(keyStroke); } }
@Override public String readLine(String msg, boolean echo) throws IOException, InterruptedException { Reader waiter = new Reader(Thread.currentThread(), echo); if (editor.compareAndSet(null, waiter)) { if (msg != null && msg.length() > 0) { console.driver.write(msg); console.driver.flush(); } console.iterate(); try { return waiter.line.take(); } finally { editor.set(null); } } else { throw new IllegalStateException("A thread is already reading the line"); } }
/** * Initiali */ public void init() { // Take care of pormpt String welcome = shell.getWelcome(); if (welcome != null && welcome.length() > 0) { try { driver.write(welcome); driver.flush(); } catch (IOException e) { // Log it } } edit(); }
@Override protected void setUp() throws Exception { prompt = ""; console = new Console( shell = new SyncShell() { @Override public String getPrompt() { return prompt; } }, driver = new TestDriver()); }