mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-17 08:55:05 -04:00
Terminal handles the form feed and backspace character in a proper way. See #171
This commit is contained in:
parent
39afc030dc
commit
6a7282e52e
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Helmut Neemann.
|
||||
* Use of this source code is governed by the GPL v3 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
package de.neemann.digital.gui.components.terminal;
|
||||
|
||||
/**
|
||||
* Used to delete a char in a string and keep track of the cursor position
|
||||
*/
|
||||
public class CharDeleter {
|
||||
private String text;
|
||||
private int pos;
|
||||
|
||||
/**
|
||||
* Creaztes a new instance
|
||||
*
|
||||
* @param text the text
|
||||
* @param pos the cursor position
|
||||
*/
|
||||
public CharDeleter(String text, int pos) {
|
||||
this.text = text;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the text
|
||||
*/
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the cursor position
|
||||
*/
|
||||
public int getPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes the last char
|
||||
*
|
||||
* @return this for chained calls
|
||||
*/
|
||||
public CharDeleter delete() {
|
||||
final int len = text.length();
|
||||
if (len > 0) {
|
||||
char last = text.charAt(len - 1);
|
||||
text = text.substring(0, len - 1);
|
||||
pos--;
|
||||
|
||||
if (last == '\n') {
|
||||
pos = 0;
|
||||
int p = len - 2;
|
||||
while (p >= 0 && text.charAt(p) != '\n') {
|
||||
pos++;
|
||||
p--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
@ -66,16 +66,28 @@ public class TerminalDialog extends JDialog {
|
||||
* @param value the character
|
||||
*/
|
||||
public void addChar(char value) {
|
||||
if (value == 13 || value == 10) {
|
||||
pos = 0;
|
||||
textArea.append("\n");
|
||||
} else {
|
||||
textArea.append("" + value);
|
||||
pos++;
|
||||
if (pos == width) {
|
||||
switch (value) {
|
||||
case 13:
|
||||
case 10:
|
||||
pos = 0;
|
||||
textArea.append("\n");
|
||||
}
|
||||
break;
|
||||
case 8:
|
||||
CharDeleter cd = new CharDeleter(textArea.getText(), pos).delete();
|
||||
textArea.setText(cd.getText());
|
||||
pos = cd.getPos();
|
||||
break;
|
||||
case 12:
|
||||
pos = 0;
|
||||
textArea.setText("");
|
||||
break;
|
||||
default:
|
||||
textArea.append("" + value);
|
||||
pos++;
|
||||
if (pos == width) {
|
||||
pos = 0;
|
||||
textArea.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Helmut Neemann.
|
||||
* Use of this source code is governed by the GPL v3 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
package de.neemann.digital.gui.components.terminal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class CharDeleterTest extends TestCase {
|
||||
|
||||
public void testSimple() {
|
||||
CharDeleter cd = new CharDeleter("test", 4).delete();
|
||||
assertEquals("tes", cd.getText());
|
||||
assertEquals(3, cd.getPos());
|
||||
}
|
||||
|
||||
public void testLineFeed1() {
|
||||
CharDeleter cd = new CharDeleter("\n\n", 0).delete();
|
||||
assertEquals("\n", cd.getText());
|
||||
assertEquals(0, cd.getPos());
|
||||
}
|
||||
|
||||
public void testLineFeed2() {
|
||||
CharDeleter cd = new CharDeleter("\ntest\n", 0).delete();
|
||||
assertEquals("\ntest", cd.getText());
|
||||
assertEquals(4, cd.getPos());
|
||||
}
|
||||
|
||||
public void testLineFeed3() {
|
||||
CharDeleter cd = new CharDeleter("test\n", 0).delete();
|
||||
assertEquals("test", cd.getText());
|
||||
assertEquals(4, cd.getPos());
|
||||
}
|
||||
|
||||
public void testLineFeed4() {
|
||||
CharDeleter cd = new CharDeleter("test\ntest\n", 0).delete();
|
||||
assertEquals("test\ntest", cd.getText());
|
||||
assertEquals(4, cd.getPos());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user