Update docs

This commit is contained in:
Janne Valkealahti
2023-10-06 13:40:48 +01:00
parent 6dd038830f
commit 89a829cfef
33 changed files with 479 additions and 103 deletions

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.docs;
import org.springframework.shell.component.view.control.BoxView;
class BoxViewSnippets {
class Dump1 {
void dump1() {
// tag::sample[]
BoxView view = new BoxView();
view.setDrawFunction((screen, rect) -> {
screen.writerBuilder().build()
.text("hi", 0, 0);
return rect;
});
// end::sample[]
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.docs;
import org.springframework.shell.component.view.control.BoxView;
import org.springframework.shell.component.view.control.View;
import org.springframework.shell.samples.catalog.scenario.AbstractScenario;
import org.springframework.shell.samples.catalog.scenario.Scenario;
import org.springframework.shell.samples.catalog.scenario.ScenarioComponent;
class CatalogSnippets {
// tag::samplescenario[]
@ScenarioComponent(
name = "Scenario name",
description = "Scenario short description",
category = { Scenario.CATEGORY_OTHER })
public class SampleScenario extends AbstractScenario {
@Override
public View build() {
return new BoxView();
}
}
// end::samplescenario[]
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.docs;
import org.jline.terminal.Terminal;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.shell.component.view.TerminalUI;
import org.springframework.shell.component.view.event.EventLoop;
class EventLoopSnippets {
class Dump1 {
@Autowired
Terminal terminal;
void events() {
// tag::plainevents[]
TerminalUI ui = new TerminalUI(terminal);
EventLoop eventLoop = ui.getEventLoop();
Flux<? extends Message<?>> events = eventLoop.events();
events.subscribe();
// end::plainevents[]
}
void keyEvents() {
// tag::keyevents[]
TerminalUI ui = new TerminalUI(terminal);
EventLoop eventLoop = ui.getEventLoop();
eventLoop.keyEvents()
.doOnNext(event -> {
// do something with key event
})
.subscribe();
// end::keyevents[]
}
void onDestroy() {
// tag::ondestroy[]
TerminalUI ui = new TerminalUI(terminal);
EventLoop eventLoop = ui.getEventLoop();
eventLoop.onDestroy(eventLoop.events().subscribe());
// end::ondestroy[]
}
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.docs;
import java.util.List;
import org.springframework.shell.component.view.control.ListView;
import org.springframework.shell.component.view.control.ListView.ItemStyle;
import org.springframework.shell.component.view.control.cell.AbstractListCell;
import org.springframework.shell.component.view.geom.Rectangle;
import org.springframework.shell.component.view.screen.Screen;
import org.springframework.shell.component.view.screen.Screen.Writer;
public class ListViewSnippets {
class Dump1 {
void dump1() {
// tag::snippet1[]
ListView<String> view = new ListView<>();
view.setItems(List.of("item1", "item2"));
// end::snippet1[]
}
@SuppressWarnings("unused")
void dump2() {
// tag::snippet2[]
ListView<String> view = new ListView<>(ItemStyle.CHECKED);
// end::snippet2[]
}
}
// tag::listcell[]
record ExampleData(String name) {
};
static class ExampleListCell extends AbstractListCell<ExampleData> {
public ExampleListCell(ExampleData item) {
super(item);
}
@Override
public void draw(Screen screen) {
Rectangle rect = getRect();
Writer writer = screen.writerBuilder().style(getStyle()).build();
writer.text(getItem().name(), rect.x(), rect.y());
writer.background(rect, getBackgroundColor());
}
}
// end::listcell[]
class Dump2 {
void dump1() {
// tag::uselistcell[]
ListView<ExampleData> view = new ListView<>();
view.setCellFactory((list, item) -> new ExampleListCell(item));
// end::uselistcell[]
}
}
}

View File

@@ -25,13 +25,13 @@ import org.springframework.shell.component.view.geom.VerticalAlign;
class TerminalUiSnippets {
class Sample {
class Sample1 {
// tag::snippet1[]
@Autowired
Terminal terminal;
void build() {
void sample() {
TerminalUI ui = new TerminalUI(terminal);
BoxView view = new BoxView();
view.setDrawFunction((screen, rect) -> {
@@ -46,4 +46,25 @@ class TerminalUiSnippets {
// end::snippet1[]
}
class Sample2 {
// tag::snippet2[]
@Autowired
Terminal terminal;
void sample() {
TerminalUI ui = new TerminalUI(terminal);
BoxView view = new BoxView();
view.setDrawFunction((screen, rect) -> {
screen.writerBuilder()
.build()
.text("Hello World", rect, HorizontalAlign.CENTER, VerticalAlign.CENTER);
return rect;
});
ui.setRoot(view, false);
ui.run();
}
// end::snippet2[]
}
}