Skip to content

Commit

Permalink
Improve api separation, fixes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 5, 2017
1 parent 12dd8cc commit 66ce215
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 49 deletions.
114 changes: 112 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand All @@ -9,9 +9,11 @@
package org.jline.builtins;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -21,7 +23,9 @@
import org.jline.reader.Candidate;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.completer.FileNameCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

public class Completers {

Expand Down Expand Up @@ -245,4 +249,110 @@ protected Path getUserDir() {
}
}

/**
* A file name completer takes the buffer and issues a list of
* potential completions.
* <p/>
* This completer tries to behave as similar as possible to
* <i>bash</i>'s file name completion (using GNU readline)
* with the following exceptions:
* <p/>
* <ul>
* <li>Candidates that are directories will end with "/"</li>
* <li>Wildcard regular expressions are not evaluated or replaced</li>
* <li>The "~" character can be used to represent the user's home,
* but it cannot complete to other users' homes, since java does
* not provide any way of determining that easily</li>
* </ul>
*
* @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
* @since 2.3
*/
public static class FileNameCompleter implements org.jline.reader.Completer
{

public void complete(LineReader reader, ParsedLine commandLine, final List<Candidate> candidates) {
assert commandLine != null;
assert candidates != null;

String buffer = commandLine.word().substring(0, commandLine.wordCursor());

Path current;
String curBuf;
int lastSep = buffer.lastIndexOf(File.separator);
if (lastSep >= 0) {
curBuf = buffer.substring(0, lastSep + 1);
if (curBuf.startsWith("~")) {
if (curBuf.startsWith("~/")) {
current = getUserHome().resolve(curBuf.substring(2));
} else {
current = getUserHome().getParent().resolve(curBuf.substring(1));
}
} else {
current = getUserDir().resolve(curBuf);
}
} else {
curBuf = "";
current = getUserDir();
}
try {
Files.newDirectoryStream(current, this::accept).forEach(p -> {
String value = curBuf + p.getFileName().toString();
if (Files.isDirectory(p)) {
candidates.add(new Candidate(
value + (reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? "/" : ""),
getDisplay(reader.getTerminal(), p),
null, null,
reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? "/" : null,
null,
false));
} else {
candidates.add(new Candidate(value, getDisplay(reader.getTerminal(), p),
null, null, null, null, true));
}
});
} catch (IOException e) {
// Ignore
}
}

protected boolean accept(Path path) {
try {
return !Files.isHidden(path);
} catch (IOException e) {
return false;
}
}

protected Path getUserDir() {
return Paths.get(System.getProperty("user.dir"));
}

protected Path getUserHome() {
return Paths.get(System.getProperty("user.home"));
}

protected String getDisplay(Terminal terminal, Path p) {
// TODO: use $LS_COLORS for output
String name = p.getFileName().toString();
if (Files.isDirectory(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.append("/");
name = sb.toAnsi(terminal);
} else if (Files.isSymbolicLink(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.append("@");
name = sb.toAnsi(terminal);
}
return name;
}

}
}
4 changes: 2 additions & 2 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand Down Expand Up @@ -651,7 +651,7 @@ private void bindKeys(KeyMap<Operation> map) {
"-/0123456789?".chars().forEach(c -> map.bind(Operation.CHAR, Character.toString((char) c)));
}

enum Operation {
protected enum Operation {

// General
HELP,
Expand Down
10 changes: 5 additions & 5 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand Down Expand Up @@ -111,19 +111,19 @@ public class Nano {

protected boolean readNewBuffer = true;

enum WriteMode {
protected enum WriteMode {
WRITE,
APPEND,
PREPEND
}

enum WriteFormat {
protected enum WriteFormat {
UNIX,
DOS,
MAC
}

private class Buffer {
protected class Buffer {
String file;
Charset charset;
WriteFormat format = WriteFormat.UNIX;
Expand Down Expand Up @@ -2031,7 +2031,7 @@ protected void bindKeys() {
keys.bind(Operation.MOUSE_EVENT, key(terminal, Capability.key_mouse));
}

enum Operation {
protected enum Operation {
DO_LOWER_CASE,

QUIT,
Expand Down
6 changes: 3 additions & 3 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand All @@ -17,12 +17,12 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.jline.builtins.Completers;
import org.jline.keymap.KeyMap;
import org.jline.reader.*;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.FileNameCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
Expand Down Expand Up @@ -115,7 +115,7 @@ public static void main(String[] args) throws IOException {
case "none":
break label;
case "files":
completer = new FileNameCompleter();
completer = new Completers.FileNameCompleter();
break label;
case "simple":
completer = new StringsCompleter("foo", "bar", "baz");
Expand Down
77 changes: 76 additions & 1 deletion reader/src/main/java/org/jline/reader/Buffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand All @@ -9,4 +9,79 @@
package org.jline.reader;

public interface Buffer {

/*
* Read access
*/

int cursor();

int atChar(int i);

int length();

int currChar();

int prevChar();

int nextChar();

/*
* Movement
*/

boolean cursor(int position);

int move(int num);

boolean up();

boolean down();

boolean moveXY(int dx, int dy);

/*
* Modification
*/

boolean clear();

boolean currChar(int c);

void write(int c);

void write(int c, boolean overTyping);

void write(CharSequence str);

void write(CharSequence str, boolean overTyping);

boolean backspace();

int backspace(int num);

boolean delete();

int delete(int num);

/*
* String
*/

String substring(int start);

String substring(int start, int end);

String upToCursor();

String toString();

/*
* Copy
*/

Buffer copy();

void copyFrom(Buffer buffer);

}
10 changes: 8 additions & 2 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand Down Expand Up @@ -468,7 +468,7 @@ enum RegionType {

Map<String, Widget> getBuiltinWidgets();

BufferImpl getBuffer();
Buffer getBuffer();

void runMacro(String macro);

Expand All @@ -482,6 +482,12 @@ enum RegionType {

Map<String, KeyMap<Binding>> getKeyMaps();

String getKeyMap();

boolean setKeyMap(String name);

KeyMap<Binding> getKeys();

ParsedLine getParsedLine();

String getSearchTerm();
Expand Down
10 changes: 7 additions & 3 deletions reader/src/main/java/org/jline/reader/impl/BufferImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand Down Expand Up @@ -39,7 +39,7 @@ public BufferImpl(int size) {

public BufferImpl copy () {
BufferImpl that = new BufferImpl();
that.setBuffer(this);
that.copyFrom(this);
return that;
}

Expand Down Expand Up @@ -338,7 +338,11 @@ public String toString() {
return substring(0, length());
}

public void setBuffer(BufferImpl that) {
public void copyFrom(Buffer buf) {
if (!(buf instanceof BufferImpl)) {
throw new IllegalStateException();
}
BufferImpl that = (BufferImpl) buf;
this.g0 = that.g0;
this.g1 = that.g1;
this.buffer = that.buffer.clone();
Expand Down
Loading

0 comments on commit 66ce215

Please sign in to comment.