thor: long_desc: force keeping spaces and not break lines

Hello,

I have a long_desc method like this:

long_desc <<-LONGDESC.gsub("\n", "\x5")
  Examples:

  bin/ruby-rails-documentations                             \\
    generate ~/ruby-rails-documentations                    \\
    --sdoc-dir=~/sdoc --ruby-dir=~/ruby --rails-dir=~/rails \\
    --ruby-versions=1.9.3p484 2.0.0p353 2.1.0               \\
    --rails-versions=3.0.20 3.1.12 3.2.16 4.0.2 4.1.0.beta1
LONGDESC
def generate(output_dir)
  ...
end

Executing ruby-rails-documentations help generate I want my output to be like this:

Examples:

bin/ruby-rails-documentations                             \
  generate ~/ruby-rails-documentations                    \
  --sdoc-dir=~/sdoc --ruby-dir=~/ruby --rails-dir=~/rails \
  --ruby-versions=1.9.3p484 2.0.0p353 2.1.0               \
  --rails-versions=3.0.20 3.1.12 3.2.16 4.0.2 4.1.0.beta1

Instead, I get this:

Examples:

bin/ruby-rails-documentations \
 generate ~/ruby-rails-documentations \
 --sdoc-dir=~/sdoc --ruby-dir=~/ruby --rails-dir=~/rails \
 --ruby-versions=1.9.3p484 2.0.0p353 2.1.0 \
 --rails-versions=3.0.20 3.1.12 
3.2.16 4.0.2 4.1.0.beta1

The merged spaces is not a big problem, but the added newlines is - they break the command syntax, and I can’t predict where they will be.

Is there any way to force keeping spaces and, above all, force to not break lines?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 7
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

+1

Finding it impossible to get my long_desc displayed correctly. Line-wraps are sporadic once I use \x5.

For my sanity, I’ve simply redefined Basic#print_wrapped in my project to output the long_desc as-is.

class Thor
  module Shell
    class Basic
      def print_wrapped(message, options = {})
         stdout.puts message
       end
    end
  end
end

+1

Finding it impossible to get my long_desc displayed correctly. Line-wraps are sporadic once I use \x5.

For my sanity, I’ve simply redefined Basic#print_wrapped in my project to output the long_desc as-is.

class Thor
  module Shell
    class Basic
      def print_wrapped(message, options = {})
         stdout.puts message
       end
    end
  end
end

Thanks for that, @meissadia. Though it was a little too simplistic for me, given a need for indented multi-line output, so I expanded on it slightly:

# Fix for https://github.com/erikhuda/thor/issues/398
class Thor::Shell::Basic
  def print_wrapped(message, options = {})
    indent = (options[:indent] || 0).to_i
    if indent.zero?
      self.stdout.puts message
    else
      message.each_line do |message_line|
        self.stdout.print ' ' * indent
        self.stdout.puts message_line.chomp
      end
    end
  end
end # Thor::Shell::Basic

Experiencing issues with long_desc line breaks as well. When formatting documentation, it is essential that the author have control over the way that the text will break. This is especially true for console applications, where an incorrect line break can cause unrecoverable, or even destructive, errors.

It looks like the long description is being printed using the Shell::Basic#print_wrapped method, which is a pretty basic text wrapping implementation. Given the likelihood that long descriptions will contain code examples, would the maintainers be open to changing the method used to render the long description?