Skip to content

Commit

Permalink
Nano: manage configuration file. Now support only 'include' entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 12, 2019
1 parent 5888c78 commit 470206b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 32 deletions.
13 changes: 10 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ public static void tmux(Terminal terminal, PrintStream out, PrintStream err,
}
}

public static void nano(Terminal terminal, PrintStream out, PrintStream err,
Path currentDir,
String[] argv) throws Exception {
nano(terminal, out, err, currentDir, argv, null);
}

public static void nano(Terminal terminal, PrintStream out, PrintStream err,
Path currentDir,
String[] argv) throws Exception {
String[] argv,
Path nanorc) throws Exception {
final String[] usage = {
"nano - edit files",
"Usage: nano [OPTIONS] [FILES]",
Expand All @@ -114,7 +121,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);
Nano edit = new Nano(terminal, currentDir, opt, nanorc);
edit.open(opt.args());
edit.run();
}
Expand Down Expand Up @@ -224,7 +231,7 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
}
History history = reader.getHistory();
boolean done = true;
boolean increment = opt.isSet("I");
boolean increment = opt.isSet("I");
if (opt.isSet("clear")) {
history.purge();
} else if (opt.isSet("save")) {
Expand Down
121 changes: 92 additions & 29 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class Nano implements Editor {
public String matchBrackets = "(<[{)>]}";
public String punct = "!.?";
public String quoteStr = "^([ \\t]*[#:>\\|}])+";
private List<Path> syntaxFiles = new ArrayList<>();

// Input
protected final List<Buffer> buffers = new ArrayList<>();
Expand Down Expand Up @@ -121,6 +122,7 @@ public class Nano implements Editor {
protected boolean mark = false;

protected boolean readNewBuffer = true;
protected String errorMessage = null;

protected enum WriteMode {
WRITE,
Expand Down Expand Up @@ -727,25 +729,15 @@ void highlightDisplayedLine(int curLine, int curOffset, int nextOffset, Attribut
private SyntaxHighlighter doSyntaxHighlighter() {
SyntaxHighlighter out = new SyntaxHighlighter();
if (file != null) {
File[] dirs = {new File(System.getProperty("user.home") + "/.nanorc")
, new File("/etc/nano")
, new File("/usr/share/nano")};
for (File d: dirs) {
if (!d.exists() || !d.isDirectory()) {
continue;
}
for (File f: d.listFiles()) {
if (f.getName().endsWith(".nanorc")) {
NanorcParser parser = new NanorcParser(f, file);
try {
parser.parse();
if (parser.matches()) {
out.addRules(parser.getHighlightRules());
return out;
}
} catch (IOException e) {
}
for (Path p: syntaxFiles) {
NanorcParser parser = new NanorcParser(p, file);
try {
parser.parse();
if (parser.matches()) {
out.addRules(parser.getHighlightRules());
return out;
}
} catch (IOException e) {
}
}
}
Expand Down Expand Up @@ -1395,14 +1387,57 @@ public static RuleType evalRuleType(List<String> colorCfg){

}

private static class NanorcParser {
private static class ConfigParser extends Parser {
private File file;
private String message;
private List<Path> syntaxFiles = new ArrayList<>();

public ConfigParser(Path file) {
this.file = file.toFile();
}

public void parse() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line!= null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(Paths.get(new File(parts.get(1)).getParent()), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(p -> syntaxFiles.add(p));
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
} else {
message = "Nano config: Only 'include' entries are supported!";
}
}
line = reader.readLine();
}
reader.close();
}

public String getMessage() {
return message;
}

public List<Path> getSyntaxFiles() {
return syntaxFiles;
}

}

private static class NanorcParser extends Parser {
private File file;
private String target;
private boolean matches = false;
private List<HighlightRule> highlightRules = new ArrayList<>();

public NanorcParser(File file, String target) {
this.file = file;
public NanorcParser(Path file, String target) {
this.file = file.toFile();
this.target = target;
}

Expand Down Expand Up @@ -1510,7 +1545,10 @@ private Pattern doPattern(String regex, boolean caseInsensitive) {
: Pattern.compile(regex);
}

private List<String> split(String s){
}

private static abstract class Parser {
protected List<String> split(String s){
List<String> out = new ArrayList<String>();
if (s.length() == 0) {
return out;
Expand All @@ -1522,7 +1560,7 @@ private List<String> split(String s){
if (c == '"') {
depth = depth == 0 ? 1 : 0;
} else if (c ==' ' && depth == 0 && sb.length() > 0) {
out.add(stripQuotes(sb.toString().trim()));
out.add(stripQuotes(sb.toString()));
sb = new StringBuilder();
continue;
}
Expand All @@ -1531,13 +1569,13 @@ private List<String> split(String s){
}
}
if (sb.length() > 0) {
out.add(stripQuotes(sb.toString().trim()));
out.add(stripQuotes(sb.toString()));
}
return out;
}

private String stripQuotes(String s){
String out = s;
String out = s.trim();
if (s.startsWith("\"") && s.endsWith("\"")) {
out = s.substring(1, s.length() - 1);
}
Expand All @@ -1554,6 +1592,10 @@ public Nano(Terminal terminal, Path root) {
}

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

public Nano(Terminal terminal, Path root, Options opts, Path nanorc) {
this.terminal = terminal;
this.root = root;
this.display = new Display(terminal, true);
Expand All @@ -1562,6 +1604,24 @@ public Nano(Terminal terminal, Path root, Options opts) {
this.restricted = opts != null && opts.isSet("restricted");
this.vsusp = terminal.getAttributes().getControlChar(ControlChar.VSUSP);
bindKeys();
if (nanorc != null && nanorc.toFile().exists()) {
ConfigParser parser = new ConfigParser(nanorc);
try {
parser.parse();
syntaxFiles = parser.getSyntaxFiles();
errorMessage = parser.getMessage();
} catch (IOException e) {
errorMessage = "Encountered error while reading config file: " + nanorc;
}
} else if (new File("/usr/share/nano").exists()) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc");
try {
Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(p -> syntaxFiles.add(p));
} catch (IOException e) {
errorMessage = "Encountered error while reading nanorc files";
}
}
}

public void setRestricted(boolean restricted) {
Expand All @@ -1575,7 +1635,7 @@ public void open(String... files) throws IOException {
public void open(List<String> files) throws IOException {
for (String file : files) {
if (file.contains("*") || file.contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:"+file);
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + file);
Files.find(root, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(p -> buffers.add(new Buffer(p.toString())));
} else {
Expand Down Expand Up @@ -1617,7 +1677,10 @@ public void run() throws IOException {
status.suspend();
}
buffer.open();
if (buffer.file != null) {
if (errorMessage != null) {
setMessage(errorMessage);
errorMessage = null;
} else if (buffer.file != null) {
setMessage("Read " + buffer.lines.size() + " lines");
}

Expand Down Expand Up @@ -2535,8 +2598,8 @@ void nextBuffer() throws IOException {
}

void setMessage(String message) {
this.message = message;
this.nbBindings = 25;
this.message = message;
this.nbBindings = 25;
}

boolean quit() throws IOException {
Expand Down

0 comments on commit 470206b

Please sign in to comment.