- added support for prompt help
authorJan Kupec <jkupec@suse.cz>
Sat, 26 Apr 2008 20:40:58 +0000 (20:40 +0000)
committerJan Kupec <jkupec@suse.cz>
Sat, 26 Apr 2008 20:40:58 +0000 (20:40 +0000)
- added help for Continue? [y/n/p/?] prompt

src/output/Out.h
src/output/OutNormal.cc
src/output/OutNormal.h
src/output/OutXML.cc
src/output/OutXML.h
src/zypper-misc.cc
src/zypper-prompt.cc
src/zypper-prompt.h

index 98b23ee..6c0c05d 100644 (file)
@@ -225,6 +225,8 @@ public:
                       const std::string & prompt,
                       const PromptOptions & poptions,
                       const std::string & startdesc = "") = 0;
+  
+  virtual void promptHelp(const PromptOptions & poptions) = 0;
 
 public:
   /** Get current verbosity. */
index 261c270..63b5660 100644 (file)
@@ -235,19 +235,33 @@ void OutNormal::prompt(PromptId id,
                        const PromptOptions & poptions,
                        const std::string & startdesc)
 {
-  string option_str;
-  PromptOptions::OptionList::const_iterator it;
-  if ((it = poptions.options().begin()) != poptions.options().end())
-  {
-    option_str += (poptions.defaultOpt() == 0 ? zypp::str::toUpper(*it) : *it);
-    ++it;
-  }
-  for (unsigned int i = 1; it != poptions.options().end(); ++it, i++)
-    option_str += "/" + (poptions.defaultOpt() == i ? zypp::str::toUpper(*it) : *it);
-
   if (startdesc.empty())
     cout << CLEARLN;
   else
     cout << startdesc << endl;
-  cout << prompt << " [" << option_str << "]: " << std::flush;
+  cout << prompt << " [" << poptions.optionString() << "]: " << std::flush;
+}
+
+void OutNormal::promptHelp(const PromptOptions & poptions)
+{
+  cout << endl;
+  if (poptions.helpEmpty())
+    cout << _("No help available for this prompt.") << endl;
+  else
+  {
+    unsigned int pos = 0;
+    for(PromptOptions::StrVector::const_iterator it = poptions.options().begin();
+        it != poptions.options().end(); ++it, ++pos)
+    {
+      cout << *it << " - ";
+      const string & hs_r = poptions.optionHelp(pos); 
+      if (hs_r.empty())
+        cout << "(" << _("no help available for this option") << ")";
+      else
+        cout << hs_r;
+      cout << endl;
+    }
+  }
+
+  cout << endl << "[" << poptions.optionString() << "]: " << std::flush;
 }
index 2f3a7e7..1285295 100644 (file)
@@ -58,6 +58,8 @@ public:
                       const PromptOptions & poptions,
                       const std::string & startdesc = "");
 
+  virtual void promptHelp(const PromptOptions & poptions);
+
 protected:
   virtual bool mine(Type type);
 
index a59edea..c6f633e 100644 (file)
@@ -197,15 +197,21 @@ void OutXML::prompt(PromptId id,
   cout << "<text>" << xml_encode(prompt) << "</text>" << endl;
 
   unsigned int i = 0;
-  for (PromptOptions::OptionList::const_iterator it = poptions.options().begin();
-       it != poptions.options().end(); ++it, i++)
+  for (PromptOptions::StrVector::const_iterator it = poptions.options().begin();
+       it != poptions.options().end(); ++it, ++i)
   {
     string option = *it;
     cout << "<option";
     if (poptions.defaultOpt() == i)
       cout << " default=\"1\"";
     cout << " value=\"" << xml_encode(option) << "\"";
+    cout << " desc=\"" << xml_encode(poptions.optionHelp(i)) << "\"";
     cout << "/>" << endl;
   }
   cout << "</prompt>" << endl;
 }
+
+void OutXML::promptHelp(const PromptOptions & poptions)
+{
+  // nothing to do here
+}
index 97fd38a..45763e6 100644 (file)
@@ -42,6 +42,8 @@ public:
                       const PromptOptions & poptions,
                       const std::string & startdesc = "");
 
+  virtual void promptHelp(const PromptOptions & poptions);
+
 protected:
   virtual bool mine(Type type);
 
index f5dcca3..a7f5af2 100644 (file)
@@ -2208,8 +2208,11 @@ void solve_and_commit (Zypper & zypper)
         // after install/update command summary if there will be any package
         // to-be-removed automatically to show why, if asked.
         popts.setOptions(_("y/n/p"), 0);
