Skip to content

Commit

Permalink
less command: implemented search operations with spanning files
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 20, 2019
1 parent b79a425 commit ff1f07a
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public class Less {
private List<Path> syntaxFiles = new ArrayList<>();
private boolean highlight = true;


public Less(Terminal terminal, Path currentDir) {
this(terminal, currentDir, null);
}
Expand Down Expand Up @@ -477,21 +476,17 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
case RIGHT_ONE_HALF_SCREEN:
firstColumnToDisplay += size.getColumns() / 2;
break;
case REPEAT_SEARCH_BACKWARD:
case REPEAT_SEARCH_BACKWARD_SPAN_FILES:
if (forward) {
moveToPreviousMatch();
} else {
moveToNextMatch();
}
moveToMatch(!forward, true);
break;
case REPEAT_SEARCH_BACKWARD:
moveToMatch(!forward, false);
break;
case REPEAT_SEARCH_FORWARD:
case REPEAT_SEARCH_FORWARD_SPAN_FILES:
if (forward) {
moveToNextMatch();
} else {
moveToPreviousMatch();
}
moveToMatch(forward, true);
break;
case REPEAT_SEARCH_FORWARD:
moveToMatch(forward, false);
break;
case UNDO_SEARCH:
pattern = null;
Expand Down Expand Up @@ -645,6 +640,14 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
}
}

private void moveToMatch(boolean forward, boolean spanFiles) throws IOException {
if (forward) {
moveToNextMatch(spanFiles);
} else {
moveToPreviousMatch(spanFiles);
}
}

private class LineEditor {
private int begPos;

Expand Down Expand Up @@ -976,7 +979,7 @@ protected void openSource() throws IOException {
wasOpen = true;
}
boolean open = false;
boolean displayMessage = false;
boolean displayMessage = false;
do {
Source source = sources.get(sourceIdx);
try {
Expand Down Expand Up @@ -1046,6 +1049,10 @@ void moveTo(int lineNum) throws IOException {
}

private void moveToNextMatch() throws IOException {
moveToNextMatch(false);
}

private void moveToNextMatch(boolean spanFiles) throws IOException {
Pattern compiled = getPattern();
Pattern dpCompiled = getPattern(true);
if (compiled != null) {
Expand All @@ -1063,10 +1070,29 @@ private void moveToNextMatch() throws IOException {
}
}
}
message = "Pattern not found";
if (spanFiles) {
if (sourceIdx < sources.size() - 1) {
SavedSourcePositions ssp = new SavedSourcePositions();
String newSource = sources.get(++sourceIdx).getName();
try {
openSource();
moveToNextMatch(true);
} catch (FileNotFoundException exp) {
ssp.restore(newSource);
}
} else {
message = "Pattern not found";
}
} else {
message = "Pattern not found";
}
}

private void moveToPreviousMatch() throws IOException {
moveToPreviousMatch(false);
}

private void moveToPreviousMatch(boolean spanFiles) throws IOException {
Pattern compiled = getPattern();
Pattern dpCompiled = getPattern(true);
if (compiled != null) {
Expand All @@ -1084,7 +1110,23 @@ private void moveToPreviousMatch() throws IOException {
}
}
}
message = "Pattern not found";
if (spanFiles) {
if (sourceIdx > 1) {
SavedSourcePositions ssp = new SavedSourcePositions(-1);
String newSource = sources.get(--sourceIdx).getName();
try {
openSource();
firstLineToDisplay = (int)(long)sources.get(sourceIdx).lines();
moveToPreviousMatch(true);
} catch (FileNotFoundException exp) {
ssp.restore(newSource);
}
} else {
message = "Pattern not found";
}
} else {
message = "Pattern not found";
}
}

private String printable(String s) {
Expand Down

1 comment on commit ff1f07a

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.