Skip to content

Commit

Permalink
Improve OSGi interactive console
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Feb 27, 2024
1 parent fffcd67 commit ca87e9e
Showing 1 changed file with 21 additions and 25 deletions.
@@ -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,8 +172,16 @@ 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();
LineReaderBuilder builder = LineReaderBuilder.builder().appName(REMOTE_COMMAND).terminal(terminal);
if (isInteractive()) {
builder.completer(getCommandCompleter());
}

LineReader reader = builder.build();
if (!isInteractive()) {
reader.unsetOpt(LineReader.Option.INSERT_TAB);
}

return executeCommands(reader);
} catch (IOException e) {
throw new CommandException(e);
Expand All @@ -195,29 +202,18 @@ 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);

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(),
encoding != null ? Charset.forName(encoding) : Charset.defaultCharset());
}

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

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


Expand Down Expand Up @@ -321,7 +317,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 +432,7 @@ private int stopSession(String sessionId) throws CommandException {
}


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

Expand Down

0 comments on commit ca87e9e

Please sign in to comment.