Skip to content

Capturing CTRL+D (EOF) to exit the shell #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.shell.jline;

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.UserInterruptException;
import org.jline.utils.AttributedString;
Expand Down Expand Up @@ -101,6 +102,9 @@ public Input readInput() {
return Input.EMPTY;
}
}
catch (EndOfFileException e) {
throw new ExitRequest(1);
}
return new ParsedLineInput(lineReader.getParsedLine());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.springframework.shell.jline;

import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Attributes;
import org.jline.terminal.impl.ExternalTerminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.junit.jupiter.api.Test;
import org.springframework.shell.ExitRequest;

import static org.assertj.core.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

public class InteractiveShellRunnerTests {

private PipedOutputStream outIn;
private InteractiveShellRunner.JLineInputProvider jLineInputProvider;


private PromptProvider dummyPromptProvider() {
return () -> new AttributedString("dummy-shell:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW));
}

private void initForShortcutKeyTest() throws Exception {
PipedInputStream in = new PipedInputStream();
outIn = new PipedOutputStream(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExternalTerminal terminal = new ExternalTerminal("foo", "ansi", in, out, StandardCharsets.UTF_8);
Attributes attributes = terminal.getAttributes();
attributes.setLocalFlag(Attributes.LocalFlag.ISIG, true);
attributes.setControlChar(Attributes.ControlChar.VINTR, 3);
terminal.setAttributes(attributes);
LineReaderBuilder builder =
LineReaderBuilder.builder()
.terminal(terminal);

LineReader lineReader = builder.build();
jLineInputProvider = new InteractiveShellRunner.JLineInputProvider(lineReader, dummyPromptProvider());
}

@Test
public void testClearWithCtrlC() throws Exception {

initForShortcutKeyTest();

CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch endLatch = new CountDownLatch(1);
Thread writeThread = new Thread(() -> {
try {
startLatch.await();
outIn.write('a');
outIn.write(3);
endLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
});
Thread readThread = new Thread(() -> {
assertThatNoException().isThrownBy(() -> assertThat(jLineInputProvider.readInput().rawText()).isEqualTo(""));
endLatch.countDown();
});
readThread.start();
startLatch.countDown();
writeThread.start();

readThread.join();
writeThread.join();
}


@Test
public void testExitWithCtrlC() throws Exception {

initForShortcutKeyTest();

CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch endLatch = new CountDownLatch(1);
Thread writeThread = new Thread(() -> {
try {
startLatch.await();
outIn.write(3);
endLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
});
Thread readThread = new Thread(() -> {
assertThatThrownBy(jLineInputProvider::readInput).isInstanceOf(ExitRequest.class);
endLatch.countDown();
});
readThread.start();
startLatch.countDown();
writeThread.start();

readThread.join();
writeThread.join();
}

@Test
public void testExitWithCtrlD() throws Exception {

initForShortcutKeyTest();

CountDownLatch startLatch = new CountDownLatch(1);
CountDownLatch endLatch = new CountDownLatch(1);
Thread writeThread = new Thread(() -> {
try {
startLatch.await();
outIn.write(4);
endLatch.await();
} catch (Exception e) {
e.printStackTrace();
}
});
Thread readThread = new Thread(() -> {
assertThatThrownBy(jLineInputProvider::readInput).isInstanceOf(ExitRequest.class);
endLatch.countDown();
});
readThread.start();
startLatch.countDown();
writeThread.start();

readThread.join();
writeThread.join();
}
}