Ticket #12091: 0001-improve-option_column_width-calculation.patch

File 0001-improve-option_column_width-calculation.patch, 3.2 KB (added by Timo Weingärtner <timo@…>, 7 years ago)
  • src/options_description.cpp

    From 6b0a4ac2053c0c55157e872eb1eef292ccdbfd6c Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Timo=20Weing=C3=A4rtner?= <timo@tiwe.de>
    Date: Wed, 20 Apr 2016 17:07:22 +0200
    Subject: [PATCH] improve option_column_width calculation
    
    * also have two spaces if option takes an argument
    * don't widen the column if the option would need to have it's description
      on a separate line anyway
    
    Fixes #12091.
    ---
     src/options_description.cpp | 36 ++++++++++++++++++++++++------------
     1 file changed, 24 insertions(+), 12 deletions(-)
    
    diff --git a/src/options_description.cpp b/src/options_description.cpp
    index 6592a5d..b145222 100644
    a b namespace boost { namespace program_options {  
    577577                        unsigned first_column_width, unsigned line_length)
    578578        {
    579579            stringstream ss;
    580             ss << "  " << opt.format_name() << ' ' << opt.format_parameter();
     580            ss << "  " << opt.format_name();
     581            string const param(opt.format_parameter());
     582            if (!param.empty())
     583                ss << ' ' << param;
     584            /* two spaces to improve readability */
     585            ss << "  ";
    581586           
    582587            // Don't use ss.rdbuf() since g++ 2.96 is buggy on it.
    583588            os << ss.str();
    namespace boost { namespace program_options {  
    608613    options_description::get_option_column_width() const                               
    609614    {
    610615        /* Find the maximum width of the option column */
    611         unsigned width(23);
     616        unsigned width(24);
     617        /* this is the column were description should start */
     618        unsigned const max_width(m_line_length - m_min_description_length);
     619
    612620        unsigned i; // vc6 has broken for loop scoping
    613621        for (i = 0; i < m_options.size(); ++i)
    614622        {
    615623            const option_description& opt = *m_options[i];
    616624            stringstream ss;
    617             ss << "  " << opt.format_name() << ' ' << opt.format_parameter();
    618             width = (max)(width, static_cast<unsigned>(ss.str().size()));           
     625            ss << "  " << opt.format_name();
     626
     627            string const param(opt.format_parameter());
     628            if (!param.empty())
     629                ss << ' ' << param;
     630
     631            /* two spaces to improve readability */
     632            ss << "  ";
     633
     634            unsigned const new_width(static_cast<unsigned>(ss.str().size()));
     635
     636            /* only widen if this option doesn't need a separate desciption line anyway */
     637            if (new_width <= max_width)
     638                width = (max)(width, new_width);
    619639        }
    620640
    621641        /* Get width of groups as well*/
    622642        for (unsigned j = 0; j < groups.size(); ++j)                           
    623643            width = max(width, groups[j]->get_option_column_width());
    624644
    625         /* this is the column were description should start, if first
    626            column is longer, we go to a new line */
    627         const unsigned start_of_description_column = m_line_length - m_min_description_length;
    628 
    629         width = (min)(width, start_of_description_column-1);
    630        
    631         /* add an additional space to improve readability */
    632         ++width;
    633645        return width;                                                       
    634646    }
    635647