Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 8, 2024
1 parent 088bcff commit ddd1c7d
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 72 deletions.
12 changes: 6 additions & 6 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1682,21 +1682,21 @@ public static void highlighter(
pathStream.filter(pathMatcher::matches).forEach(p -> out.println(p.getFileName()));
}
} else {
File themeFile;
Path themeFile;
if (opt.isSet("view")) {
themeFile = new File(replaceFileName(currentTheme, opt.get("view")));
themeFile = Paths.get(replaceFileName(currentTheme, opt.get("view")));
} else {
themeFile = currentTheme.toFile();
themeFile = currentTheme;
}
out.println(themeFile.getAbsolutePath());
try (BufferedReader reader = new BufferedReader(new FileReader(themeFile))) {
out.println(themeFile.toAbsolutePath());
try (BufferedReader reader = Files.newBufferedReader(themeFile)) {
String line;
List<List<String>> tokens = new ArrayList<>();
int maxKeyLen = 0;
int maxValueLen = 0;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = Arrays.asList(line.split("\\s+", 2));
if (parts.get(0).matches(REGEX_TOKEN_NAME)) {
if (parts.get(0).length() > maxKeyLen) {
Expand Down
39 changes: 18 additions & 21 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -218,11 +217,11 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath
}

private void parseConfig(Path file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
try (BufferedReader reader = Files.newBufferedReader(file)) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = SyntaxHighlighter.RuleSplitter.split(line);
if (parts.get(0).equals(COMMAND_INCLUDE)) {
SyntaxHighlighter.nanorcInclude(parts.get(1), syntaxFiles);
Expand Down Expand Up @@ -840,20 +839,18 @@ private void addFile() throws IOException, InterruptedException {
LineEditor lineEditor = new LineEditor(begPos);
while (true) {
checkInterrupted();
Operation op;
switch (op = bindingReader.readBinding(fileKeyMap)) {
case ACCEPT:
String name = buffer.substring(begPos);
addSource(name);
try {
openSource();
} catch (Exception exp) {
ssp.restore(name);
}
return;
default:
curPos = lineEditor.editBuffer(op, curPos);
break;
Operation op = bindingReader.readBinding(fileKeyMap);
if (Objects.requireNonNull(op) == Operation.ACCEPT) {
String name = buffer.substring(begPos);
addSource(name);
try {
openSource();
} catch (Exception exp) {
ssp.restore(name);
}
return;
} else {
curPos = lineEditor.editBuffer(op, curPos);
}
if (curPos > begPos) {
display(false, curPos);
Expand Down Expand Up @@ -911,7 +908,7 @@ private boolean search() throws IOException, InterruptedException {
try {
String _pattern = buffer.substring(1);
if (type == '&') {
displayPattern = _pattern.length() > 0 ? _pattern : null;
displayPattern = !_pattern.isEmpty() ? _pattern : null;
getPattern(true);
} else {
pattern = _pattern;
Expand Down Expand Up @@ -1025,7 +1022,7 @@ protected void openSource() throws IOException {
if (displayMessage) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.style(AttributedStyle.INVERSE);
asb.append(source.getName() + " (press RETURN)");
asb.append(source.getName()).append(" (press RETURN)");
asb.toAttributedString().println(terminal);
terminal.writer().flush();
terminal.reader().read();
Expand All @@ -1039,7 +1036,7 @@ protected void openSource() throws IOException {
throw exp;
} else {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(source.getName() + " not found!");
asb.append(source.getName()).append(" not found!");
asb.toAttributedString().println(terminal);
terminal.writer().flush();
open = false;
Expand Down Expand Up @@ -1384,7 +1381,7 @@ synchronized boolean display(boolean oneScreen, Integer curPos) throws IOExcepti
}
if (buffer.length() > 0) {
msg.append(" ").append(buffer);
} else if (bindingReader.getCurrentBuffer().length() > 0
} else if (!bindingReader.getCurrentBuffer().isEmpty()
&& terminal.reader().peek(1) == NonBlockingReader.READ_EXPIRED) {
msg.append(" ").append(printable(bindingReader.getCurrentBuffer()));
} else if (message != null) {
Expand Down
9 changes: 4 additions & 5 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -1656,11 +1655,11 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config
}

private void parseConfig(Path file) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
try (BufferedReader reader = Files.newBufferedReader(file)) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = SyntaxHighlighter.RuleSplitter.split(line);
if (parts.get(0).equals(COMMAND_INCLUDE)) {
SyntaxHighlighter.nanorcInclude(parts.get(1), syntaxFiles);
Expand Down Expand Up @@ -2360,7 +2359,7 @@ void gotoLine() {
int[] args = {0, 0};
try {
for (int i = 0; i < pos.length; i++) {
if (pos[i].trim().length() > 0) {
if (!pos[i].trim().isEmpty()) {
args[i] = Integer.parseInt(pos[i]) - 1;
if (args[i] < 0) {
throw new NumberFormatException();
Expand Down Expand Up @@ -2863,7 +2862,7 @@ String computeCurPos() {
sb.append("/");
sb.append(buffer.length(buffer.lines.get(buffer.line)) + 1);
sb.append(" (");
if (buffer.lines.get(buffer.line).length() > 0) {
if (!buffer.lines.get(buffer.line).isEmpty()) {
sb.append(Math.round(
(100.0 * (buffer.offsetInLine + buffer.column)) / (buffer.length(buffer.lines.get(buffer.line)))));
} else {
Expand Down
6 changes: 3 additions & 3 deletions builtins/src/main/java/org/jline/builtins/ScreenTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ private void csi_DECSTR(String p) {

private String[] vt100_parse_params(String p, String[] defaults) {
String prefix = "";
if (p.length() > 0) {
if (!p.isEmpty()) {
if (p.charAt(0) >= '<' && p.charAt(0) <= '?') {
prefix = "" + p.charAt(0);
p = p.substring(1);
Expand All @@ -1094,7 +1094,7 @@ private String[] vt100_parse_params(String p, String[] defaults) {
String[] values = new String[n];
for (int i = 0; i < n; i++) {
String value = null;
if (i < ps.length && ps[i].length() > 0) {
if (i < ps.length && !ps[i].isEmpty()) {
value = prefix + ps[i];
}
if (value == null && i < defaults.length) {
Expand All @@ -1111,7 +1111,7 @@ private String[] vt100_parse_params(String p, String[] defaults) {
private int[] vt100_parse_params(String p, int[] defaults) {
String prefix = "";
p = p == null ? "" : p;
if (p.length() > 0) {
if (!p.isEmpty()) {
if (p.charAt(0) >= '<' && p.charAt(0) <= '?') {
prefix = p.substring(0, 1);
p = p.substring(1);
Expand Down
10 changes: 5 additions & 5 deletions builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ protected static SyntaxHighlighter build(
try {
if (colorTheme.isEmpty() && p.getFileName().toString().endsWith(TYPE_NANORCTHEME)) {
out.setCurrentTheme(p);
try (BufferedReader reader = new BufferedReader(new FileReader(p.toFile()))) {
try (BufferedReader reader = Files.newBufferedReader(p)) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = Arrays.asList(line.split("\\s+", 2));
colorTheme.put(parts.get(0), parts.get(1));
}
Expand Down Expand Up @@ -126,11 +126,11 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) {
SyntaxHighlighter out = new SyntaxHighlighter(nanorc, syntaxName);
List<Path> syntaxFiles = new ArrayList<>();
try {
try (BufferedReader reader = new BufferedReader(new FileReader(nanorc.toFile()))) {
try (BufferedReader reader = Files.newBufferedReader(nanorc)) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = RuleSplitter.split(line);
if (parts.get(0).equals(COMMAND_INCLUDE)) {
nanorcInclude(parts.get(1), syntaxFiles);
Expand Down Expand Up @@ -496,7 +496,7 @@ public void parse() throws IOException {
while ((line = reader.readLine()) != null) {
idx++;
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
if (!line.isEmpty() && !line.startsWith("#")) {
List<String> parts = RuleSplitter.split(fixRegexes(line));
if (parts.get(0).equals("syntax")) {
syntaxName = parts.get(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void main(String[] args) throws IOException {
do {
line = reader.readLine("prompt> ", null, maskingCallback, null);
System.out.println("Got line: " + line);
} while (line != null && line.length() > 0);
} while (line != null && !line.isEmpty());
}

private static class CommandArgumentMask implements MaskingCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void main(String[] args) throws IOException {
do {
line = reader.readLine("prompt> ", null, maskingCallback, null);
System.out.println("Got line: " + line);
} while (line != null && line.length() > 0);
} while (line != null && !line.isEmpty());
}

private static class OptionValueMask implements MaskingCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static void main(String[] args) throws IOException {
do {
line = reader.readLine("Enter password> ", mask);
System.out.println("Got password: " + line);
} while (line != null && line.length() > 0);
} while (line != null && !line.isEmpty());
}
}
14 changes: 13 additions & 1 deletion console/src/main/java/org/jline/console/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,19 @@ default Object execute(File script) throws Exception {
* @return script execution result
* @throws Exception in case of error
*/
Object execute(File script, String rawLine, String[] args) throws Exception;
default Object execute(File script, String rawLine, String[] args) throws Exception {
return execute(script != null ? script.toPath() : null, rawLine, args);
}

/**
* Executes either JLine or ScriptEngine script.
* @param script script file
* @param rawLine raw command line
* @param args script arguments
* @return script execution result
* @throws Exception in case of error
*/
Object execute(Path script, String rawLine, String[] args) throws Exception;

/**
* Post processes execution result. If result is to be assigned to the console variable
Expand Down
31 changes: 15 additions & 16 deletions console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -373,7 +372,7 @@ private List<String> scriptExtensions() {
}

private class ScriptFile {
private File script;
private Path script;
private String extension = "";
private String cmdLine;
private String[] args;
Expand All @@ -386,9 +385,9 @@ public ScriptFile(String command, String cmdLine, String[] args) {
return;
}
try {
this.script = new File(command);
this.script = Paths.get(command);
this.cmdLine = cmdLine;
if (script.exists()) {
if (Files.exists(script)) {
scriptExtension(command);
} else if (engine.hasVariable(VAR_PATH)) {
boolean found = false;
Expand All @@ -397,7 +396,7 @@ public ScriptFile(String command, String cmdLine, String[] args) {
String file = command + "." + e;
Path path = Paths.get(p, file);
if (path.toFile().exists()) {
script = path.toFile();
script = path;
scriptExtension(command);
found = true;
break;
Expand All @@ -414,18 +413,18 @@ public ScriptFile(String command, String cmdLine, String[] args) {
}
}

public ScriptFile(File script, String cmdLine, String[] args) {
if (!script.exists()) {
public ScriptFile(Path script, String cmdLine, String[] args) {
if (!Files.exists(script)) {
throw new IllegalArgumentException("Script file not found!");
}
this.script = script;
this.cmdLine = cmdLine;
scriptExtension(script.getName());
scriptExtension(script.getFileName().toString());
doArgs(args);
}

private void scriptExtension(String command) {
String name = script.getName();
String name = script.getFileName().toString();
this.extension = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
if (!isEngineScript() && !isConsoleScript()) {
throw new IllegalArgumentException("Command not found: " + command);
Expand All @@ -435,7 +434,7 @@ private void scriptExtension(String command) {
private void doArgs(String[] args) {
List<String> _args = new ArrayList<>();
if (isConsoleScript()) {
_args.add(script.getAbsolutePath());
_args.add(script.toAbsolutePath().toString());
}
for (String a : args) {
if (isConsoleScript()) {
Expand Down Expand Up @@ -470,7 +469,7 @@ public boolean execute() throws Exception {
result = null;
if (Arrays.asList(args).contains(OPTION_HELP[0])
|| Arrays.asList(args).contains(OPTION_HELP[1])) {
try (BufferedReader br = new BufferedReader(new FileReader(script))) {
try (BufferedReader br = Files.newBufferedReader(script)) {
int size = 0;
StringBuilder usage = new StringBuilder();
boolean helpEnd = false;
Expand Down Expand Up @@ -527,7 +526,7 @@ private void internalExecute() throws Exception {
executing = true;
boolean done = true;
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(script))) {
try (BufferedReader br = Files.newBufferedReader(script)) {
for (String l; (l = br.readLine()) != null; ) {
if (l.trim().isEmpty() || l.trim().startsWith("#")) {
done = true;
Expand Down Expand Up @@ -591,7 +590,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
try {
sb.append("script:").append(script.getCanonicalPath());
sb.append("script:").append(script.normalize());
} catch (Exception e) {
sb.append(e.getMessage());
}
Expand All @@ -611,7 +610,7 @@ public String toString() {
}

@Override
public Object execute(File script, String cmdLine, String[] args) throws Exception {
public Object execute(Path script, String cmdLine, String[] args) throws Exception {
ScriptFile file = new ScriptFile(script, cmdLine, args);
file.execute();
return file.getResult();
Expand Down Expand Up @@ -670,8 +669,8 @@ public Object execute(String cmd, String line, String[] args) throws Exception {
if (parser().validCommandName(cmd)) {
file = new ScriptFile(cmd, line, args);
} else {
File f = new File(line.split("\\s+")[0]);
if (f.exists()) {
Path f = Paths.get(line.split("\\s+")[0]);
if (Files.exists(f)) {
file = new ScriptFile(f, line, args);
}
}
Expand Down
Loading

0 comments on commit ddd1c7d

Please sign in to comment.