From 8995588a7206ec986fa039b32afbec2ec3960962 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Tue, 30 Nov 2021 22:10:33 +0100 Subject: [PATCH] Add pager support into RubyLauncher --- .../truffleruby/launcher/RubyLauncher.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/launcher/java/org/truffleruby/launcher/RubyLauncher.java b/src/launcher/java/org/truffleruby/launcher/RubyLauncher.java index 981222e4e5ee..2af9231dbc3b 100644 --- a/src/launcher/java/org/truffleruby/launcher/RubyLauncher.java +++ b/src/launcher/java/org/truffleruby/launcher/RubyLauncher.java @@ -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; @@ -163,7 +165,7 @@ protected void collectArguments(Set options) { @Override protected void printHelp(OptionCategory maxCategory) { - printHelp(System.out); + printHelp(getOutput()); } @Override @@ -173,6 +175,46 @@ 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 = System.getenv("RUBY_PAGER"); + if (pager == null) { + pager = System.getenv("PAGER"); + + if (pager == null) { + return super.runLauncherAction(); + } + } + + pager = pager.strip(); + if (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) {