Skip to content

Commit

Permalink
FilesCompleter & DirectoryCompleter: force forward slash
Browse files Browse the repository at this point in the history
delimiter
  • Loading branch information
mattirn committed Jul 26, 2019
1 parent 00891d4 commit 5d12bcc
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,44 @@ private boolean isTrue(Object result) {
public static class DirectoriesCompleter extends FileNameCompleter {

private final Supplier<Path> currentDir;
private final boolean forceSlash;

public DirectoriesCompleter(File currentDir) {
this(currentDir.toPath());
this(currentDir.toPath(), false);
}

public DirectoriesCompleter(File currentDir, boolean forceSlash) {
this(currentDir.toPath(), forceSlash);
}

public DirectoriesCompleter(Path currentDir) {
this(currentDir, false);
}

public DirectoriesCompleter(Path currentDir, boolean forceSlash) {
this.currentDir = () -> currentDir;
this.forceSlash = forceSlash;
}

public DirectoriesCompleter(Supplier<Path> currentDir) {
this(currentDir, false);
}

public DirectoriesCompleter(Supplier<Path> currentDir, boolean forceSlash) {
this.currentDir = currentDir;
this.forceSlash = forceSlash;
}

@Override
protected Path getUserDir() {
return currentDir.get();
}

@Override
protected String getSeparator() {
return forceSlash ? "/" : getUserDir().getFileSystem().getSeparator();
}

@Override
protected boolean accept(Path path) {
return Files.isDirectory(path) && super.accept(path);
Expand All @@ -246,23 +266,43 @@ protected boolean accept(Path path) {
public static class FilesCompleter extends FileNameCompleter {

private final Supplier<Path> currentDir;
private final boolean forceSlash;

public FilesCompleter(File currentDir) {
this(currentDir.toPath());
this(currentDir.toPath(), false);
}

public FilesCompleter(File currentDir, boolean forceSlash) {
this(currentDir.toPath(), forceSlash);
}

public FilesCompleter(Path currentDir) {
this(currentDir, false);
}

public FilesCompleter(Path currentDir, boolean forceSlash) {
this.currentDir = () -> currentDir;
this.forceSlash = forceSlash;
}

public FilesCompleter(Supplier<Path> currentDir) {
this(currentDir, false);
}

public FilesCompleter(Supplier<Path> currentDir, boolean forceSlash) {
this.currentDir = currentDir;
this.forceSlash = forceSlash;
}

@Override
protected Path getUserDir() {
return currentDir.get();
}

@Override
protected String getSeparator() {
return forceSlash ? "/" : getUserDir().getFileSystem().getSeparator();
}
}

/**
Expand Down Expand Up @@ -295,7 +335,7 @@ public void complete(LineReader reader, ParsedLine commandLine, final List<Candi

Path current;
String curBuf;
String sep = getUserDir().getFileSystem().getSeparator();
String sep = getSeparator();
int lastSep = buffer.lastIndexOf(sep);
if (lastSep >= 0) {
curBuf = buffer.substring(0, lastSep + 1);
Expand Down Expand Up @@ -345,6 +385,10 @@ protected Path getUserDir() {
protected Path getUserHome() {
return Paths.get(System.getProperty("user.home"));
}

protected String getSeparator() {
return getUserDir().getFileSystem().getSeparator();
}

protected String getDisplay(Terminal terminal, Path p) {
// TODO: use $LS_COLORS for output
Expand Down

2 comments on commit 5d12bcc

@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.

Added optional boolean parameter forceSlash to DirectoriesCompleter and FilesCompleter constructors.
forceSlash true: '/' character is used to delimits path components
forceSlash false: (default) delimiting character is resolved using statement getUserDir().getFileSystem().getSeparator()

fixes #413

@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.