diff --git a/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java b/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java index 6682f22b..cb209566 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java +++ b/core/src/main/java/com/taobao/arthas/core/command/BuiltinCommandPack.java @@ -2,6 +2,7 @@ package com.taobao.arthas.core.command; import com.taobao.arthas.core.command.basic1000.ClsCommand; import com.taobao.arthas.core.command.basic1000.HelpCommand; +import com.taobao.arthas.core.command.basic1000.HistoryCommand; import com.taobao.arthas.core.command.basic1000.KeymapCommand; import com.taobao.arthas.core.command.basic1000.ResetCommand; import com.taobao.arthas.core.command.basic1000.SessionCommand; @@ -82,5 +83,6 @@ public class BuiltinCommandPack implements CommandResolver { commands.add(Command.create(SystemPropertyCommand.class)); commands.add(Command.create(SystemEnvCommand.class)); commands.add(Command.create(RedefineCommand.class)); + commands.add(Command.create(HistoryCommand.class)); } } diff --git a/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java b/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java new file mode 100644 index 00000000..2069738d --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/command/basic1000/HistoryCommand.java @@ -0,0 +1,77 @@ +package com.taobao.arthas.core.command.basic1000; + +import java.util.ArrayList; +import java.util.List; + +import com.taobao.arthas.core.command.Constants; +import com.taobao.arthas.core.shell.command.AnnotatedCommand; +import com.taobao.arthas.core.shell.command.CommandProcess; +import com.taobao.arthas.core.shell.session.Session; +import com.taobao.arthas.core.shell.term.impl.TermImpl; +import com.taobao.middleware.cli.annotations.Argument; +import com.taobao.middleware.cli.annotations.Description; +import com.taobao.middleware.cli.annotations.Name; +import com.taobao.middleware.cli.annotations.Option; +import com.taobao.middleware.cli.annotations.Summary; + +import io.termd.core.readline.Readline; + +/** + * + * @author hengyunabc 2018-11-18 + * + */ +@Name("history") +@Summary("Display command history") +@Description(Constants.EXAMPLE + " history\n" + " history -c\n" + " history 5\n") +public class HistoryCommand extends AnnotatedCommand { + boolean clear = false; + int n = -1; + + @Option(shortName = "c", longName = "clear") + @Description("clear history") + public void setClear(boolean clear) { + this.clear = clear; + } + + @Argument(index = 0, argName = "n", required = false) + @Description("how many history commnads to display") + public void setNumber(int n) { + this.n = n; + } + + @Override + public void process(CommandProcess process) { + Session session = process.session(); + Object termObject = session.get(Session.TTY); + if (termObject != null && termObject instanceof TermImpl) { + TermImpl term = (TermImpl) termObject; + Readline readline = term.getReadline(); + List history = readline.getHistory(); + + if (clear) { + readline.setHistory(new ArrayList()); + } else { + StringBuilder sb = new StringBuilder(); + + int size = history.size(); + if (n < 0) { + n = size; + } + + for (int i = 0; i < n; ++i) { + int[] line = history.get(n - i - 1); + sb.append(String.format("%5s ", size - (n - i - 1))); + for (int codePoint : line) { + sb.append(Character.toChars(codePoint)); + } + sb.append('\n'); + } + + process.write(sb.toString()); + } + } + + process.end(); + } +}