Skip to content

Commit

Permalink
Improve (nano & less) command configuration management
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 19, 2019
1 parent fd8743f commit 270b52f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 18 deletions.
16 changes: 9 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jline.builtins.Source.URLSource;
import org.jline.keymap.KeyMap;
import org.jline.reader.Binding;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.Highlighter;
import org.jline.reader.History;
import org.jline.reader.LineReader;
Expand Down Expand Up @@ -103,9 +104,9 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
}

public static void nano(Terminal terminal, PrintStream out, PrintStream err,
Path currentDir,
String[] argv,
Path nanorc) throws Exception {
Path currentDir,
String[] argv,
ConfigurationPath configPath) throws Exception {
final String[] usage = {
"nano - edit files",
"Usage: nano [OPTIONS] [FILES]",
Expand Down Expand Up @@ -135,7 +136,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
if (opt.isSet("help")) {
throw new HelpException(opt.usage());
}
Nano edit = new Nano(terminal, currentDir, opt, nanorc);
Nano edit = new Nano(terminal, currentDir, opt, configPath);
edit.open(opt.args());
edit.run();
}
Expand All @@ -145,10 +146,11 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
String[] argv) throws Exception {
less(terminal, in, out, err, currentDir, argv, null);
}

public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
Path currentDir,
String[] argv,
Path lessrc) throws Exception {
ConfigurationPath configPath) throws Exception {
final String[] usage = {
"less - file pager",
"Usage: less [OPTIONS] [FILES]",
Expand All @@ -166,7 +168,7 @@ 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."

};

Expand All @@ -176,7 +178,7 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
throw new HelpException(opt.usage());
}

