Skip to content

Commit

Permalink
nano & less commands: added historylog option
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 23, 2019
1 parent f22d7e7 commit c9e3f7d
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 64 deletions.
6 changes: 4 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
" -z --suspend Enable the ability to suspend nano using the system's suspend keystroke (usually ^Z).",
" -v --view Don't allow the contents of the file to be altered: read-only mode.",
" -k --cutfromcursor Make the 'Cut Text' command cut from the current cursor position to the end of the line",
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X)."
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X).",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"
};
Options opt = Options.compile(usage).parse(argv);
if (opt.isSet("help")) {
Expand Down Expand Up @@ -171,7 +172,8 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
" -Y --syntax=name The name of the syntax highlighting to use.",
" --no-init Disable terminal initialization",
" --no-keypad Disable keypad handling",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc."
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc.",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"

};

Expand Down
58 changes: 27 additions & 31 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.regex.PatternSyntaxException;

import org.jline.builtins.Nano.Parser;
import org.jline.builtins.Nano.PatternHistory;
import org.jline.builtins.Nano.SyntaxHighlighter;
import org.jline.builtins.Source.ResourceSource;
import org.jline.builtins.Source.URLSource;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class Less {
public boolean noInit;
protected List<Integer> tabs = Arrays.asList(4);
protected String syntaxName;
private String historyLog = null;

protected final Terminal terminal;
protected final Display display;
Expand Down Expand Up @@ -103,8 +105,7 @@ public class Less {

protected int nbEof;

protected List<String> patterns = new ArrayList<String>();
protected int patternId = -1;
protected PatternHistory patternHistory = new PatternHistory(null);
protected String pattern;
protected String displayPattern;

Expand Down Expand Up @@ -184,6 +185,16 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath
if (opts.isSet("no-keypad")) {
noKeypad = true;
}
if (opts.isSet("historylog")) {
historyLog = opts.get("historylog");
}
}
if (configPath != null && historyLog != null) {
try {
patternHistory = new PatternHistory(configPath.getUserConfig(historyLog, true));
} catch (IOException e) {
errorMessage = "Encountered error while reading pattern-history file: " + historyLog;
}
}
}

Expand Down Expand Up @@ -232,6 +243,8 @@ private void parseConfig(Path file) throws IOException {
String val = parts.get(2);
if (option.equals("tabs")) {
doTabs(val);
} else if (option.equals("historylog")) {
historyLog = val;
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
}
Expand Down Expand Up @@ -637,6 +650,7 @@ else if (buffer.length() > 0 && (buffer.charAt(0) == '/' || buffer.charAt(0) ==
if (status != null) {
status.restore();
}
patternHistory.persist();
}
}

Expand Down Expand Up @@ -852,39 +866,23 @@ private boolean search() throws IOException, InterruptedException {
int curPos = buffer.length();
final int begPos = curPos;
final char type = buffer.charAt(0);
String currentBuffer = "";
String currentBuffer = buffer.toString();
LineEditor lineEditor = new LineEditor(begPos);
while (true) {
checkInterrupted();
Operation op = null;
switch (op=bindingReader.readBinding(searchKeyMap)) {
case UP:
patternId++;
if (patternId >= 0 && patternId < patterns.size()) {
if (patternId == 0) {
currentBuffer = buffer.toString();
}
buffer.setLength(0);
buffer.append(type);
buffer.append(patterns.get(patternId));
curPos = buffer.length();
} else if (patternId >= patterns.size()) {
patternId = patterns.size() - 1;
}
buffer.setLength(0);
buffer.append(type);
buffer.append(patternHistory.up(currentBuffer.substring(1)));
curPos = buffer.length();
break;
case DOWN:
if (patterns.size() > 0) {
patternId--;
buffer.setLength(0);
if (patternId < 0) {
patternId = -1;
buffer.append(currentBuffer);
} else {
buffer.append(type);
buffer.append(patterns.get(patternId));
}
curPos = buffer.length();
}
buffer.setLength(0);
buffer.append(type);
buffer.append(patternHistory.down(currentBuffer.substring(1)));
curPos = buffer.length();
break;
case ACCEPT:
try {
Expand All @@ -907,10 +905,7 @@ private boolean search() throws IOException, InterruptedException {
forward = false;
}
}
if (_pattern.length() > 0 && !patterns.contains(_pattern)) {
patterns.add(_pattern);
}
patternId = -1;
patternHistory.add(_pattern);
buffer.setLength(0);
} catch (PatternSyntaxException e) {
String str = e.getMessage();
Expand All @@ -931,6 +926,7 @@ private boolean search() throws IOException, InterruptedException {
return forward;
default:
curPos = lineEditor.editBuffer(op, curPos);
currentBuffer = buffer.toString();
break;
}
if (curPos < begPos) {
Expand Down
Loading

0 comments on commit c9e3f7d

Please sign in to comment.