implement --full in apt search
authorDavid Kalnischkies <david@kalnischkies.de>
Tue, 2 Sep 2014 12:32:48 +0000 (14:32 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Sun, 7 Sep 2014 17:27:10 +0000 (19:27 +0200)
apt-private/private-list.cc
apt-private/private-output.cc
apt-private/private-output.h
apt-private/private-search.cc
test/integration/test-apt-cli-search

index b690021..d746acf 100644 (file)
@@ -85,15 +85,14 @@ private:
 };
                                                                        /*}}}*/
 static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
-                     pkgCache::PkgIterator P,    
-                     std::ostream &outs,
-                     bool include_summary=true)
+                     pkgCache::PkgIterator const &P, std::ostream &outs,
+                     std::string const &format)
 {
    for (pkgCache::VerIterator Ver = P.VersionList();
         Ver.end() == false; ++Ver)
    {
-      ListSingleVersion(CacheFile, records, Ver, outs, include_summary);
-      outs << "\n";
+      ListSingleVersion(CacheFile, records, Ver, outs, format);
+      outs << std::endl;
    }
 }
                                                                        /*}}}*/
@@ -117,10 +116,9 @@ bool DoList(CommandLine &Cmd)
       patterns = Cmd.FileList + 1;
    }
 
-   std::map<std::string, std::string> output_map;
-   std::map<std::string, std::string>::const_iterator K;
-
-   bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary");
+   std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
+   if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
+      format += "\n  ${Description}\n";
 
    PackageNameMatcher matcher(patterns);
    LocalitySortedVersionSet bag;
@@ -130,36 +128,33 @@ bool DoList(CommandLine &Cmd)
                             Cache->Head().PackageCount,
                             _("Listing"));
    GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
-   bool ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
+   bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
+   std::map<std::string, std::string> output_map;
    for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
    {
       std::stringstream outs;
       if(ShowAllVersions == true)
-      {
-         ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary);
-         output_map.insert(std::make_pair<std::string, std::string>(
-            V.ParentPkg().Name(), outs.str()));
-      } else {
-         ListSingleVersion(CacheFile, records, V, outs, includeSummary);
-         output_map.insert(std::make_pair<std::string, std::string>(
-                           V.ParentPkg().Name(), outs.str()));
-      }
+         ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
+      else
+         ListSingleVersion(CacheFile, records, V, outs, format);
+      output_map.insert(std::make_pair<std::string, std::string>(
+              V.ParentPkg().Name(), outs.str()));
    }
 
    // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
    // output the sorted map
+   std::map<std::string, std::string>::const_iterator K;
    for (K = output_map.begin(); K != output_map.end(); ++K)
       std::cout << (*K).second << std::endl;
 
-
    // be nice and tell the user if there is more to see
    if (bag.size() == 1 && ShowAllVersions == false)
    {
       // start with -1 as we already displayed one version
       int versions = -1;
       pkgCache::VerIterator Ver = *bag.begin();
-      for ( ; Ver.end() == false; Ver++)
-         versions++;
+      for ( ; Ver.end() == false; ++Ver)
+         ++versions;
       if (versions > 0)
          _error->Notice(P_("There is %i additional version. Please use the '-a' switch to see it", "There are %i additional versions. Please use the '-a' switch to see them.", versions), versions);
    }
index 7be56de..fc76a05 100644 (file)
@@ -207,9 +207,31 @@ static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &reco
    return ShortDescription;
 }
                                                                        /*}}}*/
+static std::string GetLongDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
+{
+   pkgPolicy *policy = CacheFile.GetPolicy();
+
+   pkgCache::VerIterator ver;
+   if (P->CurrentVer != 0)
+      ver = P.CurrentVer();
+   else
+      ver = policy->GetCandidateVer(P);
+
+   std::string const EmptyDescription = "(none)";
+   if(ver.end() == true)
+      return EmptyDescription;
+
+   pkgCache::DescIterator const Desc = ver.TranslatedDescription();
+   pkgRecords::Parser & parser = records.Lookup(Desc.FileList());
+   std::string const longdesc = parser.LongDesc();
+   if (longdesc.empty() == true)
+      return EmptyDescription;
+   return SubstVar(longdesc, "\n ", "\n  ");
+}
+                                                                       /*}}}*/
 void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records,   /*{{{*/
