fixed command

This commit is contained in:
Radek Davidek 2026-01-26 14:21:56 +01:00
parent 9dac667ee8
commit 02b84d6acc
3 changed files with 92 additions and 41 deletions

View File

@ -27,7 +27,7 @@ public class MainApp {
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) { if (os.contains("win")) {
CURRENT_OS = OS.WINDOWS; CURRENT_OS = OS.WINDOWS;
} else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { } else if (os.contains("linux") || os.contains("nix") || os.contains("nux") || os.contains("aix")) {
CURRENT_OS = OS.LINUX; CURRENT_OS = OS.LINUX;
} else if (os.contains("mac")) { } else if (os.contains("mac")) {
CURRENT_OS = OS.MACOS; CURRENT_OS = OS.MACOS;

View File

@ -117,6 +117,8 @@ public class MainWindow extends JFrame {
addTabKeyHandler(table); addTabKeyHandler(table);
addCommandLineRedirect(table); addCommandLineRedirect(table);
}); });
addCommandLineRedirect(leftPanel.getFileTable());
addTabKeyHandler(leftPanel.getFileTable());
// Load and set ViewMode for left panel // Load and set ViewMode for left panel
try { try {
@ -137,6 +139,8 @@ public class MainWindow extends JFrame {
addTabKeyHandler(table); addTabKeyHandler(table);
addCommandLineRedirect(table); addCommandLineRedirect(table);
}); });
addCommandLineRedirect(rightPanel.getFileTable());
addTabKeyHandler(rightPanel.getFileTable());
// Load and set ViewMode for right panel // Load and set ViewMode for right panel
try { try {
@ -291,14 +295,6 @@ public class MainWindow extends JFrame {
} }
}); });
// Add TAB handler to switch between panels
addTabKeyHandler(leftPanel.getFileTable());
addTabKeyHandler(rightPanel.getFileTable());
// Add command line focus redirection
addCommandLineRedirect(leftPanel.getFileTable());
addCommandLineRedirect(rightPanel.getFileTable());
// Container for everything below the file panels // Container for everything below the file panels
JPanel bottomContainer = new JPanel(new BorderLayout()); JPanel bottomContainer = new JPanel(new BorderLayout());
@ -319,8 +315,21 @@ public class MainWindow extends JFrame {
if (editorComp instanceof JTextField) { if (editorComp instanceof JTextField) {
JTextField tf = (JTextField) editorComp; JTextField tf = (JTextField) editorComp;
tf.setFocusTraversalKeysEnabled(false); tf.setFocusTraversalKeysEnabled(false);
tf.putClientProperty("JTextField.selectAllOnFocus", Boolean.FALSE);
tf.addActionListener(e -> executeCommand(tf.getText())); tf.addActionListener(e -> executeCommand(tf.getText()));
tf.addFocusListener(new java.awt.event.FocusAdapter() {
@Override
public void focusGained(java.awt.event.FocusEvent e) {
// Force caret to the end and clear selection when focus is gained
SwingUtilities.invokeLater(() -> {
tf.setSelectionStart(tf.getText().length());
tf.setSelectionEnd(tf.getText().length());
tf.setCaretPosition(tf.getText().length());
});
}
});
// Enable standard clipboard operations (Cut, Copy, Paste) even if not focused initially // Enable standard clipboard operations (Cut, Copy, Paste) even if not focused initially
tf.getComponentPopupMenu(); // Ensure it has a menu or at least default actions works tf.getComponentPopupMenu(); // Ensure it has a menu or at least default actions works
@ -1523,6 +1532,7 @@ public class MainWindow extends JFrame {
* Automatically focus command line when user starts typing on a table * Automatically focus command line when user starts typing on a table
*/ */
public void addCommandLineRedirect(JTable table) { public void addCommandLineRedirect(JTable table) {
if (table == null) return;
// Use InputMap/ActionMap for Ctrl+Enter and Ctrl+Shift+Enter as KeyListener might be bypassed by JTable // Use InputMap/ActionMap for Ctrl+Enter and Ctrl+Shift+Enter as KeyListener might be bypassed by JTable
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK), "copyNameToCmd"); .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK), "copyNameToCmd");
@ -1563,7 +1573,6 @@ public class MainWindow extends JFrame {
private void copyFocusedToCommandLine(boolean fullPath) { private void copyFocusedToCommandLine(boolean fullPath) {
FileItem focused = activePanel.getFocusedItem(); FileItem focused = activePanel.getFocusedItem();
if (focused != null && !focused.getName().equals("..")) { if (focused != null && !focused.getName().equals("..")) {
String current = commandLine.getEditor().getItem().toString();
String toAdd = fullPath ? focused.getFile().getAbsolutePath() : focused.getName(); String toAdd = fullPath ? focused.getFile().getAbsolutePath() : focused.getName();
// If it contains spaces, wrap in quotes // If it contains spaces, wrap in quotes
@ -1571,11 +1580,15 @@ public class MainWindow extends JFrame {
toAdd = "\"" + toAdd + "\""; toAdd = "\"" + toAdd + "\"";
} }
String current = commandLine.getEditor().getItem().toString();
String newText;
if (!current.isEmpty() && !current.endsWith(" ")) { if (!current.isEmpty() && !current.endsWith(" ")) {
commandLine.getEditor().setItem(current + " " + toAdd); newText = current + " " + toAdd;
} else { } else {
commandLine.getEditor().setItem(current + toAdd); newText = current + toAdd;
} }
commandLine.getEditor().setItem(newText);
commandLine.requestFocusInWindow(); commandLine.requestFocusInWindow();
} }
} }
@ -2213,54 +2226,89 @@ public class MainWindow extends JFrame {
* Open terminal in the current directory * Open terminal in the current directory
*/ */
private void openTerminal() { private void openTerminal() {
openTerminal(null);
}
/**
* Open terminal in the current directory, optionally executing a command (like a shell)
*/
/**
* Open terminal in the current directory, optionally executing a command (like a shell)
*/
private void openTerminal(String shellCommand) {
File currentDir = activePanel.getCurrentDirectory(); File currentDir = activePanel.getCurrentDirectory();
if (currentDir == null) { if (currentDir == null) {
currentDir = new File(System.getProperty("user.home")); currentDir = new File(System.getProperty("user.home"));
} }
try { try {
ProcessBuilder pb = null;
if (MainApp.CURRENT_OS == MainApp.OS.WINDOWS) { if (MainApp.CURRENT_OS == MainApp.OS.WINDOWS) {
// Windows - open cmd.exe // Windows - open cmd.exe or the specific shell
pb = new ProcessBuilder("cmd.exe", "/c", "start", "cmd.exe"); if (shellCommand != null) {
new ProcessBuilder("cmd.exe", "/c", "start", shellCommand).directory(currentDir).start();
} else {
new ProcessBuilder("cmd.exe", "/c", "start", "cmd.exe").directory(currentDir).start();
}
} else if (MainApp.CURRENT_OS == MainApp.OS.MACOS) { } else if (MainApp.CURRENT_OS == MainApp.OS.MACOS) {
// macOS // macOS - open Terminal.app
pb = new ProcessBuilder("open", "-a", "Terminal", currentDir.getAbsolutePath()); new ProcessBuilder("open", "-a", "Terminal", currentDir.getAbsolutePath()).directory(currentDir).start();
} else { } else {
// Linux and other Unix-like systems // Linux - try common terminal emulators
// Try common terminal emulators with working directory arguments String[] terminals = {
String[] terminals = {"gnome-terminal", "konsole", "xfce4-terminal", "mate-terminal", "xterm"}; "x-terminal-emulator",
"gnome-terminal",
"konsole",
"xfce4-terminal",
"mate-terminal",
"terminator",
"tilix",
"qterminal",
"xterm"
};
boolean successfullyStarted = false;
for (String terminal : terminals) { for (String terminal : terminals) {
try { try {
Process p = Runtime.getRuntime().exec(new String[]{"which", terminal}); List<String> args = new ArrayList<>();
if (p.waitFor() == 0) { args.add(terminal);
if (terminal.equals("gnome-terminal") || terminal.equals("xfce4-terminal") || terminal.equals("mate-terminal")) {
pb = new ProcessBuilder(terminal, "--working-directory=" + currentDir.getAbsolutePath()); if (terminal.equals("gnome-terminal") || terminal.equals("xfce4-terminal") ||
} else if (terminal.equals("konsole")) { terminal.equals("mate-terminal") || terminal.equals("terminator") ||
pb = new ProcessBuilder(terminal, "--workdir", currentDir.getAbsolutePath()); terminal.equals("tilix")) {
} else { args.add("--working-directory=" + currentDir.getAbsolutePath());
pb = new ProcessBuilder(terminal); if (shellCommand != null) {
args.add("--");
args.add(shellCommand);
args.add("-i");
}
} else if (terminal.equals("konsole") || terminal.equals("qterminal")) {
args.add("--workdir");
args.add(currentDir.getAbsolutePath());
if (shellCommand != null) {
args.add("-e");
args.add(shellCommand);
args.add("-i");
}
} else {
if (shellCommand != null) {
args.add("-e");
args.add(shellCommand);
args.add("-i");
} }
break;
} }
} catch (Exception e) {
// Try next terminal new ProcessBuilder(args).directory(currentDir).start();
successfullyStarted = true;
break;
} catch (IOException e) {
// try next
} }
} }
if (pb == null) { if (!successfullyStarted) {
// Fallback to xterm throw new Exception("Could not start any terminal emulator.");
pb = new ProcessBuilder("xterm");
} }
} }
if (pb != null) {
pb.directory(currentDir);
pb.start();
}
} catch (Exception e) { } catch (Exception e) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Error opening terminal: " + e.getMessage(), "Error opening terminal: " + e.getMessage(),
@ -2307,6 +2355,9 @@ public class MainWindow extends JFrame {
} }
} }
} }
} else if (trimmed.equalsIgnoreCase("bash") || trimmed.equalsIgnoreCase("sh")) {
// Special handling for bash/sh to open a terminal with that shell
openTerminal(trimmed.toLowerCase());
} else { } else {
// Execute natively for other commands // Execute natively for other commands
executeNative(trimmed, null); executeNative(trimmed, null);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 66 KiB