Skip to content

Commit 2786b01

Browse files
committed
StringInput better unicode handling
- Add unicode handling in flow components in AbstractTextComponent which allows StringInput to process unicode given from jline. - This add basic non-english character support for input. - Fixes #1115
1 parent 9021238 commit 2786b01

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/StringInput.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 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.
@@ -94,6 +94,7 @@ protected boolean read(BindingReader bindingReader, KeyMap<String> keyMap, Strin
9494
}
9595
String input;
9696
switch (operation) {
97+
case OPERATION_UNICODE:
9798
case OPERATION_CHAR:
9899
String lastBinding = bindingReader.getLastBinding();
99100
input = context.getInput();

spring-shell-core/src/main/java/org/springframework/shell/component/support/AbstractComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 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.
@@ -60,6 +60,7 @@ public abstract class AbstractComponent<T extends ComponentContext<T>> implement
6060
public final static String OPERATION_EXIT = "EXIT";
6161
public final static String OPERATION_BACKSPACE = "BACKSPACE";
6262
public final static String OPERATION_CHAR = "CHAR";
63+
public final static String OPERATION_UNICODE = "UNICODE";
6364
public final static String OPERATION_SELECT = "SELECT";
6465
public final static String OPERATION_DOWN = "DOWN";
6566
public final static String OPERATION_UP = "UP";

spring-shell-core/src/main/java/org/springframework/shell/component/support/AbstractTextComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 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.
@@ -62,6 +62,7 @@ protected void bindKeyMap(KeyMap<String> keyMap) {
6262
for (char i = 32; i < KeyMap.KEYMAP_LENGTH - 1; i++) {
6363
keyMap.bind(OPERATION_CHAR, Character.toString(i));
6464
}
65+
keyMap.setUnicode(OPERATION_UNICODE);
6566
}
6667

6768
@Override

spring-shell-core/src/test/java/org/springframework/shell/component/StringInputTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 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.
@@ -166,6 +166,29 @@ public void testResultUserInput() throws InterruptedException {
166166
assertThat(run1Context.getResultValue()).isEqualTo("test");
167167
}
168168

169+
@Test
170+
public void testResultUserInputUnicode() throws InterruptedException {
171+
ComponentContext<?> empty = ComponentContext.empty();
172+
StringInput component1 = new StringInput(getTerminal(), "component1", "component1ResultValue");
173+
component1.setResourceLoader(new DefaultResourceLoader());
174+
component1.setTemplateExecutor(getTemplateExecutor());
175+
176+
service.execute(() -> {
177+
StringInputContext run1Context = component1.run(empty);
178+
result1.set(run1Context);
179+
latch1.countDown();
180+
});
181+
182+
TestBuffer testBuffer = new TestBuffer().append("😂").cr();
183+
write(testBuffer.getBytes());
184+
185+
latch1.await(2, TimeUnit.SECONDS);
186+
StringInputContext run1Context = result1.get();
187+
188+
assertThat(run1Context).isNotNull();
189+
assertThat(run1Context.getResultValue()).isEqualTo("😂");
190+
}
191+
169192
@Test
170193
public void testPassingViaContext() throws InterruptedException {
171194
ComponentContext<?> empty = ComponentContext.empty();

0 commit comments

Comments
 (0)