This is an automated email from the git hooks/post-receive script. x2go pushed a change to branch bugfix/help-cmd in repository x2goclient. from b8088ba help.cpp: populate pretty_print: fetch max length of params. new d9d7fbe help.cpp: get terminal window sizes on UNIX-based and Windows operating systems. new 6f55b84 help.cpp: complete pretty printer function. The 2 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Summary of changes: debian/changelog | 3 ++ src/help.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 2 deletions(-) -- Alioth's /srv/git/code.x2go.org/x2goclient.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goclient.git
This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch bugfix/help-cmd in repository x2goclient. commit d9d7fbe6bdb09396991e5aff91e2babb21dbfd3d Author: Mihai Moldovan <ionic@ionic.de> Date: Wed Apr 22 08:24:59 2015 +0200 help.cpp: get terminal window sizes on UNIX-based and Windows operating systems. --- debian/changelog | 2 ++ src/help.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 987f88f..ddbc5d0 100644 --- a/debian/changelog +++ b/debian/changelog @@ -261,6 +261,8 @@ x2goclient (4.0.4.0-0x2go1) UNRELEASED; urgency=low - help.{cpp,h}: add skeleton for new help system. - help.{cpp,h}: add sanitizing helpers. - help.cpp: populate pretty_print: fetch max length of params. + - help.cpp: get terminal window sizes on UNIX-based and Windows operating + systems. [ Fernando Pedemonte ] * New upstream release (4.0.4.0): diff --git a/src/help.cpp b/src/help.cpp index 5201c12..831ad63 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -20,6 +20,15 @@ #include <QCoreApplication> #include <QtDebug> #include <cstddef> + +/* For terminal size. */ +#ifdef Q_OS_WIN +#include <windows.h> +#elif defined (Q_OS_UNIX) +#include <stdio.h> +#include <sys/ioctl.h> +#endif + #include "help.h" #include "version.h" @@ -89,5 +98,25 @@ void help::pretty_print (help::data_t data) { max_len = std::max (max_len, (*it).first.length ()); } - std::size_t indent = 0; + std::size_t terminal_cols = 0; + +#ifdef Q_OS_WIN + CONSOLE_SCREEN_BUFFER_INFO terminal_internal; + HANDLE stderr_handle = GetStdHandle (STD_ERROR_HANDLE); + if (stderr_handle && (stderr_handle != INVALID_HANDLE_VALUE)) { + if (GetConsoleScreenBufferInfo (stderr_handle, &terminal_internal)) { + terminal_cols = (terminal_internal.srWindow.Right - terminal_internal.Left) + 1; + } + } +#elif defined (Q_OS_UNIX) + struct winsize terminal_internal; + ioctl (0, TIOCGWINSZ, &terminal_internal); + terminal_cols = terminal_internal.ws_col; +#endif + + for (help::params_t::const_iterator it = data.second.constBegin (); it != data.second.constEnd (); ++it) { + out << " "; + out << (*it).first; + out << QString (" ").repeated (max_len - (*it.length) + 4); + } } -- Alioth's /srv/git/code.x2go.org/x2goclient.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goclient.git
This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch bugfix/help-cmd in repository x2goclient. commit 6f55b84e55f78785b26a0b72d32b73250e19a7f5 Author: Mihai Moldovan <ionic@ionic.de> Date: Wed Apr 22 09:31:37 2015 +0200 help.cpp: complete pretty printer function. --- debian/changelog | 1 + src/help.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index ddbc5d0..59502eb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -263,6 +263,7 @@ x2goclient (4.0.4.0-0x2go1) UNRELEASED; urgency=low - help.cpp: populate pretty_print: fetch max length of params. - help.cpp: get terminal window sizes on UNIX-based and Windows operating systems. + - help.cpp: complete pretty printer function. [ Fernando Pedemonte ] * New upstream release (4.0.4.0): diff --git a/src/help.cpp b/src/help.cpp index 831ad63..8ef5811 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -95,7 +95,7 @@ void help::pretty_print (help::data_t data) { /* Iterate over all parameter options and get max width. */ for (help::params_t::const_iterator it = data.second.constBegin (); it != data.second.constEnd (); ++it) { - max_len = std::max (max_len, (*it).first.length ()); + max_len = std::max (max_len, (*it).first.size ()); } std::size_t terminal_cols = 0; @@ -115,8 +115,69 @@ void help::pretty_print (help::data_t data) { #endif for (help::params_t::const_iterator it = data.second.constBegin (); it != data.second.constEnd (); ++it) { + std::size_t indent = (max_len - (*it).first.size ()) + 4; out << " "; out << (*it).first; - out << QString (" ").repeated (max_len - (*it.length) + 4); + out << QString (" ").repeated (indent); + + indent += 2; + std::ptrdiff_t remaining = 0; + std::size_t cur_len = (*it).second.size (); + if (0 != terminal_cols) { + remaining = terminal_cols - indent; + + /* Ran out of space? That's bad... print a newline and don't use any indentation level. */ + if (0 > remaining) { + out << "\n"; + remaining = terminal_cols; + indent = 0; + } + + QString working_copy ((*it).second); + + do { + cur_len = working_copy.size (); + + /* Fits onto the current line. Great! */ + if (remaining > cur_len) { + out << working_copy; + } + else { + /* Try to find the next split point. */ + std::ptrdiff_t split_point_white = working_copy.lastIndexOf (" ", remaining); + std::ptrdiff_t split_point_hyphen = working_copy.lastIndexOf ("-", remaining); + std::ptrdiff_t split_point = std::max (split_point_white, split_point_hyphen); + + 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. */ + out << working_copy.left (split_point); + + /* If we split at a hyphen, don't lose it. */ + if (working_copy.at (split_point).compare ("-") == 0) { + out << "-" + } + + working_copy = working_copy.mid (split_point); + + /* Do the next chunk, if there are remaining characters. */ + if (!working_copy.isEmpty ()) { + out << "\n" + out << QString (" ").repeated (indent); + } + } + } + } while (!working_copy.isEmpty ()); + } + else { + /* No idea what the terminal size is. Just print it all onto one line. */ + out << (*it).second; + } + + out << "\n"; } } -- Alioth's /srv/git/code.x2go.org/x2goclient.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/x2goclient.git