Skip to content

Commit 2d3d9ba

Browse files
javier-godoymlopezFC
authored andcommitted
refactor: move TerminalHistory out of XTerm class
1 parent d456d34 commit 2d3d9ba

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/main/java/com/flowingcode/vaadin/addons/xterm/TerminalHistory.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.flowingcode.vaadin.addons.xterm;
22

3+
import com.vaadin.flow.component.ComponentUtil;
34
import com.vaadin.flow.component.Key;
45
import com.vaadin.flow.shared.Registration;
56
import java.io.IOException;
@@ -33,8 +34,23 @@ public class TerminalHistory implements Serializable {
3334

3435
private Integer maxSize;
3536

36-
public <T extends XTermBase & ITerminalConsole> TerminalHistory(T terminal) {
37+
protected <T extends XTermBase & ITerminalConsole> TerminalHistory(T terminal) {
38+
if (TerminalHistory.of(terminal) != null) {
39+
throw new IllegalArgumentException("The terminal already has a history");
40+
}
3741
this.terminal = terminal;
42+
ComponentUtil.setData(terminal, TerminalHistory.class, this);
43+
}
44+
45+
/** Returns the command history of the terminal. */
46+
public static <T extends XTermBase & ITerminalConsole> TerminalHistory of(T xterm) {
47+
return ComponentUtil.getData(xterm, TerminalHistory.class);
48+
}
49+
50+
/** Adds support for command history to the given terminal. */
51+
public static <T extends XTermBase & ITerminalConsole> void extend(XTerm xterm) {
52+
TerminalHistory history = new TerminalHistory(xterm);
53+
history.setEnabled(true);
3854
}
3955

4056
/**

src/main/java/com/flowingcode/vaadin/addons/xterm/XTerm.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,8 @@
2929
public class XTerm extends XTermBase
3030
implements ITerminalFit, ITerminalConsole, ITerminalClipboard {
3131

32-
private TerminalHistory history;
33-
3432
public XTerm() {
3533
setInsertMode(true);
3634
}
3735

38-
/** Returns the terminal history. */
39-
public TerminalHistory getHistory() {
40-
if (history == null) {
41-
history = new TerminalHistory(this);
42-
}
43-
return history;
44-
}
45-
4636
}

src/test/java/com/flowingcode/vaadin/addons/xterm/XtermDemoView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public XtermDemoView() {
5757
xterm.setPasteWithMiddleClick(true);
5858
xterm.setPasteWithRightClick(true);
5959

60-
xterm.getHistory().setEnabled(true);
60+
TerminalHistory.extend(xterm);
6161

6262
xterm.addLineListener(
6363
ev -> {
@@ -109,7 +109,7 @@ public XtermDemoView() {
109109
private void showHistory() {
110110
int index = 1;
111111
StringBuilder sb = new StringBuilder();
112-
for (String line : xterm.getHistory().getLines()) {
112+
for (String line : TerminalHistory.of(xterm).getLines()) {
113113
sb.append(String.format("%5s %s\n", index++, line));
114114
}
115115
xterm.write(sb.toString());

0 commit comments

Comments
 (0)