improve cat/mc completion

This commit is contained in:
hengyunabc
2019-02-07 15:28:06 +08:00
parent c4751931ce
commit 79fe3a9382
3 changed files with 89 additions and 63 deletions
@@ -80,71 +80,9 @@ public class CatCommand extends AnnotatedCommand {
@Override
public void complete(Completion completion) {
List<CliToken> tokens = completion.lineTokens();
String token = tokens.get(tokens.size() - 1).value();
File dir = null;
String partName = "";
if (StringUtils.isBlank(token)) {
dir = new File("").getAbsoluteFile();
token = "";
} else if (token.endsWith("/")) {
dir = new File(token);
} else {
File parent = new File(token).getAbsoluteFile().getParentFile();
if (parent != null && parent.exists()) {
dir = parent;
partName = new File(token).getName();
}
}
File tokenFile = new File(token);
String tokenFileName = null;
if (token.endsWith("/")) {
tokenFileName = "";
} else {
tokenFileName = tokenFile.getName();
}
if (dir == null) {
if (!CompletionUtils.completeFilePath(completion)) {
super.complete(completion);
return;
}
File[] listFiles = dir.listFiles();
ArrayList<String> names = new ArrayList<String>();
for (File child : listFiles) {
if (child.getName().startsWith(partName)) {
if (child.isDirectory()) {
names.add(child.getName() + "/");
} else {
names.add(child.getName());
}
}
}
if (names.size() == 1 && names.get(0).endsWith("/")) {
String name = names.get(0);
// 这个函数补全后不会有空格,并且只能传入要补全的内容
completion.complete(name.substring(tokenFileName.length(), name.length()), false);
return;
}
String prefix = null;
if (token.endsWith("/")) {
prefix = token;
} else {
prefix = token.substring(0, token.length() - new File(token).getName().length());
}
ArrayList<String> namesWithPrefix = new ArrayList<String>();
for (String name : names) {
namesWithPrefix.add(prefix + name);
}
// 这个函数需要保留前缀
CompletionUtils.complete(completion, namesWithPrefix);
}
}
@@ -9,6 +9,8 @@ import java.util.Map.Entry;
import com.taobao.arthas.compiler.DynamicCompiler;
import com.taobao.arthas.core.command.Constants;
import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.cli.CompletionUtils;
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.arthas.core.util.ClassLoaderUtils;
@@ -128,4 +130,11 @@ public class MemoryCompilerCommand extends AnnotatedCommand {
process.end(exitCode);
}
}
@Override
public void complete(Completion completion) {
if (!CompletionUtils.completeFilePath(completion)) {
super.complete(completion);
}
}
}
@@ -2,12 +2,14 @@ package com.taobao.arthas.core.shell.cli;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.shell.term.Tty;
import com.taobao.arthas.core.util.StringUtils;
import com.taobao.arthas.core.util.usage.StyledUsageFormatter;
import com.taobao.middleware.cli.CLI;
import com.taobao.middleware.cli.Option;
import com.taobao.middleware.cli.annotations.CLIConfigurator;
import io.termd.core.util.Helper;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -65,6 +67,83 @@ public class CompletionUtils {
}
}
/**
* 返回true表示已经完成completion,返回否则表示没有,调用者需要另外完成补全
* @param completion
* @return
*/
public static boolean completeFilePath(Completion completion) {
List<CliToken> tokens = completion.lineTokens();
String token = tokens.get(tokens.size() - 1).value();
if (token.startsWith("-") || StringUtils.isBlank(token)) {
return false;
}
File dir = null;
String partName = "";
if (StringUtils.isBlank(token)) {
dir = new File("").getAbsoluteFile();
token = "";
} else if (token.endsWith("/")) {
dir = new File(token);
} else {
File parent = new File(token).getAbsoluteFile().getParentFile();
if (parent != null && parent.exists()) {
dir = parent;
partName = new File(token).getName();
}
}
File tokenFile = new File(token);
String tokenFileName = null;
if (token.endsWith("/")) {
tokenFileName = "";
} else {
tokenFileName = tokenFile.getName();
}
if (dir == null) {
return false;
}
File[] listFiles = dir.listFiles();
ArrayList<String> names = new ArrayList<String>();
for (File child : listFiles) {
if (child.getName().startsWith(partName)) {
if (child.isDirectory()) {
names.add(child.getName() + "/");
} else {
names.add(child.getName());
}
}
}
if (names.size() == 1 && names.get(0).endsWith("/")) {
String name = names.get(0);
// 这个函数补全后不会有空格,并且只能传入要补全的内容
completion.complete(name.substring(tokenFileName.length(), name.length()), false);
return true;
}
String prefix = null;
if (token.endsWith("/")) {
prefix = token;
} else {
prefix = token.substring(0, token.length() - new File(token).getName().length());
}
ArrayList<String> namesWithPrefix = new ArrayList<String>();
for (String name : names) {
namesWithPrefix.add(prefix + name);
}
// 这个函数需要保留前缀
CompletionUtils.complete(completion, namesWithPrefix);
return true;
}
public static void completeShortOption(Completion completion, CliToken lastToken, List<Option> options) {
String prefix = lastToken.value().substring(1);
List<String> candidates = new ArrayList<String>();