Skip to content

Commit

Permalink
Add pager support into RubyLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Fedorov authored and Strech committed May 7, 2022
1 parent b5afdaf commit 2c64f43
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/launcher/java/org/truffleruby/launcher/RubyLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
package org.truffleruby.launcher;

import java.io.PrintStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.ProcessBuilder.Redirect;

import org.graalvm.launcher.AbstractLanguageLauncher;
import org.graalvm.nativeimage.ProcessProperties;
Expand Down Expand Up @@ -163,7 +165,7 @@ protected void collectArguments(Set<String> options) {

@Override
protected void printHelp(OptionCategory maxCategory) {
printHelp(System.out);
printHelp(getOutput());
}

@Override
Expand All @@ -173,6 +175,37 @@ protected AbortException abortUnrecognizedArgument(String argument) {
"truffleruby: invalid option " + argument + " (Use --help for usage instructions.)");
}

@Override
protected boolean runLauncherAction() {
if (System.console() == null) {
return super.runLauncherAction();
}

String pager = getPagerFromEnv();
if (pager == null || pager.length() == 0) {
return super.runLauncherAction();
}

try {
Process process = new ProcessBuilder(pager)
.redirectOutput(Redirect.INHERIT)
.redirectErrorStream(true)
.start();
PrintStream out = new PrintStream(process.getOutputStream());

setOutput(out);
boolean code = super.runLauncherAction();

out.flush();
out.close();
process.waitFor();

return code;
} catch (IOException | InterruptedException e) {
throw abort(e);
}
}

private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions config) {
if (config.executionAction == ExecutionAction.UNSET) {
switch (config.defaultExecutionAction) {
Expand Down Expand Up @@ -299,6 +332,20 @@ private static List<String> getPathListFromEnvVariable(String name) {
return Collections.emptyList();
}

private static String getPagerFromEnv() {
String pager = System.getenv("RUBY_PAGER");
if (pager != null) {
return pager.strip();
}

pager = System.getenv("PAGER");
if (pager != null) {
return pager.strip();
}

return null;
}

private void printPreRunInformation(CommandLineOptions config) {
if (config.showVersion) {
System.out.println(TruffleRuby.getVersionString(getImplementationNameFromEngine()));
Expand Down

0 comments on commit 2c64f43

Please sign in to comment.