Skip to content

Commit

Permalink
Сделал табуляцию по командам
Browse files Browse the repository at this point in the history
  • Loading branch information
lokha committed Apr 10, 2018
1 parent f5892ef commit 6427199
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
59 changes: 59 additions & 0 deletions src/main/java/ua/lokha/spigotloggerforbungee/CommandCompleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ua.lokha.spigotloggerforbungee;

import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.plugin.Command;
import org.apache.commons.lang3.StringUtils;
import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import ua.lokha.spigotloggerforbungee.utils.MyObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
* Completer command
*/
public class CommandCompleter implements Completer {
private final Pattern argsSplit = Pattern.compile(" ");
private final Map<String, Command> commandMap;
private BungeeCord bungeeCord;

public CommandCompleter(BungeeCord bungeeCord) {
this.bungeeCord = bungeeCord;
this.commandMap = MyObject.wrap(bungeeCord.getPluginManager()).getField("commandMap").getObject();
}

@Override
public void complete(LineReader reader1, ParsedLine line, List<Candidate> candidates) {
final String buffer = line.line();
List<String> suggestions = new ArrayList<>();
if (!this.suggestCommands(line, candidates)) {
bungeeCord.getPluginManager().dispatchCommand(bungeeCord.getConsole(), buffer, suggestions);
for (String suggestion : suggestions) {
candidates.add(new Candidate(suggestion));
}
}
// int lastSpace = buffer.lastIndexOf(32);
// return lastSpace == -1 ? cursor - buffer.length() : cursor - (buffer.length() - lastSpace - 1);
}

private boolean suggestCommands(ParsedLine line, List<Candidate> candidates) {
final String buffer = line.line();
String[] split = argsSplit.split(buffer, -1);
if (split.length == 1) {
final Command command = commandMap.get(split[0]);
if (command == null) {
commandMap.keySet().stream()
.filter(s -> StringUtils.startsWithIgnoreCase(s, buffer))
.forEach(s->candidates.add(new Candidate(s)));
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
import org.jline.terminal.Terminal;
import ua.lokha.spigotloggerforbungee.CommandCompleter;
import ua.lokha.spigotloggerforbungee.SpigotLoggerForBungeePlugin;

import java.util.ArrayList;
Expand All @@ -23,22 +24,11 @@ private TerminalHandler() {

public static boolean handleCommands(BungeeCord bungeeCord, SpigotLoggerForBungeePlugin plugin) {
Terminal terminal = TerminalConsoleAppender.getTerminal();
System.out.println("TERTIMAL: " + terminal);
if (terminal == null) {
return false;
} else {
LineReader reader = LineReaderBuilder.builder().appName("BungeeCord").terminal(terminal)
.completer((reader1, line, candidates) -> {
final String buffer = line.line();
List<String> suggestions = new ArrayList<>();
bungeeCord.getPluginManager().dispatchCommand(bungeeCord.getConsole(), buffer, suggestions);
System.out.println("COMPLETE " + suggestions);
for (String suggestion : suggestions) {
candidates.add(new Candidate(suggestion));
}
// int lastSpace = buffer.lastIndexOf(32);
// return lastSpace == -1 ? cursor - buffer.length() : cursor - (buffer.length() - lastSpace - 1);
})
.completer(new CommandCompleter(bungeeCord))
.build();
reader.unsetOpt(Option.INSERT_TAB);
TerminalConsoleAppender.setReader(reader);
Expand Down

0 comments on commit 6427199

Please sign in to comment.