Skip to content

Commit 83e906d

Browse files
committed
Document writing to console
- Relates #579
1 parent e5570ae commit 83e906d

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== Writing
2+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
3+
4+
When something needs to get written into your console you can always
5+
use JDK's `System.out` which then goes directly into JDK's own streams.
6+
Other recommended way is to use JLine's `Terminal` and get _writer_
7+
instance from there.
8+
9+
If using target enpoints, i.e. _consumer_ which is not expected
10+
to return anything given `CommandContext` contains reference to
11+
`Terminal` and writer can be accessed from there.
12+
13+
====
14+
[source, java, indent=0]
15+
----
16+
include::{snippets}/WritingSnippets.java[tag=reg-terminal-writer]
17+
----
18+
====
19+
20+
It's possible to autowire `Terminal` to get access to its writer.
21+
22+
====
23+
[source, java, indent=0]
24+
----
25+
include::{snippets}/WritingSnippets.java[tag=anno-terminal-writer]
26+
----
27+
====

spring-shell-docs/src/main/asciidoc/using-shell-commands.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ include::using-shell-commands-helpoptions.adoc[]
2020
include::using-shell-commands-interactionmode.adoc[]
2121

2222
include::using-shell-commands-builtin.adoc[]
23+
24+
include::using-shell-commands-writing.adoc[]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.docs;
17+
18+
import org.jline.terminal.Terminal;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.shell.command.CommandRegistration;
22+
import org.springframework.shell.standard.ShellMethod;
23+
24+
class WritingSnippets {
25+
26+
class Dump1 {
27+
28+
// tag::anno-terminal-writer[]
29+
@Autowired
30+
Terminal terminal;
31+
32+
@ShellMethod
33+
public void example() {
34+
terminal.writer().println("hi");
35+
terminal.writer().flush();
36+
}
37+
// end::anno-terminal-writer[]
38+
}
39+
40+
void dump1() {
41+
// tag::reg-terminal-writer[]
42+
CommandRegistration.builder()
43+
.command("example")
44+
.withTarget()
45+
.consumer(ctx -> {
46+
ctx.getTerminal().writer().println("hi");
47+
ctx.getTerminal().writer().flush();
48+
})
49+
.and()
50+
.build();
51+
// end::reg-terminal-writer[]
52+
}
53+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.samples.e2e;
17+
18+
import java.util.function.Supplier;
19+
20+
import org.jline.terminal.Terminal;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.shell.command.CommandRegistration;
25+
import org.springframework.shell.standard.ShellComponent;
26+
import org.springframework.shell.standard.ShellMethod;
27+
28+
@ShellComponent
29+
public class WriteCommands extends BaseE2ECommands {
30+
31+
@Autowired
32+
Terminal terminal;
33+
34+
@Bean
35+
public CommandRegistration writeTerminalWriterRegistration(Supplier<CommandRegistration.Builder> builder) {
36+
return builder.get()
37+
.command(REG, "write-terminalwriter")
38+
.group(GROUP)
39+
.withTarget()
40+
.consumer(ctx -> {
41+
ctx.getTerminal().writer().println("hi");
42+
ctx.getTerminal().writer().flush();
43+
})
44+
.and()
45+
.build();
46+
}
47+
48+
@ShellMethod(key = LEGACY_ANNO + "write-terminalwriter", group = GROUP)
49+
public void writeTerminalWriterAnnotation() {
50+
terminal.writer().println("hi");
51+
terminal.writer().flush();
52+
}
53+
54+
@Bean
55+
public CommandRegistration writeSystemOutRegistration(Supplier<CommandRegistration.Builder> builder) {
56+
return builder.get()
57+
.command(REG, "write-terminalwriter")
58+
.group(GROUP)
59+
.withTarget()
60+
.consumer(ctx -> {
61+
System.out.println("hi");
62+
})
63+
.and()
64+
.build();
65+
}
66+
67+
@ShellMethod(key = LEGACY_ANNO + "write-systemout", group = GROUP)
68+
public void writeSystemOutAnnotation() {
69+
System.out.println("hi");
70+
}
71+
}

0 commit comments

Comments
 (0)