mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added: Add functions to get dirname and basename in FileUtils
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user