File tree Expand file tree Collapse file tree 3 files changed +19
-13
lines changed
main/java/com/flowingcode/vaadin/addons/xterm
test/java/com/flowingcode/vaadin/addons/xterm Expand file tree Collapse file tree 3 files changed +19
-13
lines changed Original file line number Diff line number Diff line change 11package com .flowingcode .vaadin .addons .xterm ;
22
3+ import com .vaadin .flow .component .ComponentUtil ;
34import com .vaadin .flow .component .Key ;
45import com .vaadin .flow .shared .Registration ;
56import 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 /**
Original file line number Diff line number Diff line change 2929public 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}
Original file line number Diff line number Diff 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 ());
You can’t perform that action at this time.
0 commit comments