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