23
23
import org .springframework .messaging .support .MessageBuilder ;
24
24
import org .springframework .shell .command .annotation .Command ;
25
25
import org .springframework .shell .component .ViewComponent ;
26
+ import org .springframework .shell .component .ViewComponent .ViewComponentRun ;
26
27
import org .springframework .shell .component .message .ShellMessageBuilder ;
27
28
import org .springframework .shell .component .message .ShellMessageHeaderAccessor ;
28
29
import org .springframework .shell .component .message .StaticShellMessageHeaderAccessor ;
@@ -90,14 +91,15 @@ public void tui3() {
90
91
public String stringInput () {
91
92
InputView view = new InputView ();
92
93
view .setRect (0 , 0 , 10 , 1 );
93
- ViewComponent component = new ViewComponent ( getTerminal (), view );
94
- component .run ();
94
+ ViewComponent component = getViewComponentBuilder (). build ( view );
95
+ component .runBlocking ();
95
96
String input = view .getInputText ();
96
97
return String .format ("Input was '%s'" , input );
97
98
}
98
99
99
100
private void runProgress (ProgressView view ) {
100
- ViewComponent component = new ViewComponent (getTerminal (), view );
101
+ ViewComponent component = getViewComponentBuilder ().build (view );
102
+
101
103
EventLoop eventLoop = component .getEventLoop ();
102
104
103
105
Flux <Message <?>> ticks = Flux .interval (Duration .ofMillis (100 )).map (l -> {
@@ -118,7 +120,7 @@ private void runProgress(ProgressView view) {
118
120
}
119
121
}));
120
122
121
- component .run ();
123
+ component .runAsync (). await ();
122
124
}
123
125
124
126
@ Command (command = "componentui progress1" )
@@ -166,4 +168,60 @@ public void progress4() {
166
168
runProgress (view );
167
169
}
168
170
171
+ @ Command (command = "componentui progress5" )
172
+ public void progress5 () {
173
+ ProgressView view = new ProgressView (0 , 100 ,
174
+ ProgressViewItem .ofText (10 , HorizontalAlign .LEFT ),
175
+ ProgressViewItem .ofSpinner (3 , HorizontalAlign .LEFT ),
176
+ ProgressViewItem .ofPercent (0 , HorizontalAlign .RIGHT ));
177
+
178
+ view .setDescription ("name" );
179
+ view .setRect (0 , 0 , 20 , 1 );
180
+ view .start ();
181
+
182
+ ViewComponent component = getViewComponentBuilder ().build (view );
183
+ component .setUseTerminalWidth (false );
184
+
185
+ Flux <Message <?>> ticks = Flux .interval (Duration .ofMillis (100 )).map (l -> {
186
+ Message <Long > message = MessageBuilder
187
+ .withPayload (l )
188
+ .setHeader (ShellMessageHeaderAccessor .EVENT_TYPE , EventLoop .Type .USER )
189
+ .build ();
190
+ return message ;
191
+ });
192
+ EventLoop eventLoop = component .getEventLoop ();
193
+ eventLoop .dispatch (ticks );
194
+ eventLoop .onDestroy (eventLoop .events ()
195
+ .filter (m -> EventLoop .Type .USER .equals (StaticShellMessageHeaderAccessor .getEventType (m )))
196
+ .subscribe (m -> {
197
+ if (m .getPayload () instanceof Long ) {
198
+ view .tickAdvance (5 );
199
+ eventLoop .dispatch (ShellMessageBuilder .ofRedraw ());
200
+ }
201
+ }));
202
+
203
+ ViewComponentRun run = component .runAsync ();
204
+
205
+ for (int i = 0 ; i < 4 ; i ++) {
206
+
207
+ if (run .isDone ()) {
208
+ break ;
209
+ }
210
+ try {
211
+ Thread .sleep (2000 );
212
+ } catch (InterruptedException e ) {
213
+ }
214
+ if (run .isDone ()) {
215
+ break ;
216
+ }
217
+
218
+ String msg = String .format ("%s " , i );
219
+ getTerminal ().writer ().write (msg + System .lineSeparator ());
220
+ getTerminal ().writer ().flush ();
221
+
222
+ }
223
+
224
+ run .cancel ();
225
+ }
226
+
169
227
}
0 commit comments