Advertisement
Guest User

Todo

a guest
Jul 4th, 2010
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.72 KB | None | 0 0
  1. /**
  2.  *Some little program  for finding and prettyprinting //TODO comments.
  3.  *
  4.  *License: the MIT-License also known as X11-License
  5.  *Copyright: (c) 2010 Marcel Fischer
  6.  */
  7.  
  8. module todo;
  9.  
  10. import std.string : strip, chomp, find, ifind, format;
  11. import std.stream : BufferedFile;
  12. import std.stdio;
  13. static import std.file;
  14. static import std.path;
  15.  
  16.  
  17. //I use string only for pseudo immutables for compa with D2
  18.  
  19. //private char[][string] todos;
  20. private char[] todos="";
  21. private char[] highPrioTodos="";
  22. private int count = 0;
  23. private char[] exts = "";
  24.  
  25. private string versionnum = "0.1";
  26. debug private string versiontext= "BETA DEBUG";
  27. else  private string versiontext= "BETA";
  28.  
  29. /*
  30. //Use std.path.sep instead
  31. version(Windows) { private const string seperator = "\\";}
  32. version(Linux)   { private const string seperator = "/"; }
  33. static assert(is(typeof(seperator)));
  34. */
  35. alias std.path.sep seperator;
  36.  
  37. void main(string[] args) {  
  38.   showInit();
  39.   standardExts();
  40.   debug writefln("StExts: %s", exts);
  41.   if(args.length <= 1) {
  42.    
  43.   } else {
  44.     foreach(arg; args[1..$]) {
  45.       if(!std.file.exists(arg)) {
  46.         writefln("%s doesn't exist!",arg);
  47.         continue;
  48.       }
  49.       search(arg);
  50.     }
  51.     printResults();
  52.   }
  53. }
  54.  
  55. void showInit() {
  56.   writefln("Todo written by Marcel Fischer");
  57.   writefln("Version %s %s\n", versionnum, versiontext);
  58. }
  59.  
  60. void standardExts() {
  61.   exts ~= "{d}{di}{c}{cpp}{h}{hpp}{cxx}{py}{rb}{pl}{php}{php5}{cs}{java}{tex}";
  62. }
  63.  
  64. void showHelp() {
  65.   //TODO write showHelp() ;-)
  66. }
  67.  
  68. void search(string path) {
  69.   //The path has to exist
  70.   assert(std.file.exists(path));
  71.   debug writef("\nSearching in %s...",path);
  72.  
  73.   //When it's a directory
  74.   if(std.file.isdir(path)) {
  75.     debug writefln(" it's directory");
  76.    
  77.     //recursively seach in subdirectorys
  78.     auto entrys = std.file.listdir(path);
  79.     foreach(entry; entrys) {
  80.       if(entry != "." && entry != "..")
  81.         search(path ~  seperator ~ entry);
  82.     }
  83.    
  84.    
  85.   //when it's a file
  86.   } else if (std.file.isfile(path)){
  87.     debug writefln(" it's file");
  88.     //if file is not a programming file
  89.     if(! find(exts, format("{%s}", std.path.getExt(path)) ) >= 0) {
  90.       debug writef("ignore: %s", format("{%s}", std.path.getExt(path)));
  91.       return;
  92.     }
  93.  
  94.     BufferedFile file = new BufferedFile(path);
  95.     scope(exit) file.close();
  96.     char[] line = "";      //the current line
  97.     int linenum = 0;       //linenumber
  98.     bool highPrio = false; //if high priority
  99.    
  100.     while(!file.eof()) {
  101.       line = file.readLine();
  102.       linenum++;
  103.       debug writef(" %s ",linenum);
  104.       highPrio = false;
  105.      
  106.       int index = ifind(line, "//TODO");
  107.       if(index >= 0) {
  108.         //Take the line after todo
  109.         line = line[index+("//TODO".length)..$];
  110.         line = strip(chomp(line));
  111.         if(line[0] == "$"[0]) {
  112.           highPrio = true;
  113.           line = line[1..$];
  114.         }
  115.        
  116.         if(!highPrio)
  117.           todos ~= createMessage(path,linenum,line);
  118.         else
  119.           highPrioTodos ~= createMessage(path,linenum,line);
  120.         count++;
  121.       }
  122.     }
  123.   } else {
  124.     assert(0); //File can't be neither a directory nor a file
  125.   }
  126. }
  127.  
  128.  
  129. string createMessage(string path, int linenum, string message) {
  130.   return format("  * %15s(%s): %s\n", std.path.getBaseName(path),
  131.                 linenum, message);
  132. }
  133.  
  134. void printResults() {
  135.   writefln("%s Results found\n",count);
  136.   if(highPrioTodos.length > 0)
  137.     writefln("Todos with high priority:\n%s\n",highPrioTodos);
  138.   if(todos.length > 0)
  139.     writefln("%sodos:\n%s\n", highPrioTodos.length > 0 ? "Other t" : "T", todos);
  140.   if(highPrioTodos.length == 0 && todos.length == 0)
  141.     writefln("Whoa, nothing left to do!");
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement