| |   |
| 23 | 23 | import java.io.File; |
| 24 | 24 | import java.io.IOException; |
| 25 | 25 | import java.io.PrintWriter; |
| import java.text.DecimalFormat; |
| 26 | 27 | import java.text.SimpleDateFormat; |
| 27 | 28 | import java.util.Arrays; |
| 28 | 29 | import java.util.Comparator; |
| … | … | |
| 32 | 32 | import org.jnode.shell.AbstractCommand; |
| 33 | 33 | import org.jnode.shell.syntax.Argument; |
| 34 | 34 | import org.jnode.shell.syntax.FileArgument; |
| import org.jnode.shell.syntax.FlagArgument; |
| 35 | 36 | |
| 36 | 37 | /** |
| 37 | 38 | * @author epr |
| … | … | |
| 46 | 46 | private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm"); |
| 47 | 47 | |
| 48 | 48 | private static final String help_path = "the file or directory to list"; |
| private static final String help_humanReadable = "with -l, print sizes in human readable format (e.g., 1K 234M 2G)"; |
| 49 | 50 | private static final String help_super = "List files or directories"; |
| 50 | 51 | private static final String fmt_no_path = "No such path: %s%n"; |
| |
|
| 52 | 53 | private final FileArgument argPath; |
| |
| private final FlagArgument humanReadableArg; |
|
| 54 | 56 | public DirCommand() { |
| 55 | 57 | super(help_super); |
| humanReadableArg = new FlagArgument("humanReadable", Argument.OPTIONAL, help_humanReadable); |
| 56 | 59 | argPath = new FileArgument("path", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, help_path); |
| registerArguments(argPath); |
| registerArguments(argPath, humanReadableArg); |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | 63 | public static void main(String[] args) throws Exception { |
| … | … | |
| 107 | 107 | sb.setLength(0); |
| 108 | 108 | lastModified.setTime(f.lastModified()); |
| 109 | 109 | if (f.isFile()) { |
| String ln = String.valueOf(f.length()).concat("B"); |
| String ln = formatSize(f.length()); |
| 111 | 111 | int cnt = LEFT_MARGIN - ln.length(); |
| for (int j = 0; j < cnt; j++, sb.append(' ')) |
| ; |
| for (int j = 0; j < cnt; j++) sb.append(' '); |
| 114 | 113 | sb.append(ln); |
| 115 | 114 | sb.append(" "); |
| 116 | 115 | sb.append(df.format(lastModified)); |
| … | … | |
| 128 | 128 | } |
| 129 | 129 | out.println(); |
| 130 | 130 | } |
| } |
|
| private static final String[] units = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"}; |
|
| protected String formatSize(double bytes) { |
| if (humanReadableArg.isSet()) { |
| int index; |
| for (index = 0; bytes >= 1024; index++) bytes = bytes / 1024; |
| DecimalFormat df = new DecimalFormat("###0.0"); |
| return df.format(bytes) + units[index]; |
| } else |
| return bytes + "B"; |
| 131 | 143 | } |
| 132 | 144 | } |