TerminalUI

TerminalUI is a main implementation to drive ui execution logic.spring-doc.cn

Create TerminalUI

You can build TerminalUI manually but recommended way is to use TerminalUIBuilder build is autoconfigured for you and will set needed services.spring-doc.cn

@Autowired
TerminalUIBuilder builder;

void sample() {
	TerminalUI ui = builder.build();
	// do something with ui
}

Configuring Views

TerminalUI has a helper method configure(View) which can be used to set needed integrations into eventloop and other services.spring-doc.cn

TerminalUI ui;

void sample() {
	BoxView view = new BoxView();
	ui.configure(view);
}

Running UI Loop

Running TerminalUI execution loop is a blocking operation. You’re going to need a way to exit from a loop, for example Exiting App.spring-doc.cn

TerminalUI ui;

void sample() {
	ui.run();
}

Exiting App

If you want to exit from an app using normal CTRL-Q key combination listen events and request interrupt.spring-doc.cn

@Autowired
Terminal terminal;

void sample() {
	TerminalUI ui = new TerminalUI(terminal);
	BoxView view = new BoxView();
	ui.configure(view);
	ui.setRoot(view, true);
	EventLoop eventLoop = ui.getEventLoop();
	eventLoop.keyEvents()
		.subscribe(event -> {
			if (event.getPlainKey() == Key.q && event.hasCtrl()) {
				eventLoop.dispatch(ShellMessageBuilder.ofInterrupt());
			}
		});
	ui.run();
}

Modal View

TerminalUI supports having one active modal view. Modal view is placed on top of all other views and takes all input events.spring-doc.cn

TerminalUI ui;

void sample() {
	DialogView dialog = new DialogView();
	// set modal
	ui.setModal(dialog);
	// clear modal
	ui.setModal(null);
}
As views should not directly know anything about TerminalUi and interface ViewService exposes modal related functions.