Less less = new Less(terminal, currentDir, opt, lessrc);
Less less = new Less(terminal, currentDir, opt, configPath);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
opt.args().add("-");
Expand Down
18 changes: 12 additions & 6 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jline.builtins.Source.URLSource;
import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.reader.ConfigurationPath;
import org.jline.terminal.Attributes;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
Expand Down Expand Up @@ -115,16 +116,21 @@ public class Less {


public Less(Terminal terminal, Path currentDir) {
this(terminal, currentDir, null, null);
this(terminal, currentDir, null);
}

public Less(Terminal terminal, Path currentDir, Options opts, Path lessrc) {
public Less(Terminal terminal, Path currentDir, Options opts) {
this(terminal, currentDir, opts, null);
}

public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath configPath) {
this.terminal = terminal;
this.display = new Display(terminal, true);
this.bindingReader = new BindingReader(terminal.reader());
this.currentDir = currentDir;
Path lessrc = configPath != null ? configPath.getConfig("jlessrc") : null;
boolean ignorercfiles = opts!=null && opts.isSet("ignorercfiles");
if (lessrc != null && lessrc.toFile().exists() && !ignorercfiles) {
if (lessrc != null && !ignorercfiles) {
try {
parseConfig(lessrc);
} catch (IOException e) {
Expand Down Expand Up @@ -1103,7 +1109,7 @@ void moveForward(int lines) throws IOException {
int width = size.getColumns() - (printLineNumbers ? 8 : 0);
int height = size.getRows();
boolean doOffsets = firstColumnToDisplay == 0 && !chopLongLines;
if (lines >= size.getRows() - 1) {
if (lines >= size.getRows() - 1) {
display.clear();
}
if (lines == Integer.MAX_VALUE) {
Expand Down Expand Up @@ -1156,7 +1162,7 @@ void moveForward(int lines) throws IOException {
void moveBackward(int lines) throws IOException {
Pattern dpCompiled = getPattern(true);
int width = size.getColumns() - (printLineNumbers ? 8 : 0);
if (lines >= size.getRows() - 1) {
if (lines >= size.getRows() - 1) {
display.clear();
}
while (--lines >= 0) {
Expand Down Expand Up @@ -1302,7 +1308,7 @@ synchronized boolean display(boolean oneScreen, Integer curPos) throws IOExcepti
if (MESSAGE_FILE_INFO.equals(message)){
Source source = sources.get(sourceIdx);
Long allLines = source.lines();
message = source.getName()
message = source.getName()
+ (sources.size() > 2 ? " (file " + sourceIdx + " of " + (sources.size() - 1) + ")" : "")
+ " lines " + (firstLineToDisplay + 1) + "-" + inputLine + "/" + (allLines != null ? allLines : lines.size())
+ (eof ? " (END)" : "");
Expand Down
8 changes: 5 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import org.jline.keymap.BindingReader;
import org.jline.keymap.KeyMap;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.Editor;
import org.jline.terminal.Attributes;
import org.jline.terminal.Attributes.ControlChar;
Expand Down Expand Up @@ -1572,19 +1573,20 @@ public Nano(Terminal terminal, Path root) {
}

public Nano(Terminal terminal, Path root, Options opts) {
this(terminal, root, null, null);
this(terminal, root, opts, null);
}

public Nano(Terminal terminal, Path root, Options opts, Path nanorc) {
public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath configPath) {
this.terminal = terminal;
this.root = root;
this.display = new Display(terminal, true);
this.bindingReader = new BindingReader(terminal.reader());
this.size = new Size();
this.vsusp = terminal.getAttributes().getControlChar(ControlChar.VSUSP);
bindKeys();
Path nanorc = configPath != null ? configPath.getConfig("jnanorc") : null;
boolean ignorercfiles = opts!=null && opts.isSet("ignorercfiles");
if (nanorc != null && nanorc.toFile().exists() && !ignorercfiles) {
if (nanorc != null && !ignorercfiles) {
try {
parseConfig(nanorc);
} catch (IOException e) {
Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ protected void less(CommandSession session, Process process, String[] argv) thro
" -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."

};
try {
Expand Down Expand Up @@ -1003,7 +1003,7 @@ protected void less(CommandSession session, Process process, String[] argv) thro
return;
}

Less less = new Less(Shell.getTerminal(session), session.currentDir(), opt, null);
Less less = new Less(Shell.getTerminal(session), session.currentDir(), opt);
less.run(sources);
}

Expand Down
75 changes: 75 additions & 0 deletions reader/src/main/java/org/jline/reader/ConfigurationPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2002-2019, 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.reader;

import java.io.IOException;
import java.nio.file.Path;

public class ConfigurationPath {
private Path appConfig;
private Path userConfig;

/**
* Configuration class constructor.
* @param appConfig Application configuration directory
* @param userConfig User private configuration directory
*/
public ConfigurationPath(Path appConfig, Path userConfig) {
this.appConfig = appConfig;
this.userConfig = userConfig;
}

/**
* Search configuration file first from userConfig and then appConfig directory. Returns null if file is not found.
* @param name Configuration file name.
* @return Configuration file.
*
*/
public Path getConfig(String name) {
Path out = null;
if (userConfig != null && userConfig.resolve(name).toFile().exists()) {
out = userConfig.resolve(name);
} else if (appConfig != null && appConfig.resolve(name).toFile().exists()) {
out = appConfig.resolve(name);
}
return out;
}

/**
* Search configuration file from userConfig directory. Returns null if file is not found.
* @param name Configuration file name.
* @return Configuration file.
* @throws IOException When we do not have read access to the file or directory.
*
*/
public Path getUserConfig(String name) throws IOException {
return getUserConfig(name, false);
}

/**
* Search configuration file from userConfig directory. Returns null if file is not found.
* @param name Configuration file name
* @param create When true configuration file is created if not found.
* @return Configuration file.
* @throws IOException When we do not have read/write access to the file or directory.
*/
public Path getUserConfig(String name, boolean create) throws IOException {
Path out = null;
if (userConfig != null) {
if (!userConfig.resolve(name).toFile().exists() && create) {
userConfig.resolve(name).toFile().createNewFile();
}
if (userConfig.resolve(name).toFile().exists()) {
out = userConfig.resolve(name);
}
}
return out;
}

}

0 comments on commit 270b52f

Please sign in to comment.