better path insertion to command line

This commit is contained in:
Radek Davidek 2026-03-04 19:25:30 +01:00
parent 31715240b6
commit a06d421e84

View File

@ -388,6 +388,12 @@ public class MainWindow extends JFrame {
} else if (e.getKeyCode() == KeyEvent.VK_TAB) {
activePanel.getFileTable().requestFocusInWindow();
e.consume();
} else if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown() && e.isShiftDown()) {
copyFocusedToCommandLineAtCaret(tf, true);
e.consume();
} else if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown()) {
copyFocusedToCommandLineAtCaret(tf, false);
e.consume();
} else if (e.getKeyCode() == KeyEvent.VK_E && e.isControlDown()) {
showCommandLineHistory();
e.consume();
@ -1666,6 +1672,37 @@ public class MainWindow extends JFrame {
commandLine.requestFocusInWindow();
}
}
private void copyFocusedToCommandLineAtCaret(JTextField editor, boolean fullPath) {
if (editor == null || activePanel == null) return;
FileItem focused = activePanel.getFocusedItem();
if (focused == null || focused.getName().equals("..")) return;
String toAdd = fullPath ? focused.getFile().getAbsolutePath() : focused.getName();
if (toAdd.contains(" ")) {
toAdd = "\"" + toAdd + "\"";
}
String current = editor.getText();
int caret = editor.getCaretPosition();
if (caret < 0) caret = 0;
if (caret > current.length()) caret = current.length();
String before = current.substring(0, caret);
String after = current.substring(caret);
boolean addSpaceBefore = !before.isEmpty() && !Character.isWhitespace(before.charAt(before.length() - 1));
boolean addSpaceAfter = !after.isEmpty() && !Character.isWhitespace(after.charAt(0));
String insertion = (addSpaceBefore ? " " : "") + toAdd + (addSpaceAfter ? " " : "");
String newText = before + insertion + after;
editor.setText(newText);
int newCaret = before.length() + insertion.length();
editor.setCaretPosition(Math.min(newCaret, newText.length()));
commandLine.requestFocusInWindow();
}
/**
* Copy selected files to the opposite panel