Skip to content
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

Improve OSGi interactive console #24830

Merged
merged 2 commits into from Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,11 +31,9 @@
import jakarta.inject.Inject;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -64,6 +62,7 @@
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.impl.ExternalTerminal;
import org.jvnet.hk2.annotations.Service;

import static org.glassfish.hk2.utilities.BuilderHelper.createConstantDescriptor;
Expand Down Expand Up @@ -173,9 +172,12 @@ protected int executeCommand() throws CommandException {
logger.log(Level.FINEST, "executeCommand: args {0}", Arrays.toString(args));
shellType = cmd.executeAndReturnOutput(args).trim();
try (Terminal terminal = createTerminal()) {
LineReader reader = LineReaderBuilder.builder().completer(getCommandCompleter()).appName(REMOTE_COMMAND)
.terminal(terminal).build();
return executeCommands(reader);
LineReaderBuilder builder = LineReaderBuilder.builder().appName(REMOTE_COMMAND).terminal(terminal);
if (isInteractive()) {
builder.completer(getCommandCompleter());
builder.option(LineReader.Option.INSERT_TAB, false);
}
return executeCommands(builder.build());
} catch (IOException e) {
throw new CommandException(e);
}
Expand All @@ -195,29 +197,20 @@ private String[] enhanceForTarget(String[] args) {


private Terminal createTerminal() throws IOException, CommandException {
InputStream inputStream;
OutputStream outputStream;
if (file != null) {
if (!isInteractive()) {
if (!file.canRead()) {
throw new CommandException("File: " + file + " can not be read");
}
inputStream = new FileInputStream(file);
outputStream = new EmptyOutputStream();
} else {
System.out.println(STRINGS.get("multimodeIntro"));
inputStream = new FileInputStream(FileDescriptor.in);
outputStream = System.out;
}

TerminalBuilder builder = TerminalBuilder.builder().streams(inputStream, outputStream);
Charset charset = encoding == null ? Charset.defaultCharset() : Charset.forName(encoding);

if (System.getenv("TERM") == null) {
Terminal terminal = builder.type("dumb").build();
terminal.echo(false);
return terminal;
return new ExternalTerminal(REMOTE_COMMAND, "dumb",
new FileInputStream(file), new EmptyOutputStream(), charset);
}

return builder.build();
System.out.println(STRINGS.get("multimodeIntro"));

return TerminalBuilder.builder().system(true).build();
}


Expand Down Expand Up @@ -321,7 +314,7 @@ private int executeCommands(LineReader reader) throws CommandException {
try {
while (true) {
try {
if (isPromptPrinted()) {
if (isInteractive()) {
line = reader.readLine(shellType + "$ ");
} else {
line = reader.readLine();
Expand Down Expand Up @@ -436,7 +429,7 @@ private int stopSession(String sessionId) throws CommandException {
}


private boolean isPromptPrinted() {
private boolean isInteractive() {
return file == null;
}

Expand Down