Skip to content

Commit 7fce8af

Browse files
committed
fix bug in command mode
1 parent c4138eb commit 7fce8af

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ui/KeyHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class KeyHandler implements KeyListener {
1919
private PaintPanel paintPanel;
2020
private boolean inCommandMode;
2121
Interpreter interpreter ;
22-
private int curIndex;
22+
private boolean isCurrentDirectionNext;
2323

2424

2525
JTextField textField;
@@ -82,11 +82,11 @@ public void keyTyped(KeyEvent e) {
8282

8383
//record command
8484

85-
while(commandHistoryIter.hasNext())
85+
while(commandHistoryIter.hasNext()){
8686
this.commandHistoryIter.next();
87-
87+
}
88+
isCurrentDirectionNext = true;
8889
commandHistoryIter.add(textField.getText());
89-
curIndex++;
9090

9191
executeCommand(textField.getText().substring(PROMPT.length()));
9292
exitCommandMode();
@@ -106,18 +106,18 @@ public void keyPressed(KeyEvent e) {
106106
//if it is up arrow, go back in history
107107
} else if (e.getKeyCode() == KeyEvent.VK_UP ){
108108
if(commandHistoryIter.hasPrevious()){
109-
if(curIndex == commandHistoryIter.previousIndex()){
109+
if(isCurrentDirectionNext){
110110
commandHistoryIter.previous();
111111
}
112-
curIndex--;
112+
isCurrentDirectionNext = false;
113113
this.textField.setText(commandHistoryIter.previous());
114114
}
115115
} else if (e.getKeyCode() == KeyEvent.VK_DOWN ){
116116
if(commandHistoryIter.hasNext()){
117-
if(curIndex == commandHistoryIter.nextIndex()){
117+
if(!isCurrentDirectionNext){
118118
commandHistoryIter.next();
119119
}
120-
curIndex++;
120+
isCurrentDirectionNext = true;
121121
this.textField.setText(commandHistoryIter.next());
122122
}
123123
}

0 commit comments

Comments
 (0)