Skip to content

Commit

Permalink
Merge pull request #377 from snuyanzin/JLINE3_376
Browse files Browse the repository at this point in the history
Use History interface to navigate thgrough history, add a test indica…
  • Loading branch information
gnodet authored May 7, 2019
2 parents 985d2cd + 4d941b5 commit db70837
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
10 changes: 5 additions & 5 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
}
int firstId = opt.args().size() > argId ? parseInteger(opt.args().get(argId++)) : -17;
int lastId = opt.args().size() > argId ? parseInteger(opt.args().get(argId++)) : -1;
firstId = historyId(firstId, history.size() - 1);
lastId = historyId(lastId, history.size() - 1);
firstId = historyId(firstId, history.first(), history.last());
lastId = historyId(lastId, history.first(), history.last());
if (firstId > lastId) {
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -275,13 +275,13 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
}
}

private static int historyId(int id, int maxId) {
private static int historyId(int id, int minId, int maxId) {
int out = id;
if (id < 0) {
out = maxId + id + 1;
}
if (out < 0) {
out = 0;
if (out < minId) {
out = minId;
} else if (out > maxId) {
out = maxId;
}
Expand Down
67 changes: 67 additions & 0 deletions builtins/src/test/java/org/jline/builtins/CommandsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.builtins;

import org.jline.reader.History;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.impl.history.DefaultHistory;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.junit.Test;

import java.io.*;
import java.nio.file.Files;

import static org.junit.Assert.assertEquals;

public class CommandsTest {
@Test
public void testHistoryForFileWithMoreHistoryRecordsThanAtHistoryFileSize() {
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final File tmpHistoryFile = Files.createTempFile("tmpHistory", "temp").toFile();
tmpHistoryFile.deleteOnExit();
try (BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(tmpHistoryFile)))) {
bw.write("1536743099591:SELECT \\n CURRENT_TIMESTAMP \\n as \\n c1;\n"
+ "1536743104551:SELECT \\n 'asd' as \"sdf\", 4 \\n \\n as \\n c2;\\n\n"
+ "1536743104551:SELECT \\n 'asd' \\n as \\n c2;\\n\n"
+ "1536743104551:!/ 2\n"
+ "1536743104551:SELECT \\n 2123 \\n as \\n c2 from dual;\\n\n"
+ "1536743107526:!history\n"
+ "1536743115431:SELECT \\n 2 \\n as \\n c2;\n"
+ "1536743115431:SELECT \\n '213' \\n as \\n c1;\n"
+ "1536743115431:!/ 8\n");
bw.flush();
}
Terminal terminal = TerminalBuilder.builder().streams(System.in, System.out).build();
terminal.setSize(new Size(50, 30));
final History historyFromFile = new DefaultHistory();
final LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder()
.terminal(terminal)
.variable(LineReader.HISTORY_FILE, tmpHistoryFile.getAbsolutePath())
.history(historyFromFile);
final LineReader lineReader = lineReaderBuilder.build();
historyFromFile.attach(lineReader);
final int maxLines = 3;
lineReader.setVariable(LineReader.HISTORY_FILE_SIZE, maxLines);
lineReader.getHistory().save();
PrintStream out = new PrintStream(os, false);
Commands.history(lineReader, out, out, new String[] {"-d"});
assertEquals(maxLines + 1,
os.toString("UTF8").split("\\s+\\d{2}:\\d{2}:\\d{2}\\s+").length);
} catch (Exception e) {
throw new RuntimeException("Test failed", e);
}
}
}

0 comments on commit db70837

Please sign in to comment.