/** * Read a line until a request can be processed. * * @param requestHandler the requestHandler */ public void readline(TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) { synchronized (this) { if (interaction != null) { throw new IllegalStateException("Already reading a line"); } interaction = new Interaction(conn, prompt, requestHandler, completionHandler); } interaction.install(); conn.write(prompt); schedulePendingEvent(); }
@Override public void apply(Readline.Interaction interaction) { LineBuffer buf = interaction.buffer().copy(); int size = buf.getSize(); int next; while ((next = buf.getCursor()) < size) { int codePoint = buf.getAt(next); if (Character.isLetterOrDigit(codePoint)) { break; } else { buf.moveCursor(1); } } while ((next = buf.getCursor()) < size) { int codePoint = buf.getAt(next); if (Character.isLetterOrDigit(codePoint)) { buf.moveCursor(1); } else { break; } } interaction.refresh(buf); interaction.resume(); } }
@Override public void apply(Readline.Interaction interaction) { Consumer<Completion> handler = interaction.completionHandler(); if (handler != null) { Completion completion = new Completion(interaction); handler.accept(completion); } else { interaction.resume(); } } }
refresh(new LineBuffer(), newWidth);
refresh(new LineBuffer(), newWidth);
@Override public void apply(Readline.Interaction interaction) { LineBuffer buf = interaction.buffer().copy(); int size = buf.getSize(); int next; while ((next = buf.getCursor()) < size) { int codePoint = buf.getAt(next); if (Character.isLetterOrDigit(codePoint)) { break; } else { buf.moveCursor(1); } } while ((next = buf.getCursor()) < size) { int codePoint = buf.getAt(next); if (Character.isLetterOrDigit(codePoint)) { buf.moveCursor(1); } else { break; } } interaction.refresh(buf); interaction.resume(); } }
private void refresh(LineBuffer update, int width) { LineBuffer copy3 = new LineBuffer(); IntStream.Builder consumer = IntStream.builder(); copy3.insert(Helper.toCodePoints(currentPrompt)); copy3.insert(buffer().toArray()); copy3.setCursor(currentPrompt.length() + buffer().getCursor()); LineBuffer copy2 = new LineBuffer(); copy2.insert(Helper.toCodePoints(currentPrompt)); copy2.insert(update.toArray()); copy2.setCursor(currentPrompt.length() + update.getCursor()); copy3.update(copy2, data -> { for (int cp : data) { consumer.accept(cp); } }, width); conn.stdoutHandler().accept(consumer.build().toArray()); buffer.clear(); buffer.insert(update.toArray()); buffer.setCursor(update.getCursor()); }
private void refresh(LineBuffer update, int width) { LineBuffer copy3 = new LineBuffer(); IntStream.Builder consumer = IntStream.builder(); copy3.insert(Helper.toCodePoints(currentPrompt)); copy3.insert(buffer().toArray()); copy3.setCursor(currentPrompt.length() + buffer().getCursor()); LineBuffer copy2 = new LineBuffer(); copy2.insert(Helper.toCodePoints(currentPrompt)); copy2.insert(update.toArray()); copy2.setCursor(currentPrompt.length() + update.getCursor()); copy3.update(copy2, data -> { for (int cp : data) { consumer.accept(cp); } }, width); conn.stdoutHandler().accept(consumer.build().toArray()); buffer.clear(); buffer.insert(update.toArray()); buffer.setCursor(update.getCursor()); }
@Override public void apply(Readline.Interaction interaction) { List<int[]> history = interaction.history(); int curr = interaction.getHistoryIndex(); if (curr >= 0) { int next = curr - 1; int[] line; if (next == -1) { line = (int[]) interaction.data().get("abc"); } else { line = history.get(next); } interaction.refresh(new LineBuffer().insert(line)); interaction.setHistoryIndex(next); } interaction.resume(); } }
@Override public void apply(Readline.Interaction interaction) { List<int[]> history = interaction.history(); int curr = interaction.getHistoryIndex(); if (curr >= 0) { int next = curr - 1; int[] line; if (next == -1) { line = (int[]) interaction.data().get("abc"); } else { line = history.get(next); } interaction.refresh(new LineBuffer().insert(line)); interaction.setHistoryIndex(next); } interaction.resume(); } }
@Override public void apply(Readline.Interaction interaction) { List<int[]> history = interaction.history(); if (history.size() > 0) { int curr = interaction.getHistoryIndex(); int next = curr + 1; if (next < history.size()) { if (curr == -1) { int[] tmp = interaction.buffer().toArray(); interaction.data().put("abc", tmp); } int[] line = history.get(next); interaction.refresh(new LineBuffer().insert(line)); interaction.setHistoryIndex(next); } } interaction.resume(); } }
@Override public void apply(Readline.Interaction interaction) { List<int[]> history = interaction.history(); if (history.size() > 0) { int curr = interaction.getHistoryIndex(); int next = curr + 1; if (next < history.size()) { if (curr == -1) { int[] tmp = interaction.buffer().toArray(); interaction.data().put("abc", tmp); } int[] line = history.get(next); interaction.refresh(new LineBuffer().insert(line)); interaction.setHistoryIndex(next); } } interaction.resume(); } }
public Completion(Readline.Interaction interaction) { // int index = interaction.buffer().getCursor(); while (index > 0 && interaction.buffer().getAt(index - 1) != ' ') { index--; } // Compute line : need to test full line :-) // Compute prefix LineBuffer prefix = interaction.line().copy(); for (int i = index; i < interaction.buffer().getCursor();i++) { prefix.insert(interaction.buffer().getAt(i)); } this.interaction = interaction; this.prefix = prefix.toArray(); this.line = interaction.line().copy().insert(interaction.buffer().toArray()).toArray(); }
/** * Write a block of text below the current edition line, a {@code CRLF} sequence is inserted before the * block of text.<p/> * * @param text the text to insert inline */ public void suggest(int[] text) { if (!done.compareAndSet(false, true)) { throw new IllegalStateException(); } interaction.conn.write("\n"); interaction.conn.stdoutHandler().accept(text); interaction.redraw(); interaction.resume(); }
/** * Write a block of text below the current edition line, a {@code CRLF} sequence is inserted before the * block of text.<p/> * * @param text the text to insert inline */ public void suggest(int[] text) { if (!done.compareAndSet(false, true)) { throw new IllegalStateException(); } interaction.conn.write("\n"); interaction.conn.stdoutHandler().accept(text); interaction.redraw(); interaction.resume(); }
public Completion(Readline.Interaction interaction) { // int index = interaction.buffer().getCursor(); while (index > 0 && interaction.buffer().getAt(index - 1) != ' ') { index--; } // Compute line : need to test full line :-) // Compute prefix LineBuffer prefix = interaction.line().copy(); for (int i = index; i < interaction.buffer().getCursor();i++) { prefix.insert(interaction.buffer().getAt(i)); } this.interaction = interaction; this.prefix = prefix.toArray(); this.line = interaction.line().copy().insert(interaction.buffer().toArray()).toArray(); }