Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from body20002/clean
Browse files Browse the repository at this point in the history
Clean
  • Loading branch information
0x61nas authored Jun 10, 2022
2 parents 941c5fd + c63fd26 commit 069f7f9
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 159 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

![jls](./Screenshots/0.2.2_in_alacritty.png "jls")
![jls -lh](./Screenshots/0.2.2_in_alacritty_with-lh.png "jls -lh")

## Features

- Support text color.
- Support icons for file type (require [nerd-font](https://www.nerdfonts.com)).


## Options table

| Option | Value | Description |
|----------------------------|-------------------------------------------------|-----------------------------------------------------------------|
| -------------------------- | ----------------------------------------------- | --------------------------------------------------------------- |
| `--help` | n/a | Show help message |
| `-v` or `--version` | n/a | Show version |
| `-a` or `--all` | n/a | Show all files including hidden files |
Expand All @@ -30,25 +32,29 @@
| `-s` or `--size` | n/a | Show file size in normal format |
| `-S` | n/a | Sort by size (largest first) |
| `-R` or `--revcursive` | n/a | Show files in sub-directories recursively (not implemented yet) |
| `-nc` or `--no-icons` | n/a | Don't show icons |
| `-nc` or `--no-icons` | n/a | Don't show icons |
| `-ic` or `--icons-color` | Icon color in hex format or Auto for auto color | Use custom icons color (not implemented yet) |
| `-tc` or `--text-color` | Text color in hex format or Auto for auto color | Use custom text color (not implemented yet) |
| `-tc` or `--text-color` | Text color in hex format or Auto for auto color | Use custom text color (not implemented yet) |
| `--no-owner` | n/a | Don't show owner name in long format |
| `--no-colors` | n/a | Don't use colors |
| `-nd` or `--no-date` | n/a | Don't show last modified date |
| `-nn` or `--no-name` | n/a | Don't show file name |
| `-L` | n/a | Sort by last modified date (latest first) |



## Requirements for development:

- [nerd-font](https://www.nerdfonts.com)
- Gradle 7.4
- jdk 17
- IntelliJ IDEA (not required but recommended)

## TODO

- [ ] Add Tests
- [ ] Add Formatter

### Available in

[![GitHub](https://img.shields.io/badge/GitHub-Main%20repo-brightgreen?style=for-the-badge&logo=GitHub)](https://github.com/Anas-Elgarhy/jls)
[![GitLab](https://img.shields.io/badge/GitLab-Mirror%20repo-brightgreen?style=for-the-badge&logo=GitLab)](https://gitlab.com/java-utils1/jls)
[![BitBucket](https://img.shields.io/badge/BitBucket-Mirror%20repo-brightgreen?style=for-the-badge&logo=BitBucket)](https://bitbucket.org/anas_elgarhy/jls)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/anas/javautils/jls/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class Main {
public static final String version = "0.2.2";

public static void main(String[] args) {
ArgumentProcessor.getInstance().process(args);
try {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/anas/javautils/jls/args/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.util.logging.Logger;

public class ArgumentProcessor {
private final Options options;
private CommandLine commandLine;
private final Logger logger = Logger.getLogger(ArgumentProcessor.class.getName());
// Singleton
private static ArgumentProcessor instance;
private final Options options;
private final Logger logger = Logger.getLogger(ArgumentProcessor.class.getName());
private CommandLine commandLine;

private ArgumentProcessor() {
options = new Options();
Expand Down Expand Up @@ -58,16 +58,18 @@ public boolean hasOption(CLIOption option) {

public Path getTargetPath() {
Path targetPath = null;
if (commandLine.getArgs().length > 0) {
try {
targetPath = Path.of(commandLine.getArgs()[0]).toRealPath();
} catch (IOException e) {
logger.severe("Error: " + e.getMessage());
System.exit(1);
}
} else {
targetPath = Path.of(System.getProperty("user.dir"));

if (commandLine.getArgs().length == 0) {
return Path.of(System.getProperty("user.dir"));
}

try {
targetPath = Path.of(commandLine.getArgs()[0]).toRealPath();
} catch (IOException e) {
logger.severe("Error: " + e.getMessage());
System.exit(1);
}

return targetPath;
}
}
195 changes: 113 additions & 82 deletions src/main/java/com/anas/javautils/jls/output/FileInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import java.nio.file.Path;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class FileInfo {
private final Path filePath;
private PosixFileAttributes fileAttributes;

public FileInfo(Path filePath) {
public FileInfo(Path filePath) {
this.filePath = filePath;
try {
this.fileAttributes = Files.getFileAttributeView(filePath, PosixFileAttributeView.class).readAttributes();
Expand All @@ -24,20 +25,29 @@ public FileInfo(Path filePath) {
}

public String getPermissions() {
// TODO: dynamically get the colors;
var RColor = new TextColor.RGB(253, 188, 75);
var WColor = new TextColor.RGB(192, 57, 43);
var XColor = new TextColor.RGB(147, 154, 89);
var color = new TextColor.RGB(61, 174, 233);
var hithonColor = new TextColor.RGB(124, 124, 124);

var sb = new StringBuilder();
sb.append(new ColoredString(Files.isDirectory(filePath) ? "d" : Files.isSymbolicLink(filePath) ? "l" : ".",
new TextColor.RGB(61, 174, 233)));
var hithon = new ColoredString("-", new TextColor.RGB(124, 124, 124));

sb.append(new ColoredString(isDirectory() ? "d" : isSymlink() ? "l" : ".", color));

var hithon = new ColoredString("-", hithonColor);

ArrayList<String> permissions = new ArrayList<>();
if (fileAttributes != null) {
fileAttributes.permissions().forEach(permission -> {
permissions.add(permission.name());
});
}

var R = new ColoredString("r", new TextColor.RGB(253, 188, 75));
var W = new ColoredString("w", new TextColor.RGB(192, 57, 43));
var X = new ColoredString("x", new TextColor.RGB(147, 154, 89));
var R = new ColoredString("r", RColor);
var W = new ColoredString("w", WColor);
var X = new ColoredString("x", XColor);

sb.append(permissions.contains("OWNER_READ") ? R : hithon)
.append(permissions.contains("OWNER_WRITE") ? W : hithon)
Expand All @@ -53,92 +63,109 @@ public String getPermissions() {
return sb.toString();
}

private Object[] calculateUnitNSize(float size) {
var unit = "B";
if (size >= 1024) {
size = size / 1024; // Convert to KB
unit = "KB";

if (size >= 1024) {
size = size / 1024; // Convert to MB
unit = "MB";

if (size >= 1024) {
size = size / 1024; // Convert to GB
unit = "GB";

if (size >= 1024) {
size = size / 1024; // Convert to TB
unit = "TB";
}
}
}
}

return new Object[]{unit, size};
}

public String getSize(boolean humanReadable, boolean withColor) {
var str = new ColoredString("-", new TextColor.RGB(124, 124, 124));
var strColor = new TextColor.RGB(124, 124, 124);
var str = new ColoredString("-", strColor);

if (!Files.isDirectory(filePath)) {
var size = (float) (fileAttributes != null ? fileAttributes.size() : 0);
var unit = "B";

var color = new TextColor.RGB(28, 108, 117);

if (humanReadable) {
size = size / 1024; // Convert to KB
unit = "KB";
if (size > 1024) {
size = size / 1024; // Convert to MB
unit = "MB";
if (size > 1024) {
size = size / 1024; // Convert to GB
unit = "GB";
if (size > 1024) {
size = size / 1024; // Convert to TB
unit = "TB";
}
}
}
str = new ColoredString(String.format("%.2f %s", size, unit),
new TextColor.RGB(28, 108, 117));
var calculation = calculateUnitNSize(size);
unit = (String) calculation[0];
size = (float) calculation[1];

str = new ColoredString(String.format("%.2f %s", size, unit), color);
} else {
str = new ColoredString(String.format("%d %s", (int)size, unit),
new TextColor.RGB(28, 108, 117));
str = new ColoredString(String.format("%d %s", (int) size, unit), color);
}
}
if (withColor) {
return str.toString();
} else {
return str.getNormalString();
}

return withColor ? str.toString() : str.getNormalString();
}

public String getName() {
return getName(false);
}

public String getName(boolean withColor) {
var strName = filePath.getFileName().toString();
if (withColor) {
return new ColoredString(strName, new TextColor.RGB(150, 153, 91)).toString();
} else {
return strName;
}

// TODO: dynamically get the colors;
var color = new TextColor.RGB(150, 153, 91);

return withColor ? new ColoredString(strName, color).toString() : strName;
}

public String getCreationTime(boolean withColor) {
var str = new ColoredString(
new java.text.SimpleDateFormat("MMM d HH:mm")
.format(
fileAttributes != null ? fileAttributes.creationTime().toMillis() : System.currentTimeMillis()),
new TextColor.RGB(29, 153, 243));
if (withColor) {
return str.toString();
} else {
return str.getNormalString();
}
// TODO: dynamically get the colors;
var color = new TextColor.RGB(29, 153, 243);

// TODO: Simplify
String date = new SimpleDateFormat("MMM d HH:mm")
.format(
fileAttributes != null ? fileAttributes.creationTime().toMillis() : System.currentTimeMillis()
);

var str = new ColoredString(date, color);

return withColor ? str.toString() : str.getNormalString();
}

public Icon getIcon(boolean withColor) {
Icon icon = Icon.getCorrectIcon(getName(false));
if (icon == null) {
if (fileAttributes != null && fileAttributes.isDirectory()) {
icon = Icon.DIR;
} else {
icon = Icon.FILE;
}
}
return icon;
Icon icon = Icon.getCorrectIcon(getName());

if (icon != null) return icon;

return isDirectory() ? Icon.DIR : Icon.FILE;
}

public String getOwner(boolean withColor) {
var str = new ColoredString( fileAttributes != null ? fileAttributes.owner().getName() : "----",
new TextColor.RGB(240, 179, 73));
if (withColor) {
return str.toString();
} else {
return str.getNormalString();
}
// TODO: dynamically get the colors;
var color = new TextColor.RGB(240, 179, 73);
var str = new ColoredString(
fileAttributes != null ? fileAttributes.owner().getName() : "----", color
);

return withColor ? str.toString() : str.getNormalString();
}

public String getGroup(boolean withColor) {
var str = new ColoredString( fileAttributes != null ? fileAttributes.group().getName() : "------",
new TextColor.RGB(240, 179, 73));
if (withColor) {
return str.toString();
} else {
return str.getNormalString();
}
// TODO: dynamically get the colors;
var color = new TextColor.RGB(240, 179, 73);
var str = new ColoredString(
fileAttributes != null ? fileAttributes.group().getName() : "------", color
);

return withColor ? str.toString() : str.getNormalString();
}

public PosixFileAttributes getFileAttributes() {
Expand All @@ -153,20 +180,24 @@ public boolean isSymlink() {
return Files.isSymbolicLink(filePath);
}

public String getSymlinkTarget() {
return getSymlinkTarget(false);
}

public String getSymlinkTarget(boolean withColors) {
if (isSymlink()) {
try {
var target = Files.readSymbolicLink(filePath);
if (withColors) {
return new ColoredString(target.toFile().getName(),
new TextColor.RGB(240, 179, 73)).toString();
} else {
return target.toFile().getName();
}
} catch (IOException e) {
return "";
}
} else {

if (!isSymlink()) return "";

try {
var target = Files.readSymbolicLink(filePath);

// TODO: dynamically get the colors;
var color = new TextColor.RGB(240, 179, 73);

return withColors ? new ColoredString(target.toFile().getName(), color).toString() : target.toFile().getName();

} catch (IOException e) {
// TODO: Log the error;
return "";
}
}
Expand Down
Loading

0 comments on commit 069f7f9

Please sign in to comment.