+        popts.setOptionHelp(0, _("Accept the summary and proceed with installation/removal of packages."));
+        popts.setOptionHelp(1, _("Cancel the operation."));
+        popts.setOptionHelp(2, _("Restart solver in no-force-resolution mode in order to show dependency problems."));
         // translators: Translate 'p' to whathever you translated it in the y/n/p prompt text.
-        string prompt_text = _("Continue? (Choose 'p' to show dependency problems.)");
+        string prompt_text = _("Continue?");
         zypper.out().prompt(PROMPT_YN_INST_REMOVE_CONTINUE, prompt_text, popts);
         unsigned int reply =
           get_prompt_reply(zypper, PROMPT_YN_INST_REMOVE_CONTINUE, popts);
index 8c7b7a9..9445928 100644 (file)
@@ -40,6 +40,44 @@ void PromptOptions::setOptions(const std::string & option_str, unsigned int defa
     _default = default_opt; 
 }
 
+const string PromptOptions::optionString() const
+{
+  string option_str;
+  StrVector::const_iterator it;
+  if ((it = options().begin()) != options().end())
+  {
+    option_str += (defaultOpt() == 0 ? zypp::str::toUpper(*it) : *it);
+    ++it;
+  }
+  for (unsigned int i = 1; it != options().end(); ++it, i++)
+    option_str += "/" + (defaultOpt() == i ? zypp::str::toUpper(*it) : *it);
+  
+  if (!_opt_help.empty())
+    option_str += "/?";
+
+  return option_str;
+}
+
+void PromptOptions::setOptionHelp(unsigned int opt, const std::string & help_str)
+{
+  if (help_str.empty())
+    return;
+
+  if (opt >= _options.size())
+  {
+    WAR << "attempt to set option help for non-existing option."
+        << " text: " << help_str << endl;
+    return;
+  }
+
+  if (opt >= _opt_help.capacity())
+    _opt_help.reserve(_options.size());
+  if (opt >= _opt_help.size())
+    _opt_help.resize(_options.size());
+
+  _opt_help[opt] = help_str;
+}
+
 // ----------------------------------------------------------------------------
 
 const std::string ari_mapping[] = { string(_("abort")),string(_("retry")),string(_("ignore"))};
@@ -54,7 +92,7 @@ int read_action_ari_with_timeout (PromptId pid, unsigned timeout,
     default_action = 0;
   }
 
-  out.info (_("Abort,retry, ignore?\n"));
+  out.info (_("Abort, retry, ignore?\n"));
   
   //FIXME XML output
   cout << endl;
@@ -162,6 +200,12 @@ unsigned int get_prompt_reply(Zypper & zypper,
     if (reply.empty())
       break;
 
+    if (reply == "?")
+    {
+      zypper.out().promptHelp(poptions);
+      continue;
+    }
+
     if (is_yn_prompt && rpmatch(reply.c_str()) >= 0)
     {
       if (rpmatch(reply.c_str()))
@@ -172,12 +216,12 @@ unsigned int get_prompt_reply(Zypper & zypper,
     }
     else
     {
+      DBG << " reply: " << reply << " (" << zypp::str::toLower(reply) << " lowercase)" << endl;
       bool got_valid_reply = false;
       for (unsigned int i = 0; i < poptions.options().size(); i++)
       {
         DBG << "index: " << i << " option: "
-            << poptions.options()[i] << " reply: " << reply
-            << " (" << zypp::str::toLower(reply) << " lowercase)" << endl; 
+            << poptions.options()[i] << endl; 
         if (poptions.options()[i] == zypp::str::toLower(reply))
         {
           reply_int = i;
index e775b60..c0f239d 100644 (file)
@@ -7,7 +7,7 @@
 class PromptOptions
 {
 public:
-  typedef std::vector<std::string> OptionList;
+  typedef std::vector<std::string> StrVector;
 
   /**
    * Default c-tor.
@@ -27,13 +27,21 @@ public:
   /** D-tor */
   ~PromptOptions();
 
-  const OptionList & options() const { return _options; }
+  const StrVector & options() const { return _options; }
   void setOptions(const std::string & option_str, unsigned int default_opt);
   unsigned int defaultOpt() const { return _default; }
+  const std::string optionString() const;
+
+  const std::string optionHelp(unsigned int opt) const
+  { static std::string empty; return opt < _opt_help.size() ? _opt_help[opt] : empty; } 
+  //const std::string getOptionHelp(const std::string & opt_str);
+  void setOptionHelp(unsigned int opt, const std::string & help_str);
+  bool helpEmpty() const { return _opt_help.empty(); }
 
 private:
-  OptionList _options;
+  StrVector _options;
   unsigned int _default;
+  StrVector _opt_help;
 };