add history command. close #298

This commit is contained in:
hengyunabc 2018-11-18 23:42:35 +08:00
parent b839be59da
commit eaae4eb72a
2 changed files with 79 additions and 0 deletions

View File

@ -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.ClsCommand;
import com.taobao.arthas.core.command.basic1000.HelpCommand; 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.KeymapCommand;
import com.taobao.arthas.core.command.basic1000.ResetCommand; import com.taobao.arthas.core.command.basic1000.ResetCommand;
import com.taobao.arthas.core.command.basic1000.SessionCommand; 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(SystemPropertyCommand.class));
commands.add(Command.create(SystemEnvCommand.class)); commands.add(Command.create(SystemEnvCommand.class));
commands.add(Command.create(RedefineCommand.class)); commands.add(Command.create(RedefineCommand.class));
commands.add(Command.create(HistoryCommand.class));
} }
} }

View File

@ -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<int[]> history = readline.getHistory();
if (clear) {
readline.setHistory(new ArrayList<int[]>());
} 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();
}
}