Make ViewComponent api better for expected use

- Exposing eventloop if user need to interact with it
- Add api/feature to set used view rect expand to
  full terminal width
- Add plain ProgressView samples
- Relates #995
- Fixes #997
This commit is contained in:
Janne Valkealahti
2024-02-02 09:32:07 +00:00
parent 10d9940299
commit 9446afecb7
2 changed files with 115 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,11 +15,23 @@
*/
package org.springframework.shell.samples.standard;
import java.time.Duration;
import reactor.core.publisher.Flux;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.shell.command.annotation.Command;
import org.springframework.shell.component.ViewComponent;
import org.springframework.shell.component.message.ShellMessageBuilder;
import org.springframework.shell.component.message.ShellMessageHeaderAccessor;
import org.springframework.shell.component.message.StaticShellMessageHeaderAccessor;
import org.springframework.shell.component.view.TerminalUI;
import org.springframework.shell.component.view.control.BoxView;
import org.springframework.shell.component.view.control.InputView;
import org.springframework.shell.component.view.control.ProgressView;
import org.springframework.shell.component.view.control.ProgressView.ProgressViewItem;
import org.springframework.shell.component.view.event.EventLoop;
import org.springframework.shell.geom.HorizontalAlign;
import org.springframework.shell.geom.VerticalAlign;
import org.springframework.shell.standard.AbstractShellComponent;
@@ -83,4 +95,70 @@ public class ComponentUiCommands extends AbstractShellComponent {
return String.format("Input was '%s'", input);
}
@Command(command = "componentui progress1")
public void progress1() {
ProgressView view = new ProgressView();
view.setDescription("name");
view.setRect(0, 0, 20, 1);
ViewComponent component = new ViewComponent(getTerminal(), view);
EventLoop eventLoop = component.getEventLoop();
Flux<Message<?>> ticks = Flux.interval(Duration.ofMillis(100)).map(l -> {
Message<Long> message = MessageBuilder
.withPayload(l)
.setHeader(ShellMessageHeaderAccessor.EVENT_TYPE, EventLoop.Type.USER)
.build();
return message;
});
eventLoop.dispatch(ticks);
eventLoop.onDestroy(eventLoop.events()
.filter(m -> EventLoop.Type.USER.equals(StaticShellMessageHeaderAccessor.getEventType(m)))
.subscribe(m -> {
if (m.getPayload() instanceof Long) {
view.tickAdvance(5);
eventLoop.dispatch(ShellMessageBuilder.ofRedraw());
}
}));
component.run();
}
@Command(command = "componentui progress2")
public void progress2() {
ProgressView view = new ProgressView(0, 100, ProgressViewItem.ofText(10, HorizontalAlign.LEFT),
ProgressViewItem.ofSpinner(3, HorizontalAlign.LEFT),
ProgressViewItem.ofPercent(0, HorizontalAlign.RIGHT));
view.setDescription("name");
view.setRect(0, 0, 20, 1);
ViewComponent component = new ViewComponent(getTerminal(), view);
EventLoop eventLoop = component.getEventLoop();
Flux<Message<?>> ticks = Flux.interval(Duration.ofMillis(100)).map(l -> {
Message<Long> message = MessageBuilder
.withPayload(l)
.setHeader(ShellMessageHeaderAccessor.EVENT_TYPE, EventLoop.Type.USER)
.build();
return message;
});
eventLoop.dispatch(ticks);
eventLoop.onDestroy(eventLoop.events()
.filter(m -> EventLoop.Type.USER.equals(StaticShellMessageHeaderAccessor.getEventType(m)))
.subscribe(m -> {
if (m.getPayload() instanceof Long) {
view.tickAdvance(5);
eventLoop.dispatch(ShellMessageBuilder.ofRedraw());
}
}));
component.run();
}
}