Skip to content

Commit

Permalink
Various minor code cleanup (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Dec 10, 2024
1 parent ef44e24 commit c978a7d
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 112 deletions.
28 changes: 14 additions & 14 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ public static void history(LineReader reader, PrintStream out, PrintStream err,
} else if (opt.isSet("save")) {
history.save();
} else if (opt.isSet("A")) {
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
history.append(file, increment);
} else if (opt.isSet("R")) {
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
history.read(file, increment);
} else if (opt.isSet("W")) {
Path file = opt.args().size() > 0 ? currentDir.resolve(opt.args().get(0)) : null;
Path file = !opt.args().isEmpty() ? currentDir.resolve(opt.args().get(0)) : null;
history.write(file, increment);
} else {
done = false;
Expand Down Expand Up @@ -361,7 +361,7 @@ public ReExecute(History history, Options opt) throws IOException {
if (edit) {
cmdFile = File.createTempFile("jline-history-", null);
cmdWriter = new FileWriter(cmdFile);
} else if (opt.args().size() > 0) {
} else if (!opt.args().isEmpty()) {
String[] s = opt.args().get(argId).split("=");
if (s.length == 2) {
argId = argId + 1;
Expand Down Expand Up @@ -640,7 +640,7 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
if (opt.isSet("l")) {
boolean commands = opt.isSet("L");
// TODO: handle commands
if (opt.args().size() > 0) {
if (!opt.args().isEmpty()) {
for (String arg : opt.args()) {
KeyMap<Binding> map = keyMaps.get(arg);
if (map == null) {
Expand Down Expand Up @@ -704,7 +704,7 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
err.println("keymap: keymap can not be selected with -N");
return;
}
if (opt.args().size() > 0) {
if (!opt.args().isEmpty()) {
err.println("keymap: too many arguments for -d");
return;
}
Expand Down Expand Up @@ -869,9 +869,9 @@ public static void keymap(LineReader reader, PrintStream out, PrintStream err, S
err.println("keymap: option -p requires a prefix string");
return;
}
if (opt.args().size() > 0 || !opt.isSet("e") && !opt.isSet("v")) {
if (!opt.args().isEmpty() || !opt.isSet("e") && !opt.isSet("v")) {
Map<String, Binding> bound = map.getBoundKeys();
String seq = opt.args().size() > 0 ? KeyMap.translate(opt.args().get(0)) : null;
String seq = !opt.args().isEmpty() ? KeyMap.translate(opt.args().get(0)) : null;
Map.Entry<String, Binding> begin = null;
String last = null;
Iterator<Entry<String, Binding>> iterator = bound.entrySet().iterator();
Expand Down 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 (op == Operation.ACCEPT) {
String name = buffer.substring(begPos);
addSource(name);
try {
openSource();
} catch (Exception exp) {
ssp.restore(name);
}
return;
} else if (op != null) {
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
30 changes: 12 additions & 18 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 @@ -1450,7 +1449,7 @@ public String up(String hint) {
}
boolean found = false;
for (int pid = patternId; pid < patterns.size(); pid++) {
if (hint.length() == 0 || patterns.get(pid).startsWith(hint)) {
if (hint.isEmpty() || patterns.get(pid).startsWith(hint)) {
patternId = pid + 1;
out = patterns.get(pid);
found = true;
Expand All @@ -1467,7 +1466,7 @@ public String up(String hint) {

public String down(String hint) {
String out = hint;
if (patterns.size() > 0) {
if (!patterns.isEmpty()) {
if (lastMoveUp) {
patternId--;
}
Expand All @@ -1493,7 +1492,7 @@ public String down(String hint) {
}

public void add(String pattern) {
if (pattern.trim().length() == 0) {
if (pattern.trim().isEmpty()) {
return;
}
patterns.remove(pattern);
Expand All @@ -1512,7 +1511,7 @@ public void persist() {
try (BufferedWriter writer = Files.newBufferedWriter(
historyFile.toAbsolutePath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
for (String s : patterns) {
if (s.trim().length() > 0) {
if (!s.trim().isEmpty()) {
writer.append(s);
writer.newLine();
}
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 @@ -2151,7 +2150,7 @@ private boolean save(String name) throws IOException {
setMessage("Wrote " + buffer.lines.size() + " lines");
return true;
} catch (IOException e) {
setMessage("Error writing " + name + ": " + e.toString());
setMessage("Error writing " + name + ": " + e);
return false;
} finally {
Files.deleteIfExists(t);
Expand Down Expand Up @@ -2312,7 +2311,7 @@ private String getReadMessage() {
return sb.toString();
}

void gotoLine() throws IOException {
void gotoLine() {
KeyMap<Operation> readKeyMap = new KeyMap<>();
readKeyMap.setUnicode(Operation.INSERT);
for (char i = 32; i < 256; i++) {
Expand Down Expand Up @@ -2360,7 +2359,7 @@ void gotoLine() throws IOException {
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 @@ -2786,12 +2785,7 @@ String replace() throws IOException {
if (editBuffer.length() > 0) {
replaceTerm = editBuffer.toString();
}
if (replaceTerm == null) {
setMessage("Cancelled");
throw new IllegalArgumentException();
} else {
patternHistory.add(replaceTerm);
}
patternHistory.add(replaceTerm);
return replaceTerm;
case HELP:
help("nano-replace-help.txt");
Expand Down Expand Up @@ -2868,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 Expand Up @@ -2965,7 +2959,7 @@ void smoothScrolling() {
setMessage("Smooth scrolling " + (smoothScrolling ? "enabled" : "disabled"));
}

void mouseSupport() throws IOException {
void mouseSupport() {
mouseSupport = !mouseSupport;
setMessage("Mouse support " + (mouseSupport ? "enabled" : "disabled"));
terminal.trackMouse(mouseSupport ? Terminal.MouseTracking.Normal : Terminal.MouseTracking.Off);
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());
}
}
2 changes: 1 addition & 1 deletion console/src/main/java/org/jline/console/CmdDesc.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ public boolean optionWithValue(String option) {
}

public AttributedString optionDescription(String key) {
return optsDesc.get(key).size() > 0 ? optsDesc.get(key).get(0) : new AttributedString("");
return !optsDesc.get(key).isEmpty() ? optsDesc.get(key).get(0) : new AttributedString("");
}
}
Loading

0 comments on commit c978a7d

Please sign in to comment.