Readline { public Readline() { this(EditModeBuilder.builder().create()); } Readline();  Readline(EditMode editMode);  Readline(EditMode editMode, History history, CompletionHandler completionHandler); void readline(Connection conn, String prompt, Consumer<String> requestHandler); void readline(Connection conn, String prompt, Consumer<String> requestHandler,
                         List<Completion> completions); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler,
                         List<Completion> completions); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler,
                         List<Completion> completions,
                         List<Function<String,Optional<String>>> preProcessors ); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler,
                         List<Completion> completions,
                         List<Function<String,Optional<String>>> preProcessors, History history); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler,
                         List<Completion> completions,
                         List<Function<String,Optional<String>>> preProcessors,
                         History history, CursorListener listener); void readline(Connection conn, Prompt prompt, Consumer<String> requestHandler,
                         List<Completion> completions,
                         List<Function<String,Optional<String>>> preProcessors,
                         History history, CursorListener listener, EnumMap<ReadlineFlag, Integer> flags);  }
@Test public void testPaste() { TestConnection term = new TestConnection(); term.read("1234\nfoo bar\ngah bah"); term.assertLine("1234"); term.readline(); term.assertLine("foo bar"); term.readline(); term.assertBuffer("gah bah"); }
@Test public void testMasking() { Prompt prompt = new Prompt(": ", '#'); TestConnection term = new TestConnection(null, null, null, prompt); term.setSignalHandler(null); term.read("foo bar"); assertEquals(": #######", term.getOutputBuffer()); term.read(Key.BACKSPACE); term.read(Key.CTRL_A); term.read(Key.CTRL_D); term.read(Key.ENTER); term.assertLine("oo ba"); prompt = new Prompt("", '\0'); term.setPrompt(prompt); term.readline(); term.read("foo bar"); assertEquals("", term.getOutputBuffer()); term.read(Key.BACKSPACE); term.read(Key.BACKSPACE); term.read(Key.ENTER); term.assertLine("foo b"); }
@Test public void testMultipleCompleteResults() { List<Completion> completions = new ArrayList<>(); completions.add(completeOperation -> { if(completeOperation.getBuffer().equals("f")) { completeOperation.addCompletionCandidate("foo"); completeOperation.addCompletionCandidate("foo bar"); } else if(completeOperation.getBuffer().equals("foo")) { completeOperation.addCompletionCandidate("foo"); completeOperation.addCompletionCandidate("foo bar"); } else if(completeOperation.getBuffer().equals("b")) { completeOperation.addCompletionCandidate("bar bar"); completeOperation.addCompletionCandidate("bar baar"); } }); TestConnection term = new TestConnection(completions); term.read("f"); term.read(Key.CTRL_I); term.assertBuffer("foo"); term.clearOutputBuffer(); term.read(Key.CTRL_I); assertEquals(Config.getLineSeparator()+"foo foo bar "+ Config.getLineSeparator()+term.getPrompt()+"foo", term.getOutputBuffer()); term.read(Key.ENTER); term.readline(completions); term.read("b"); term.read(Key.CTRL_I); term.assertBuffer("bar\\ ba"); }
@Test public void testCompleteResultsMultipleLines() { List<Completion> completions = new ArrayList<>(); completions.add(completeOperation -> { if(completeOperation.getBuffer().equals("ff")) { completeOperation.addCompletionCandidate("ffoo"); } else if(completeOperation.getBuffer().endsWith("f")) { completeOperation.addCompletionCandidate(completeOperation.getBuffer()+"oo"); } else if(completeOperation.getBuffer().endsWith("foo")) { completeOperation.addCompletionCandidate(completeOperation.getBuffer()+"foo"); completeOperation.addCompletionCandidate(completeOperation.getBuffer()+"foo bar"); } else if(completeOperation.getBuffer().endsWith("b")) { completeOperation.addCompletionCandidate(completeOperation.getBuffer()+"bar bar"); completeOperation.addCompletionCandidate(completeOperation.getBuffer()+"bar baar"); } }); Size termSize = new Size(10, 10); TestConnection term = new TestConnection(EditModeBuilder.builder().create(), completions, termSize); term.read("ff"); term.read(Key.CTRL_I); term.assertBuffer("ffoo "); term.read(Key.ENTER); term.readline(completions); term.read("11111111111f"); term.read(Key.CTRL_I); term.assertBuffer("11111111111foo "); }
@Test public void testCompletionDoNotMatchBuffer() { List<Completion> completions = new ArrayList<>(); completions.add(completeOperation -> { if(completeOperation.getBuffer().endsWith("f")) { completeOperation.addCompletionCandidate("foo"); completeOperation.setOffset(2); } else if(completeOperation.getBuffer().endsWith("foo")) { completeOperation.addCompletionCandidate("foo bar"); completeOperation.setOffset(completeOperation.getCursor()-3); } else if(completeOperation.getBuffer().endsWith("b")) { completeOperation.addCompletionCandidate("bar bar"); completeOperation.addCompletionCandidate("bar baar"); completeOperation.setOffset(completeOperation.getCursor()-1); } }); TestConnection term = new TestConnection(completions); term.read("oof"); term.read(Key.CTRL_I); term.assertBuffer("oofoo "); term.read(Key.ENTER); term.readline(completions); term.read("bab"); term.read(Key.CTRL_I); term.assertBuffer("babar\\ ba"); term.read(Key.ENTER); term.readline(completions); term.read("foo foo"); term.read(Key.CTRL_I); term.assertBuffer("foo foo bar "); }
@Test public void testCompletionOnMultiline() { List<Completion> completions = new ArrayList<>(); completions.add(completeOperation -> { if(completeOperation.getBuffer().endsWith("f")) { completeOperation.addCompletionCandidate("foo"); completeOperation.setOffset(completeOperation.getCursor()-1); } else if(completeOperation.getBuffer().endsWith("foo")) { completeOperation.addCompletionCandidate("foo bar"); completeOperation.setOffset(completeOperation.getCursor()-3); } else if(completeOperation.getBuffer().endsWith("b")) { completeOperation.addCompletionCandidate("bar bar"); completeOperation.addCompletionCandidate("bar baar"); completeOperation.setOffset(completeOperation.getCursor()-1); } }); TestConnection term = new TestConnection(completions); term.read("fooish \\\noof"); term.read(Key.CTRL_I); term.assertBuffer("fooish oofoo "); term.read(Key.ENTER); term.readline(completions); term.read("bar bar \\\n bab"); term.read(Key.CTRL_I); term.assertBuffer("bar bar babar\\ ba"); term.read(Key.ENTER); term.readline(completions); term.read("foo \\\n foo \\\nfoo"); term.read(Key.CTRL_I); term.assertBuffer("foo foo foo bar "); }