Skip to content

Commit ce01bac

Browse files
author
Sergey Fedorov
committed
Add pager support into RubyLauncher
1 parent e2f62b8 commit ce01bac

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/launcher/java/org/truffleruby/launcher/RubyLauncher.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
package org.truffleruby.launcher;
1111

1212
import java.io.PrintStream;
13+
import java.io.IOException;
1314
import java.util.ArrayList;
1415
import java.util.Arrays;
1516
import java.util.Collections;
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Set;
20+
import java.lang.ProcessBuilder.Redirect;
1921

2022
import org.graalvm.launcher.AbstractLanguageLauncher;
2123
import org.graalvm.nativeimage.ProcessProperties;
@@ -33,6 +35,7 @@ public class RubyLauncher extends AbstractLanguageLauncher {
3335

3436
private CommandLineOptions config;
3537
private String implementationName = null;
38+
private boolean helpOptionUsed;
3639

3740
public static void main(String[] args) {
3841
new RubyLauncher().launch(args);
@@ -163,7 +166,7 @@ protected void collectArguments(Set<String> options) {
163166

164167
@Override
165168
protected void printHelp(OptionCategory maxCategory) {
166-
printHelp(System.out);
169+
printHelp(getOutput());
167170
}
168171

169172
@Override
@@ -173,6 +176,47 @@ protected AbortException abortUnrecognizedArgument(String argument) {
173176
"truffleruby: invalid option " + argument + " (Use --help for usage instructions.)");
174177
}
175178

179+
@Override
180+
protected boolean parseCommonOption(String defaultOptionPrefix, Map<String, String> polyglotOptions,
181+
boolean experimentalOptions, String arg) {
182+
if (arg.startsWith("--help")) {
183+
helpOptionUsed = true;
184+
}
185+
186+
return super.parseCommonOption(defaultOptionPrefix, polyglotOptions, experimentalOptions, arg);
187+
}
188+
189+
@Override
190+
protected boolean runLauncherAction() {
191+
if (!helpOptionUsed || System.console() == null) {
192+
return super.runLauncherAction();
193+
}
194+
195+
String pager = getPagerFromEnv();
196+
if (pager == null || pager.length() == 0) {
197+
return super.runLauncherAction();
198+
}
199+
200+
try {
201+
Process process = new ProcessBuilder(pager)
202+
.redirectOutput(Redirect.INHERIT)
203+
.redirectErrorStream(true)
204+
.start();
205+
PrintStream out = new PrintStream(process.getOutputStream());
206+
207+
setOutput(out);
208+
boolean code = super.runLauncherAction();
209+
210+
out.flush();
211+
out.close();
212+
process.waitFor();
213+
214+
return code;
215+
} catch (IOException | InterruptedException e) {
216+
throw abort(e);
217+
}
218+
}
219+
176220
private int runRubyMain(Context.Builder contextBuilder, CommandLineOptions config) {
177221
if (config.executionAction == ExecutionAction.UNSET) {
178222
switch (config.defaultExecutionAction) {
@@ -299,6 +343,20 @@ private static List<String> getPathListFromEnvVariable(String name) {
299343
return Collections.emptyList();
300344
}
301345

346+
private static String getPagerFromEnv() {
347+
String pager = System.getenv("RUBY_PAGER");
348+
if (pager != null) {
349+
return pager.strip();
350+
}
351+
352+
pager = System.getenv("PAGER");
353+
if (pager != null) {
354+
return pager.strip();
355+
}
356+
357+
return null;
358+
}
359+
302360
private void printPreRunInformation(CommandLineOptions config) {
303361
if (config.showVersion) {
304362
System.out.println(TruffleRuby.getVersionString(getImplementationNameFromEngine()));

0 commit comments

Comments
 (0)