PageRenderTime 61ms CodeModel.GetById 55ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/commands/dir.java

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