01: /**
02: Display the contents of the current working directory.
03: The format is similar to the Unix ls -l
04: <em>This is an example of a bsh command written in Java for speed.</em>
05:
06: @method void dir( [ String dirname ] )
07: */package org.gjt.sp.jedit.bsh.commands;
08:
09: import java.io.*;
10: import org.gjt.sp.jedit.bsh.*;
11: import java.util.Date;
12: import java.util.GregorianCalendar;
13: import java.util.Calendar;
14:
15: public class dir {
16: static final String[] months = { "Jan", "Feb", "Mar", "Apr", "May",
17: "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
18:
19: public static String usage() {
20: return "usage: dir( String dir )\n dir()";
21: }
22:
23: /**
24: Implement dir() command.
25: */
26: public static void invoke(Interpreter env, CallStack callstack) {
27: String dir = ".";
28: invoke(env, callstack, dir);
29: }
30:
31: /**
32: Implement dir( String directory ) command.
33: */
34: public static void invoke(Interpreter env, CallStack callstack,
35: String dir) {
36: File file;
37: try {
38: file = env.pathToFile(dir);
39: } catch (IOException e) {
40: env.println("error reading path: " + e);
41: return;
42: }
43:
44: if (!file.exists() || !file.canRead()) {
45: env.println("Can't read " + file);
46: return;
47: }
48: if (!file.isDirectory()) {
49: env.println("'" + dir + "' is not a directory");
50: }
51:
52: String[] files = file.list();
53: files = StringUtil.bubbleSort(files);
54:
55: for (int i = 0; i < files.length; i++) {
56: File f = new File(dir + File.separator + files[i]);
57: StringBuffer sb = new StringBuffer();
58: sb.append(f.canRead() ? "r" : "-");
59: sb.append(f.canWrite() ? "w" : "-");
60: sb.append("_");
61: sb.append(" ");
62:
63: Date d = new Date(f.lastModified());
64: GregorianCalendar c = new GregorianCalendar();
65: c.setTime(d);
66: int day = c.get(Calendar.DAY_OF_MONTH);
67: sb.append(months[c.get(Calendar.MONTH)] + " " + day);
68: if (day < 10)
69: sb.append(" ");
70:
71: sb.append(" ");
72:
73: // hack to get fixed length 'length' field
74: int fieldlen = 8;
75: StringBuffer len = new StringBuffer();
76: for (int j = 0; j < fieldlen; j++)
77: len.append(" ");
78: len.insert(0, f.length());
79: len.setLength(fieldlen);
80: // hack to move the spaces to the front
81: int si = len.toString().indexOf(" ");
82: if (si != -1) {
83: String pad = len.toString().substring(si);
84: len.setLength(si);
85: len.insert(0, pad);
86: }
87:
88: sb.append(len.toString());
89:
90: sb.append(" " + f.getName());
91: if (f.isDirectory())
92: sb.append("/");
93:
94: env.println(sb.toString());
95: }
96: }
97: }
|