cwrap: Wrap comments. By line. Which... is all this does.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Benjamin Shelton 2847b5bcef Missing imports post-split. 2 months ago
LICENSE Added LICENSE and README. 2 months ago Added LICENSE and README. 2 months ago
wrapper.go Missing imports post-split. 2 months ago

cwrap: Wrap lines. Really.

cwrap, intended as a truncation of "comment wrap" and often typoed as "crap," is intended as a solution for generators that produce inline comments (e.g. go-toml) but make no effort to wrap long lines of comments themselves.

With cwrap (again, not crap), you can use it in place of your standard io.Writer, and it'll handle wrapping comments for you!

Note: cwrap does not wrap comments that begin after a line of code. It only wraps comments that appear alone on a line themselves preceded with zero or more whitespace characters.


Here's a sample application demonstrating c(w)rap usage.

package main

import (


type config struct {
    Value string `toml:"value" comment:"This is a long comment that should find itself wrapped at the 80th column. Wrapping occurs after the TOML has been generated by pelletier/go-toml and before it is written to disk."`

    Section struct {
        Value string `toml:"value" comment:"Here is another long comment that will also find itself wrapped at the 80th column. As with the previous comment, this will be wrapped after pass-through via cwrap (not crap!)."`
    } `toml:"header" comment:"Sample section header."`

func main() {
    fp, err := os.OpenFile(filepath.Join(basePath, "sample.toml"),
            os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0644))
    if err != nil {
    defer fp.Close()

    wrapper := cwrap.New(fp)

    // Instruct the TOML encoder to use cwrap instead.
    enc := toml.NewEncoder(wrapper)
    enc.Indentation("    ")

    if err = enc.Encode(&config{}); err != nil {



As with many of my other projects this is released under the liberal NCSA license alongside any documentation. This may be used in commercial products.