diff --git a/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java b/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java index 6a24dae1..4b4f8931 100644 --- a/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/file/FileUtils.java @@ -117,7 +117,7 @@ public class FileUtils { * @param fileName The name to sanitize. * @param sanitizeWhitespaces If set to {@code true}, then white space characters ` \t\n` will be * converted. - * @param sanitizeWhitespaces If set to {@code true}, then file name will be converted to lowe case. + * @param toLower If set to {@code true}, then file name will be converted to lower case. * @return Returns the {@code sanitized name}. */ public static String sanitizeFileName(String fileName, boolean sanitizeWhitespaces, boolean toLower) { @@ -1855,4 +1855,42 @@ public class FileUtils { return shortErrno.getError(throwables, error.getLabel(), "file"); } + + /** + * Get file dirname for file at {@code filePath}. + * + * @param filePath The {@code path} for file. + * @return Returns the file dirname if not {@code null}. + */ + public static String getFileDirname(String filePath) { + if (DataUtils.isNullOrEmpty(filePath)) return null; + int lastSlash = filePath.lastIndexOf('/'); + return (lastSlash == -1) ? null : filePath.substring(0, lastSlash); + } + + /** + * Get file basename for file at {@code filePath}. + * + * @param filePath The {@code path} for file. + * @return Returns the file basename if not {@code null}. + */ + public static String getFileBasename(String filePath) { + if (DataUtils.isNullOrEmpty(filePath)) return null; + int lastSlash = filePath.lastIndexOf('/'); + return (lastSlash == -1) ? filePath : filePath.substring(lastSlash + 1); + } + + /** + * Get file basename for file at {@code filePath} without extension. + * + * @param filePath The {@code path} for file. + * @return Returns the file basename without extension if not {@code null}. + */ + public static String getFileBasenameWithoutExtension(String filePath) { + String fileBasename = getFileBasename(filePath); + if (DataUtils.isNullOrEmpty(fileBasename)) return null; + int lastDot = fileBasename.lastIndexOf('.'); + return (lastDot == -1) ? fileBasename : fileBasename.substring(0, lastDot); + } + } diff --git a/termux-shared/src/main/java/com/termux/shared/shell/ShellUtils.java b/termux-shared/src/main/java/com/termux/shared/shell/ShellUtils.java index d6687e05..302528b7 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/ShellUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/ShellUtils.java @@ -1,5 +1,6 @@ package com.termux.shared.shell; +import com.termux.shared.file.FileUtils; import com.termux.terminal.TerminalBuffer; import com.termux.terminal.TerminalEmulator; import com.termux.terminal.TerminalSession; @@ -23,9 +24,7 @@ public class ShellUtils { } public static String getExecutableBasename(String executable) { - if (executable == null) return null; - int lastSlash = executable.lastIndexOf('/'); - return (lastSlash == -1) ? executable : executable.substring(lastSlash + 1); + return FileUtils.getFileBasename(executable); } public static String getTerminalSessionTranscriptText(TerminalSession terminalSession, boolean linesJoined, boolean trim) {