[Aptitude-devel] r3023 - in branches/aptitude-0.3/aptitude: . src

Daniel Burrows dburrows@costa.debian.org
Thu, 21 Apr 2005 03:12:14 +0000


Author: dburrows
Date: Thu Apr 21 03:12:11 2005
New Revision: 3023

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/desc_parse.cc
Log:
Start working on automagic bullet detection.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Thu Apr 21 03:12:11 2005
@@ -2,6 +2,11 @@
 
 	* src/desc_parse.cc:
 
+	  Initial implementation of automagic bullet detection.
+	  Buggy.
+
+	* src/desc_parse.cc:
+
 	  Rework make_desc_fragment to add theoretical support for
 	  indentation levels in the input description.  No change should
 	  occur in the resulting text as a result of this modification.

Modified: branches/aptitude-0.3/aptitude/src/desc_parse.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/desc_parse.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/desc_parse.cc	Thu Apr 21 03:12:11 2005
@@ -8,6 +8,7 @@
 #include "ui.h"
 
 #include <vscreen/fragment.h>
+#include <vscreen/config/colors.h>
 
 using namespace std;
 
@@ -30,31 +31,49 @@
  * corresponding to it.
  *
  *  \param desc the string from which the description should be built.
+ *  \param level how many list indent levels have been entered; used
+ *  to choose bullet style.
+ *
  *  \param indent the number of spaces to strip from the left-hand
  *  side of each line.  If a line with less than this many spaces is
  *  encountered, start is placed at the beginning of the line and we
  *  return.
  *
  *  \param start the current location in the string; will be updated
- *  to reflect how many characters were consumed.
+ *  to reflect how many characters were consumed.  This should be
+ *  placed after the "nspaces" indentation -- but it will be updated
+ *  to be placed at the beginning of a line (0 indentation).
+ *
+ *  \return the new fragment.
  */
 static fragment *make_level_fragment(string desc,
+				     unsigned int level,
 				     string::size_type indent,
 				     string::size_type &start)
 {
   vector<fragment*> fragments;
+  bool first=true;
 
   while(start<desc.size())
     {
       string::size_type loc=start;
-      unsigned int nspaces=0;
+      unsigned int nspaces;
 
-      while(loc<desc.size() && desc[loc]==' ' && nspaces<indent)
+      if(first)
+	nspaces=indent;
+      else
 	{
-	  ++loc;
-	  ++nspaces;
+	  nspaces=0;
+
+	  while(loc<desc.size() && desc[loc]==' ' && nspaces<indent)
+	    {
+	      ++loc;
+	      ++nspaces;
+	    }
 	}
 
+      first=false;
+
       if(nspaces<indent)
 	break;
 
@@ -64,19 +83,48 @@
 	  {
 	    ++loc;
 
-	    int amt=0;
-	    while(loc+amt<desc.size() && desc[loc+amt]!='\n')
-	      ++amt;
+	    if(loc<desc.size() &&
+	       (desc[loc] == '+' ||
+		desc[loc] == '-' ||
+		desc[loc] == '*'))
+	      {
+		// Start a list item (i.e., an indented region).
+
+		string bullet;
+		bullet+=("*+-"[level%3]);
+		bullet+=" ";
+
+		start=loc+1;
+
+		fragment *item_contents=make_level_fragment(desc,
+							    level+1,
+							    nspaces+3,
+							    start);
+
+		fragments.push_back(text_fragment(bullet,
+						  get_color("Bullet")));
+		fragments.push_back(indentbox(nspaces,
+					      nspaces+2,
+					      fragf("%s%F",
+						    bullet.c_str(),
+						    item_contents)));
+	      }
+	    else
+	      {  
+		int amt=0;
+		while(loc+amt<desc.size() && desc[loc+amt]!='\n')
+		  ++amt;
 
-	    // Hard-wrap AS REQUIRED BY POLICY.
-	    fragments.push_back(hardwrapbox(text_fragment(string(desc, loc, amt))));
+		// Hard-wrap AS REQUIRED BY POLICY.
+		fragments.push_back(hardwrapbox(text_fragment(string(desc, loc, amt))));
 
-	    loc+=amt;
-	    if(loc<desc.size())
-	      ++loc;
+		loc+=amt;
+		if(loc<desc.size())
+		  ++loc;
 
-	    start=loc;
-	    break;
+		start=loc;
+		break;
+	      }
 	  }
 	case '.':
 	  // Add a blank line (ignore the rest of the line)
@@ -158,6 +206,10 @@
   if(loc<desc.size()) // Skip the '\n'
     ++loc;
 
-  // Note that the starting indentation level is 1...
-  return make_level_fragment(desc, 1, loc);
+  // Skip leading whitespace on the first line if there is any.
+  if(loc<desc.size() && desc[loc] == ' ')
+    ++loc;
+
+  // Note that the starting amount of indentation is 1...
+  return make_level_fragment(desc, 0, 1, loc);
 }