PageRenderTime 149ms CodeModel.GetById 136ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/commands/dir.java

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