[X2Go-Commits] [x2goclient] 07/12: help.{cpp, h}: provide string splitting logic as separate split_long_line() function. Use this in pretty_print().

git-admin at x2go.org git-admin at x2go.org
Fri Apr 24 01:32:17 CEST 2015


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2goclient.

commit bf9294c223e2ed31dfd3571d4d1a1fe8c2bda1d2
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Fri Apr 24 00:37:20 2015 +0200

    help.{cpp,h}: provide string splitting logic as separate split_long_line() function. Use this in pretty_print().
---
 debian/changelog |    2 ++
 src/help.cpp     |   62 ++++++++++++++++++++++++++++--------------------------
 src/help.h       |   13 ++++++++++++
 3 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 7bb0bce..dbe7eaf 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -303,6 +303,8 @@ x2goclient (4.0.4.0-0x2go1) UNRELEASED; urgency=low
     - helpdialog.cpp: lower font size to have more space for text.
     - ui/helpdialog.ui: almost double the window size.
     - help.cpp: typo fix for --git-info.
+    - help.{cpp,h}: provide string splitting logic as separate
+      split_long_line() function. Use this in pretty_print().
 
   [ Fernando Pedemonte ]
   * New upstream release (4.0.4.0):
diff --git a/src/help.cpp b/src/help.cpp
index bd0a4a0..78bef9e 100644
--- a/src/help.cpp
+++ b/src/help.cpp
@@ -167,6 +167,24 @@ QString help::pretty_print () {
   return (help::pretty_print (help::build_data ()));
 }
 
+help::string_split_t help::split_long_line (QString &line, std::ptrdiff_t max_length) {
+  string_split_t ret (line, "");
+
+  if (line.size () > max_length) {
+    /* Try to find the next split point. */
+    std::ptrdiff_t split_point = line.lastIndexOf (" ", max_length - 1);
+
+    /* Only care for valid split points. */
+    if (0 <= split_point) {
+      x2goDebug << "Split onto " << line.left (split_point) << " and new part " << line.mid (split_point + 1);
+      ret.first = line.left (split_point);
+      ret.second = line.mid (split_point + 1);
+    }
+  }
+
+  return (ret);
+}
+
 QString help::pretty_print (help::data_t data) {
   QString ret = "";
   QTextStream out (&ret);
@@ -238,36 +256,20 @@ QString help::pretty_print (help::data_t data) {
           cur_len = working_copy.size ();
           x2goDebug << "Trying to fit a (remaining) description " << cur_len << " characters wide." << endl;
 
-          /* Fits onto the current line. Great! */
-          if (remaining > static_cast<std::ptrdiff_t> (cur_len)) {
-            x2goDebug << "Fit onto the current line. Done." << endl;
-            out << working_copy;
-            working_copy = "";
-          }
-          else {
-            /* Try to find the next split point. */
-            std::ptrdiff_t split_point = working_copy.lastIndexOf (" ", remaining - 1);
-
-            if (-1 == split_point) {
-              /* No split point available. Just print it out and hope for better times... */
-              out << working_copy;
-              working_copy = "";
-            }
-            else {
-              /* Yay, we can split. */
-              x2goDebug << "Split onto " << working_copy.left (split_point);
-              out << working_copy.left (split_point);
-
-              x2goDebug << " and new part " << working_copy.mid (split_point + 1);
-              working_copy = working_copy.mid (split_point + 1);
-
-              /* Do the next chunk, if there are remaining characters. */
-              if (!working_copy.isEmpty ()) {
-                out << "\n";
-                indent = terminal_cols - remaining;
-                out << QString (" ").repeated (indent);
-              }
-            }
+          string_split_t string_split = split_long_line (working_copy, remaining);
+          working_copy = string_split.first;
+
+          /* Print potentially splitted line. */
+          out << working_copy;
+
+          /* Continue with next chunk. */
+          working_copy = string_split.second;;
+
+          /* Print whitespace if the remainder string is non-empty. */
+          if (!working_copy.isEmpty ()) {
+            out << "\n";
+            indent = terminal_cols - remaining;
+            out << QString (" ").repeated (indent);
           }
         }
       }
diff --git a/src/help.h b/src/help.h
index 69852b3..b6bfb21 100644
--- a/src/help.h
+++ b/src/help.h
@@ -23,12 +23,14 @@
 #include <QPair>
 #include <QStringList>
 #include <QTextStream>
+#include <cstddef>
 
 namespace help {
   typedef QStringList prelude_t;
   typedef QPair<QString, QString> params_elem_t;
   typedef QList<params_elem_t> params_t;
   typedef QPair<prelude_t, params_t> data_t;
+  typedef QPair<QString, QString> string_split_t;
 
   /* Builds a prelude_t object. Values are hardcoded here. */
   prelude_t build_prelude ();
@@ -43,6 +45,17 @@ namespace help {
   prelude_t cleanup_prelude (prelude_t prelude);
   params_t cleanup_params (params_t params);
 
+  /*
+   * Splits a string into two component.
+   * The first component is at most max_length (or 200) characters long.
+   * The string is split on the nearest space surrounding max_length
+   * (or max_length itself.)
+   * Caveat: if the string length is less than max_length, no splitting
+   * is performed.
+   * In that case, the second component is empty.
+   */
+  string_split_t split_long_line (QString &line, std::ptrdiff_t max_length = 200);
+
   /* Prints a help_data_t structure. */
   QString pretty_print ();
   QString pretty_print (data_t data);

--
Alioth's /srv/git/code.x2go.org/x2goclient.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goclient.git


More information about the x2go-commits mailing list