Skip to content
Snippets Groups Projects
Commit a7ff9c2b authored by Ludovic Apvrille's avatar Ludovic Apvrille
Browse files

Update on history command

parent 70060a9e
No related branches found
No related tags found
No related merge requests found
...@@ -76,16 +76,31 @@ public class History extends Command { ...@@ -76,16 +76,31 @@ public class History extends Command {
return "hi"; return "hi";
} }
public String getUsage() { return "history"; } public String getUsage() { return "history [command id (optional)]"; }
public String getDescription() { public String getDescription() {
return "Prints all the non empty commands previously executed"; return "Prints all the non empty commands previously executed\n" +
" If an int arg is povided, the corresponding command is executed";
} }
public String executeCommand(String command, Interpreter interpreter) { public String executeCommand(String command, Interpreter interpreter) {
TraceManager.addDev("History command"); //TraceManager.addDev("History command:" + command);
return interpreter.printAllFormerCommands();
if (command.length() == 0) {
return interpreter.printAllFormerCommands();
}
try {
Integer indexInfo = new Integer(command);
return interpreter.executeFormerCommand(indexInfo);
} catch (Exception e) {
return "Invalid argument. Must provide an int";
}
} }
public void fillSubCommands() { public void fillSubCommands() {
......
...@@ -87,6 +87,7 @@ public class Interpreter implements Runnable, TerminalProviderInterface { ...@@ -87,6 +87,7 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
public MainGUI mgui; public MainGUI mgui;
private Vector<String> formerCommands; private Vector<String> formerCommands;
private Terminal term; private Terminal term;
private int currentLine;
public Interpreter(String script, InterpreterOutputInterface printInterface, boolean show) { public Interpreter(String script, InterpreterOutputInterface printInterface, boolean show) {
...@@ -108,10 +109,10 @@ public class Interpreter implements Runnable, TerminalProviderInterface { ...@@ -108,10 +109,10 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
term.setTerminalProvider(this); term.setTerminalProvider(this);
String line; String line;
int cptLine = 0; currentLine = 0;
while ((line = term.getNextCommand()) != null) { while ((line = term.getNextCommand()) != null) {
executeLine(line, cptLine, false); executeLine(line, currentLine, false);
cptLine ++; currentLine++;
} }
} }
...@@ -140,11 +141,11 @@ public class Interpreter implements Runnable, TerminalProviderInterface { ...@@ -140,11 +141,11 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
public void interpret() { public void interpret() {
Scanner scanner = new Scanner(script); Scanner scanner = new Scanner(script);
int cptLine = 0; currentLine = 0;
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String line = scanner.nextLine(); String line = scanner.nextLine();
cptLine ++; currentLine ++;
executeLine(line, cptLine, true); executeLine(line, currentLine, true);
} }
scanner.close(); scanner.close();
...@@ -302,6 +303,7 @@ public class Interpreter implements Runnable, TerminalProviderInterface { ...@@ -302,6 +303,7 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
printInterface.print(s); printInterface.print(s);
} }
// History
public String printAllFormerCommands() { public String printAllFormerCommands() {
StringBuffer sb = new StringBuffer(""); StringBuffer sb = new StringBuffer("");
for(int i=0; i<formerCommands.size(); i++) { for(int i=0; i<formerCommands.size(); i++) {
...@@ -311,6 +313,18 @@ public class Interpreter implements Runnable, TerminalProviderInterface { ...@@ -311,6 +313,18 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
return null; return null;
} }
public String executeFormerCommand(int indexOfCommand) {
if (indexOfCommand >= formerCommands.size() || (indexOfCommand < 0)) {
return "Invalid command index";
}
String formerCommand = formerCommands.get(indexOfCommand);
System.out.println("Executing: " + formerCommand);
executeLine(formerCommand, currentLine, false);
return null;
}
// Terminal provider interface // Terminal provider interface
public String getMidPrompt() { public String getMidPrompt() {
return "> "; return "> ";
......
...@@ -189,6 +189,8 @@ public class Terminal { ...@@ -189,6 +189,8 @@ public class Terminal {
// Usual CHAR // Usual CHAR
if ((sequence == null) && (val != -1)) { if ((sequence == null) && (val != -1)) {
// CR
if (val == CR) { if (val == CR) {
cursorPosition = 0; cursorPosition = 0;
if (currentBuf.length() == 0) { if (currentBuf.length() == 0) {
...@@ -196,9 +198,10 @@ public class Terminal { ...@@ -196,9 +198,10 @@ public class Terminal {
printPrompt(cpt); printPrompt(cpt);
} else { } else {
cpt++; cpt++;
if (!(os.startsWith("mac"))) {
//if (!(os.startsWith("mac"))) {
myPrint("\n"); myPrint("\n");
} //}
addToBuffer(currentBuf); addToBuffer(currentBuf);
return currentBuf; return currentBuf;
} }
...@@ -225,7 +228,7 @@ public class Terminal { ...@@ -225,7 +228,7 @@ public class Terminal {
myPrint("" + x); myPrint("" + x);
currentBuf += x; currentBuf += x;
} else { } else {
currentBuf = currentBuf.substring(0,cursorPosition) + x + currentBuf.substring(cursorPosition, currentBuf.length()); currentBuf = currentBuf.substring(0,cursorPosition-1) + x + currentBuf.substring(cursorPosition, currentBuf.length());
myPrint("" + x + currentBuf.substring(cursorPosition, currentBuf.length())); myPrint("" + x + currentBuf.substring(cursorPosition, currentBuf.length()));
} }
cursorPosition ++; cursorPosition ++;
...@@ -272,23 +275,9 @@ public class Terminal { ...@@ -272,23 +275,9 @@ public class Terminal {
//if (os.compareTo("mac") != 0) { //if (os.compareTo("mac") != 0) {
System.out.print(s); System.out.print(s);
//} //}
//System.out.flush(); System.out.flush();
}
public void printHistory() {
int cpt = 1;
for(String s: buffer) {
System.out.println("" + cpt+ ":" + s);
cpt ++;
}
} }
private void printSequence(String seq) {
for(int i=0; i<seq.length(); i++) {
System.out.print("" + (int)(seq.charAt(i)) + " ");
}
System.out.println("");
}
private String delCurrent(String currentBuf) { private String delCurrent(String currentBuf) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment