Skip to content

Commit

Permalink
Merge pull request #24830 from avpinchuk/osgi-shell
Browse files Browse the repository at this point in the history
Improve OSGi interactive console
  • Loading branch information
avpinchuk authored Feb 28, 2024
2 parents 47b1081 + d5f0c7b commit 1e1def5
Showing 1 changed file with 18 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e1def5

Please sign in to comment.