
ste.bshell.commands.help Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bsh-console Show documentation
Show all versions of bsh-console Show documentation
A BeanShell console with history, autocompletion and more...
The newest version!
/*
* Copyright (C) 2018 Stefano Fornari.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from Stefano Fornari. This notice must be
* included on all copies, modifications and derivatives of this
* work.
*
* STEFANO FORNARI MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
* OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. STEFANO FORNARI SHALL NOT BE LIABLE FOR ANY
* DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
package ste.bshell.commands;
import bsh.CallStack;
import bsh.EvalError;
import bsh.Interpreter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
/**
* TODO: commands should be alphabetically ordered
*/
public class help {
public static final String HELP = "bshell.help";
public static void invoke(final Interpreter bsh, final CallStack callstack)
throws EvalError {
invoke(bsh, callstack, null);
}
public static void invoke(final Interpreter bsh, final CallStack callstack, final String name)
throws EvalError {
if (bsh.get(HELP) == null) {
throw new EvalError("please set bsh.help to an existing and readable directory", null, callstack);
}
final String help = String.valueOf(bsh.get(HELP));
final Path helpPath = Paths.get(help);
if (!Files.exists(helpPath) || !Files.isDirectory(helpPath)) {
throw new EvalError("invalid help directory " + help + "; please set bsh.help to an existing and readable directory", null, callstack);
}
try {
try (Stream paths = Files.walk(helpPath)) {
paths
.filter(Files::isRegularFile)
.filter(new Predicate() {
@Override
public boolean test(Path p) {
return (name == null) ? p.toString().endsWith(".txt")
: String.valueOf(p.getFileName()).equals(name + ".txt");
}
})
.sorted()
.forEachOrdered(new Consumer() {
@Override
public void accept(Path p) {
String name = StringUtils.removeEnd(
StringUtils.removeStart(p.toString(), help),
".txt"
).substring(1).replaceAll("/", ".");
bsh.println("");
bsh.println(name);
bsh.println(StringUtils.repeat("=", name.length()));
bsh.println("");
try {
//
// TODO: indent the content of the file
//
bsh.println(FileUtils.readFileToString(p.toFile(), "UTF8"));
} catch (IOException x) {
//
// TODO: handling
//
}
bsh.println(""); bsh.println("");
}
});
}
} catch (IOException x) {
//
// TODO handle the error
//
}
return;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy