Text formatting with smart crown margins

Suraj N. Kurapati

  1. Problem
    1. Solution
      1. Usage

        Problem

        When composing plain text files, I like to let each paragraph occupy an entire line because I do not enjoy manually formatting my paragraphs. Instead, I let the computer format and beautify my work – a task so fitting for one who never tires of mundane, repetitive misery. ;-)

        In particular, I’ve come to prefer the fmt -c command because it preserves indentation of paragraphs. However, the command does not respect the crown margins of lists and bullet points very well. For example, consider the following:

        $ cat foo
            *   This line is intentionally long, in order to illustrate the effects of word wrapping. In any other situation, it would be unbearably terse, not unlike the barren core of a black hole.  Or so I think...
        
        $ fmt -w 70 -c foo
            *   This line is intentionally long, in order to illustrate the
            effects of word wrapping. In any other situation, it would be
            unbearably terse, not unlike the barren core of a black hole.
            Or so I think...
        

        Notice how the first word of the second line (“effects”) is vertically aligned with the asterisk immediately above it rather than being vertically aligned with the first word of the first sentence (“This”). This arrangement hinders quick scanning of text and also makes the text appear unpolished.

        Solution

        To solve the problem, I wrote a small tool in Ruby that properly accommodates lists and bullet points. For instance, observe how this tool handles the previous example:

        $ wrap-text -w 70 < foo
            *   This line is intentionally long, in order to illustrate the
                effects of word wrapping. In any other situation, it would be
                unbearably terse, not unlike the barren core of a black hole.
                Or so I think...
        

        In addition, this tool also accommodates crowns composed purely of whitespace. For example:

        $ tr -d \* < foo | wrap-text -w 70
               This line is intentionally long, in order to illustrate the
               effects of word wrapping. In any other situation, it would be
               unbearably terse, not unlike the barren core of a black hole.
               Or so I think...
        

        Usage

        Usage: wrap-text [options]
            -h, --help                       show this help message
            -w, --width WIDTH                maximum line width
                                             (default: 80)
            -s, --split-only                 split long lines, but do not refill
                                             (default: false)
            -2, --two-space                  two spaces after sentences
                                             (default: false)
            -b, --balance                    prefer equal line widths
                                             (default: false)
            -m, --select REGEXP              only wrap lines that match REGEXP
                                             (default: //)
            -M, --reject REGEXP              do not wrap lines that match REGEXP
                                             (default: nil)