From 79fe3a9382829bde6b5ff8ae265ce60633c4ad5f Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Thu, 7 Feb 2019 15:28:06 +0800 Subject: [PATCH] improve cat/mc completion --- .../core/command/basic1000/CatCommand.java | 64 +-------------- .../klass100/MemoryCompilerCommand.java | 9 +++ .../core/shell/cli/CompletionUtils.java | 79 +++++++++++++++++++ 3 files changed, 89 insertions(+), 63 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java b/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java index 6ce2fe060..90c9b272d 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/basic1000/CatCommand.java @@ -80,71 +80,9 @@ public class CatCommand extends AnnotatedCommand { @Override public void complete(Completion completion) { - List 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 names = new ArrayList(); - 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 namesWithPrefix = new ArrayList(); - for (String name : names) { - namesWithPrefix.add(prefix + name); - } - // 这个函数需要保留前缀 - CompletionUtils.complete(completion, namesWithPrefix); } } diff --git a/core/src/main/java/com/taobao/arthas/core/command/klass100/MemoryCompilerCommand.java b/core/src/main/java/com/taobao/arthas/core/command/klass100/MemoryCompilerCommand.java index 01aad6a74..d4ed15c9d 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/klass100/MemoryCompilerCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/klass100/MemoryCompilerCommand.java @@ -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); + } + } } diff --git a/core/src/main/java/com/taobao/arthas/core/shell/cli/CompletionUtils.java b/core/src/main/java/com/taobao/arthas/core/shell/cli/CompletionUtils.java index eb63a64ce..548ab0c62 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/cli/CompletionUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/cli/CompletionUtils.java @@ -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 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 names = new ArrayList(); + 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 namesWithPrefix = new ArrayList(); + for (String name : names) { + namesWithPrefix.add(prefix + name); + } + // 这个函数需要保留前缀 + CompletionUtils.complete(completion, namesWithPrefix); + return true; + } + public static void completeShortOption(Completion completion, CliToken lastToken, List