title: golang screen printing formatted data with color attributes
body: <p>Want to print a row/column table that is formatted as in a typical
  fmt.Printf(&quot;%5s  %5s  %5s\n&quot;,col1, col2, col3)</p>
  <p>Works fine of course if the strings are plain text, but if a string has display attributes
  like color, bold, font - even though the visible data is the same length as the plain text,
  and would be fine in %5s; doing len(col1) is much longer it skews the table alignment.</p>
  <p>Is there a way for Printf to accomplish this, or another std Go package?</p>
  <p>Want:</p>
  <pre><code>Item    Item   Item
  =====   =====  ====
  abc     defgh  xyz
  x       abc    d
  vv      xxxxx                 zz      &lt;=== this happens if string xxxxx has display attributes from
                                              fatih,gchalk, etc. to set foreground/background color
  </code></pre>
  <p>`</p>
  <p>//</p>
  <p>package main</p>
  <p>import (
  &quot;fmt&quot;
  &quot;github.com/jwalton/gchalk&quot;
  &quot;github.com/fatih/color&quot;
  )</p>
  <p>func main() {</p>
  <pre><code>var colorWithGchalk = gchalk.Red
  var data = []string{&quot;one&quot;, &quot;ten&quot;, &quot;twenty&quot;}
  
  gchalk.SetLevel(gchalk.LevelAnsi16m) // seems needed for gitbash
  
  // note output columns framed by &lt;&gt; just to see actual width
  
  fmt.Println(&quot;desired formatted output&quot;)
  fmt.Printf(&quot;&lt;%-10s&gt;  &lt;%-10s&gt;  &lt;%-10s&gt;\n\n&quot;, data[0],data[1],data[2])
  
  /*
  ** gchalk
  */
  // first try using gchalk for color
  // colorize second column - column width ignored?
  fmt.Println(&quot;colorized field loses its 10 character width, so subsequent fields now misaligned&quot;)
  fmt.Printf(&quot;&lt;%-10s&gt;  &lt;%-10s&gt;  &lt;%-10s&gt;\n&quot;, data[0], colorWithGchalk(data[1]), data[2])
  
  // same as above but eliminate gchalk function and just apply colorizing directly - same result
  fmt.Printf(&quot;&lt;%-10s&gt;  &lt;%-10s&gt;  &lt;%-10s&gt;\n&quot;, data[0], gchalk.Red(data[1]), data[2])
  
  /*
  ** fatih
  */
  
  fmt.Println(&quot;\nwith fatih&quot;)
  var colorWithFatih = color.New(color.FgRed).SprintFunc()
  fmt.Printf(&quot;&lt;%-10s&gt;  &lt;%-10s&gt;  &lt;%-10s&gt;\n&quot;, data[0], colorWithFatih(data[1]), data[2])
  </code></pre>
  <p>}
  `</p>
  <p>Output:
  `
  desired formatted output
      </p>
  <p>colorized field loses its 10 character width,
  so subsequent fields now misaligned
      
      </p>
  <p>with fatih
      </p>
  <p>`
  On screen the above  3 lines display the word &quot;ten&quot; in red as desired, but the field is no longer 10 wide.</p>