-                       pkgCache::VerIterator V, std::ostream &out,
-                       bool include_summary)
+                       pkgCache::VerIterator const &V, std::ostream &out,
+                       std::string const &format)
 {
    pkgCache::PkgIterator const P = V.ParentPkg();
    pkgDepCache * const DepCache = CacheFile.GetDepCache();
@@ -219,11 +241,7 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records,       /*{{{*/
    if (_config->FindB("APT::Cmd::use-format", false))
       output = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}");
    else
-   {
-      // linux-kernel/unstable version [installed,upradable to: new-version]
-      //    description
-      output = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture} ${apt:Status}";
-   }
+      output = format;
 
    // FIXME: some of these names are really icky – and all is nowhere documented
    output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P));
@@ -264,14 +282,13 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records,      /*{{{*/
    output = SubstVar(output, "${apt:Status}", StatusStr);
    output = SubstVar(output, "${color:highlight}", _config->Find("APT::Color::Highlight", ""));
    output = SubstVar(output, "${color:neutral}", _config->Find("APT::Color::Neutral", ""));
-
-   output = APT::String::Strip(output);
-   if (_config->FindB("APT::Cmd::use-format", false) == false)
-   {
-      if (include_summary)
-        output += "\n  ${Description}\n";
-   }
    output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P));
+   output = SubstVar(output, "${LongDescription}", GetLongDescription(CacheFile, records, P));
+   output = SubstVar(output, "${ }${ }", "${ }");
+   output = SubstVar(output, "${ }\n", "\n");
+   output = SubstVar(output, "${ }", " ");
+   if (APT::String::Endswith(output, " ") == true)
+      output.erase(output.length() - 1);
 
    out << output;
 }
index 6f3a964..e0dc9bf 100644 (file)
@@ -23,8 +23,8 @@ APT_PUBLIC extern unsigned int ScreenWidth;
 APT_PUBLIC bool InitOutput();
 
 void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records,
-                       pkgCache::VerIterator V, std::ostream &out,
-                       bool include_summary=true);
+                       pkgCache::VerIterator const &V, std::ostream &out,
+                       std::string const &format);
 
 
 // helper to describe global state
index 2d427fa..5e12902 100644 (file)
@@ -69,6 +69,13 @@ bool FullTextSearch(CommandLine &CmdL)                                       /*{{{*/
    progress.OverallProgress(50, 100, 50,  _("Full Text Search"));
    progress.SubProgress(bag.size());
    pkgRecords records(CacheFile);
+
+   std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n";
+   if (_config->FindB("APT::Cache::ShowFull",false) == false)
+      format += "  ${Description}\n";
+   else
+      format += "  ${LongDescription}\n";
+
    int Done = 0;
    for ( ;V != bag.end(); ++V)
    {
@@ -100,7 +107,7 @@ bool FullTextSearch(CommandLine &CmdL)                                      /*{{{*/
       if (all_found == true)
       {
             std::stringstream outs;
-            ListSingleVersion(CacheFile, records, V, outs);
+            ListSingleVersion(CacheFile, records, V, outs, format);
             output_map.insert(std::make_pair<std::string, std::string>(
                                  PkgName, outs.str()));
       }
index 3ac0749..8f009d5 100755 (executable)
@@ -15,7 +15,10 @@ fi
 
 DESCR='Some description that has a unusual word xxyyzz and aabbcc and a UPPERCASE'
 DESCR2='Some other description with the unusual aabbcc only'
-insertpackage 'unstable' 'foo' 'all' '1.0' '' '' "$DESCR"
+insertpackage 'unstable' 'foo' 'all' '1.0' '' '' "$DESCR
+ Long description of stuff and such, with lines
+ .
+ and paragraphs and everything."
 insertpackage 'testing' 'bar' 'i386' '2.0' '' '' "$DESCR2"
 
 setupaptarchive
@@ -59,6 +62,17 @@ testequal "foo/unstable 1.0 all
   $DESCR
 " apt search -qq 'up[pP]erc[Aa]se'
 
+# search is done in the long description
+testequal "foo/unstable 1.0 all
+  $DESCR
+" apt search -qq 'long description'
+testequal "foo/unstable 1.0 all
+  $DESCR
+  Long description of stuff and such, with lines
+  .
+  and paragraphs and everything.
+" apt search --full -qq 'long description'
+
 # output is sorted and search word finds both package
 testequal "bar/testing 2.0 i386
   $DESCR2