18
18
import java .util .ArrayList ;
19
19
import java .util .stream .Collectors ;
20
20
21
+ import org .slf4j .Logger ;
22
+ import org .slf4j .LoggerFactory ;
23
+
21
24
import org .springframework .shell .component .message .ShellMessageBuilder ;
22
25
import org .springframework .shell .component .view .event .KeyEvent ;
23
26
import org .springframework .shell .component .view .event .KeyEvent .Key ;
33
36
*/
34
37
public class InputView extends BoxView {
35
38
39
+ private final Logger log = LoggerFactory .getLogger (InputView .class );
36
40
private final ArrayList <String > text = new ArrayList <>();
37
41
private int cursorIndex = 0 ;
38
42
39
43
@ Override
40
44
protected void initInternal () {
41
- registerKeyBinding (Key .CursorLeft , event -> left ());
42
- registerKeyBinding (Key .CursorRight , event -> right ());
43
- registerKeyBinding (Key .Delete , () -> delete ());
44
- registerKeyBinding (Key .Backspace , () -> backspace ());
45
- registerKeyBinding (Key .Enter , () -> done ());
45
+ registerViewCommand (ViewCommand .ACCEPT , () -> done ());
46
+ registerViewCommand (ViewCommand .LEFT , () -> left ());
47
+ registerViewCommand (ViewCommand .RIGHT , () -> right ());
48
+ registerViewCommand (ViewCommand .DELETE_CHAR_LEFT , () -> deleteCharLeft ());
49
+ registerViewCommand (ViewCommand .DELETE_CHAR_RIGHT , () -> deleteCharRight ());
50
+
51
+ registerKeyBinding (Key .Enter , ViewCommand .ACCEPT );
52
+ registerKeyBinding (Key .CursorLeft , ViewCommand .LEFT );
53
+ registerKeyBinding (Key .CursorRight , ViewCommand .RIGHT );
54
+ registerKeyBinding (Key .Backspace , ViewCommand .DELETE_CHAR_LEFT );
55
+ registerKeyBinding (Key .Delete , ViewCommand .DELETE_CHAR_RIGHT );
46
56
}
47
57
48
58
@ Override
49
59
public KeyHandler getKeyHandler () {
60
+ log .trace ("getKeyHandler()" );
50
61
KeyHandler handler = args -> {
51
62
KeyEvent event = args .event ();
52
63
boolean consumed = false ;
@@ -68,9 +79,11 @@ protected void drawInternal(Screen screen) {
68
79
Rectangle rect = getInnerRect ();
69
80
String s = getInputText ();
70
81
screen .writerBuilder ().build ().text (s , rect .x (), rect .y ());
71
- screen .setShowCursor (hasFocus ());
72
- int cPos = cursorPosition ();
73
- screen .setCursorPosition (new Position (rect .x () + cPos , rect .y ()));
82
+ if (hasFocus ()) {
83
+ screen .setShowCursor (true );
84
+ int cPos = cursorPosition ();
85
+ screen .setCursorPosition (new Position (rect .x () + cPos , rect .y ()));
86
+ }
74
87
super .drawInternal (screen );
75
88
}
76
89
@@ -87,21 +100,34 @@ private int cursorPosition() {
87
100
return text .stream ().limit (cursorIndex ).mapToInt (text -> text .length ()).sum ();
88
101
}
89
102
103
+ private void dispatchTextChange (String oldText , String newText ) {
104
+ dispatch (ShellMessageBuilder .ofView (this , InputViewTextChangeEvent .of (this , oldText , newText )));
105
+ }
106
+
90
107
private void add (String data ) {
108
+ String oldText = text .stream ().collect (Collectors .joining ());
91
109
text .add (cursorIndex , data );
92
110
moveCursor (1 );
111
+ String newText = text .stream ().collect (Collectors .joining ());
112
+ dispatchTextChange (oldText , newText );
93
113
}
94
114
95
- private void backspace () {
115
+ private void deleteCharLeft () {
96
116
if (cursorIndex > 0 ) {
117
+ String oldText = text .stream ().collect (Collectors .joining ());
97
118
text .remove (cursorIndex - 1 );
119
+ String newText = text .stream ().collect (Collectors .joining ());
120
+ dispatchTextChange (oldText , newText );
98
121
}
99
122
left ();
100
123
}
101
124
102
- private void delete () {
125
+ private void deleteCharRight () {
103
126
if (cursorIndex < text .size ()) {
127
+ String oldText = text .stream ().collect (Collectors .joining ());
104
128
text .remove (cursorIndex );
129
+ String newText = text .stream ().collect (Collectors .joining ());
130
+ dispatchTextChange (oldText , newText );
105
131
}
106
132
}
107
133
@@ -124,4 +150,18 @@ private void done() {
124
150
dispatch (ShellMessageBuilder .ofView (this , ViewDoneEvent .of (this )));
125
151
}
126
152
153
+ public record InputViewTextChangeEventArgs (String oldText , String newText ) implements ViewEventArgs {
154
+
155
+ public static InputViewTextChangeEventArgs of (String oldText , String newText ) {
156
+ return new InputViewTextChangeEventArgs (oldText , newText );
157
+ }
158
+ }
159
+
160
+ public record InputViewTextChangeEvent (View view , InputViewTextChangeEventArgs args ) implements ViewEvent {
161
+
162
+ public static InputViewTextChangeEvent of (View view , String oldText , String newText ) {
163
+ return new InputViewTextChangeEvent (view , InputViewTextChangeEventArgs .of (oldText , newText ));
164
+ }
165
+ }
166
+
127
167
}
0 commit comments