Skip to content

Commit e993c3a

Browse files
committed
Add default ShellWriteSequence
1 parent c273fd5 commit e993c3a

File tree

4 files changed

+210
-99
lines changed

4 files changed

+210
-99
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2025-present 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.test;
17+
18+
/**
19+
* Default implementation of {@link ShellWriteSequence}.
20+
*
21+
* @author Janne Valkealahti
22+
* @author Mahmoud Ben Hassine
23+
*/
24+
class DefaultShellWriteSequence implements ShellWriteSequence {
25+
26+
private final StringBuilder buf = new StringBuilder();
27+
28+
@Override
29+
public ShellWriteSequence carriageReturn() {
30+
this.buf.append("\r");
31+
return this;
32+
}
33+
34+
@Override
35+
public ShellWriteSequence clearScreen() {
36+
this.buf.append("\033[H\033[2J");
37+
return this;
38+
}
39+
40+
@Override
41+
public ShellWriteSequence ctrl(char c) {
42+
this.buf.append((char) (c & 0x1f));
43+
return this;
44+
}
45+
46+
@Override
47+
public ShellWriteSequence command(String command) {
48+
this.text(command);
49+
return carriageReturn();
50+
}
51+
52+
@Override
53+
public ShellWriteSequence cr() {
54+
return carriageReturn();
55+
}
56+
57+
@Override
58+
public ShellWriteSequence keyUp() {
59+
this.buf.append("\033[A");
60+
return this;
61+
}
62+
63+
@Override
64+
public ShellWriteSequence keyDown() {
65+
this.buf.append("\033[B");
66+
return this;
67+
}
68+
69+
@Override
70+
public ShellWriteSequence keyLeft() {
71+
this.buf.append("\033[D");
72+
return this;
73+
}
74+
75+
@Override
76+
public ShellWriteSequence keyRight() {
77+
this.buf.append("\033[C");
78+
return this;
79+
}
80+
81+
@Override
82+
public ShellWriteSequence text(String text) {
83+
this.buf.append(text);
84+
return this;
85+
}
86+
87+
@Override
88+
public ShellWriteSequence space() {
89+
return this.text(" ");
90+
}
91+
92+
@Override
93+
public String build() {
94+
return buf.toString();
95+
}
96+
97+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2025-present 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.test;
17+
18+
import org.jline.keymap.KeyMap;
19+
import org.jline.terminal.Terminal;
20+
import org.jline.utils.InfoCmp;
21+
22+
/**
23+
* JLine based implementation of {@link ShellWriteSequence}.
24+
*
25+
* @author Janne Valkealahti
26+
* @author Mahmoud Ben Hassine
27+
*/
28+
class JLineShellWriteSequence implements ShellWriteSequence {
29+
30+
private final Terminal terminal;
31+
32+
private final StringBuilder buf = new StringBuilder();
33+
34+
JLineShellWriteSequence(Terminal terminal) {
35+
this.terminal = terminal;
36+
}
37+
38+
@Override
39+
public ShellWriteSequence carriageReturn() {
40+
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.carriage_return));
41+
return this;
42+
}
43+
44+
@Override
45+
public ShellWriteSequence clearScreen() {
46+
String ansiClearScreen = KeyMap.key(this.terminal, InfoCmp.Capability.clear_screen);
47+
this.buf.append(ansiClearScreen);
48+
return this;
49+
}
50+
51+
@Override
52+
public ShellWriteSequence ctrl(char c) {
53+
String ctrl = KeyMap.ctrl(c);
54+
this.buf.append(ctrl);
55+
return this;
56+
}
57+
58+
@Override
59+
public ShellWriteSequence command(String command) {
60+
this.text(command);
61+
return carriageReturn();
62+
}
63+
64+
@Override
65+
public ShellWriteSequence cr() {
66+
return carriageReturn();
67+
}
68+
69+
@Override
70+
public ShellWriteSequence keyUp() {
71+
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_up));
72+
return this;
73+
}
74+
75+
@Override
76+
public ShellWriteSequence keyDown() {
77+
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_down));
78+
return this;
79+
}
80+
81+
@Override
82+
public ShellWriteSequence keyLeft() {
83+
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_left));
84+
return this;
85+
}
86+
87+
@Override
88+
public ShellWriteSequence keyRight() {
89+
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_right));
90+
return this;
91+
}
92+
93+
@Override
94+
public ShellWriteSequence text(String text) {
95+
this.buf.append(text);
96+
return this;
97+
}
98+
99+
@Override
100+
public ShellWriteSequence space() {
101+
return this.text(" ");
102+
}
103+
104+
@Override
105+
public String build() {
106+
return buf.toString();
107+
}
108+
109+
}

