PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. @method void dir( [ String dirname ] )
  6. */
  7. package bsh.commands;
  8. import java.io.*;
  9. import bsh.*;
  10. import java.util.Date;
  11. import java.util.Vector;
  12. import java.util.GregorianCalendar;
  13. import java.util.Calendar;
  14. public class dir
  15. {
  16. static final String [] months = { "Jan", "Feb", "Mar", "Apr",
  17. "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  18. public static String usage() {
  19. return "usage: dir( String dir )\n dir()";
  20. }
  21. /**
  22. Implement dir() command.
  23. */
  24. public static void invoke( Interpreter env, CallStack callstack )
  25. {
  26. String dir = ".";
  27. invoke( env, callstack, dir );
  28. }
  29. /**
  30. Implement dir( String directory ) command.
  31. */
  32. public static void invoke(
  33. Interpreter env, CallStack callstack, String dir )
  34. {
  35. File file;
  36. try {
  37. file = env.pathToFile( dir );
  38. } catch (IOException e ) {
  39. env.println("error reading path: "+e);
  40. return;
  41. }
  42. if ( !file.exists() || !file.canRead() ) {
  43. env.println( "Can't read " + file );
  44. return;
  45. }
  46. if ( !file.isDirectory() ) {
  47. env.println("'"+dir+"' is not a directory");
  48. }
  49. String [] files = file.list();
  50. files = StringUtil.bubbleSort(files);
  51. for( int i=0; i< files.length; i++ ) {
  52. File f = new File( dir + File.separator + files[i] );
  53. StringBuffer sb = new StringBuffer();
  54. sb.append( f.canRead() ? "r": "-" );
  55. sb.append( f.canWrite() ? "w": "-" );
  56. sb.append( "_" );
  57. sb.append( " ");
  58. Date d = new Date(f.lastModified());
  59. GregorianCalendar c = new GregorianCalendar();
  60. c.setTime(d);
  61. int day = c.get(Calendar.DAY_OF_MONTH);
  62. sb.append( months[ c.get(Calendar.MONTH) ] + " " + day );
  63. if ( day < 10 )
  64. sb.append(" ");
  65. sb.append(" ");
  66. // hack to get fixed length 'length' field
  67. int fieldlen = 8;
  68. StringBuffer len = new StringBuffer();
  69. for(int j=0; j<fieldlen; j++)
  70. len.append(" ");
  71. len.insert(0, f.length());
  72. len.setLength(fieldlen);
  73. // hack to move the spaces to the front
  74. int si = len.toString().indexOf(" ");
  75. if ( si != -1 ) {
  76. String pad = len.toString().substring(si);
  77. len.setLength(si);
  78. len.insert(0, pad);
  79. }
  80. sb.append( len.toString() );
  81. sb.append( " " + f.getName() );
  82. if ( f.isDirectory() )
  83. sb.append("/");
  84. env.println( sb.toString() );
  85. }
  86. }
  87. }