spring-shell-test/src/main/java/org/springframework/shell/test/ShellTestClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public DefaultInteractiveShellSession(PromptProvider promptProvider, LineReader
241241

242242
@Override
243243
public ShellWriteSequence writeSequence() {
244-
return ShellWriteSequence.of(terminal);
244+
return new JLineShellWriteSequence(terminal);
245245
}
246246

247247
@Override
@@ -297,7 +297,7 @@ public DefaultNonInteractiveShellSession(Parser parser, String[] args,
297297

298298
@Override
299299
public ShellWriteSequence writeSequence() {
300-
return ShellWriteSequence.of(terminal);
300+
return new JLineShellWriteSequence(terminal);
301301
}
302302

303303
@Override
Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,11 @@
1515
*/
1616
package org.springframework.shell.test;
1717

18-
import org.jline.keymap.KeyMap;
19-
import org.jline.terminal.Terminal;
20-
import org.jline.utils.InfoCmp;
21-
2218
/**
2319
* Interface sequencing various things into terminal aware text types.
2420
*
2521
* @author Janne Valkealahti
22+
* @author Mahmoud Ben Hassine
2623
*/
2724
public interface ShellWriteSequence {
2825

@@ -101,96 +98,4 @@ public interface ShellWriteSequence {
10198
*/
10299
String build();
103100

104-
/**
105-
* Get a new instance of a {@code ShellWriteSequence}.
106-
* @param terminal the terminal
107-
* @return instance of a write sequence
108-
*/
109-
static ShellWriteSequence of(Terminal terminal) {
110-
return new DefaultShellWriteSequence(terminal);
111-
}
112-
113-
static class DefaultShellWriteSequence implements ShellWriteSequence {
114-
115-
private final Terminal terminal;
116-
117-
private StringBuilder buf = new StringBuilder();
118-
119-
DefaultShellWriteSequence(Terminal terminal) {
120-
this.terminal = terminal;
121-
}
122-
123-
@Override
124-
public ShellWriteSequence carriageReturn() {
125-
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.carriage_return));
126-
return this;
127-
}
128-
129-
@Override
130-
public ShellWriteSequence clearScreen() {
131-
String ansiClearScreen = KeyMap.key(this.terminal, InfoCmp.Capability.clear_screen);
132-
this.buf.append(ansiClearScreen);
133-
return this;
134-
}
135-
136-
@Override
137-
public ShellWriteSequence ctrl(char c) {
138-
String ctrl = KeyMap.ctrl(c);
139-
this.buf.append(ctrl);
140-
return this;
141-
}
142-
143-
@Override
144-
public ShellWriteSequence command(String command) {
145-
this.text(command);
146-
return carriageReturn();
147-
}
148-
149-
@Override
150-
public ShellWriteSequence cr() {
151-
return carriageReturn();
152-
}
153-
154-
@Override
155-
public ShellWriteSequence keyUp() {
156-
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_up));
157-
return this;
158-
}
159-
160-
@Override
161-
public ShellWriteSequence keyDown() {
162-
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_down));
163-
return this;
164-
}
165-
166-
@Override
167-
public ShellWriteSequence keyLeft() {
168-
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_left));
169-
return this;
170-
}
171-
172-
@Override
173-
public ShellWriteSequence keyRight() {
174-
this.buf.append(KeyMap.key(this.terminal, InfoCmp.Capability.key_right));
175-
return this;
176-
}
177-
178-
@Override
179-
public ShellWriteSequence text(String text) {
180-
this.buf.append(text);
181-
return this;
182-
}
183-
184-
@Override
185-
public ShellWriteSequence space() {
186-
return this.text(" ");
187-
}
188-
189-
@Override
190-
public String build() {
191-
return buf.toString();
192-
}
193-
194-
}
195-
196101
}

0 commit comments

Comments
 (0)