简体   繁体   中英

golang screen printing formatted data with color attributes

Want to print a row/column table that is formatted as in a typical fmt.Printf("%5s %5s %5s\n",col1, col2, col3)

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.

Is there a way for Printf to accomplish this, or another std Go package?

Want:

Item    Item   Item
=====   =====  ====
abc     defgh  xyz
x       abc    d
vv      xxxxx                 zz      <=== this happens if string xxxxx has display attributes from
                                            fatih,gchalk, etc. to set foreground/background color

`

//

package main

import ( "fmt" "github.com/jwalton/gchalk" "github.com/fatih/color" )

func main() {

var colorWithGchalk = gchalk.Red
var data = []string{"one", "ten", "twenty"}

gchalk.SetLevel(gchalk.LevelAnsi16m) // seems needed for gitbash

// note output columns framed by <> just to see actual width

fmt.Println("desired formatted output")
fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n\n", data[0],data[1],data[2])

/*
** gchalk
*/
// first try using gchalk for color
// colorize second column - column width ignored?
fmt.Println("colorized field loses its 10 character width, so subsequent fields now misaligned")
fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], colorWithGchalk(data[1]), data[2])

// same as above but eliminate gchalk function and just apply colorizing directly - same result
fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], gchalk.Red(data[1]), data[2])

/*
** fatih
*/

fmt.Println("\nwith fatih")
var colorWithFatih = color.New(color.FgRed).SprintFunc()
fmt.Printf("<%-10s>  <%-10s>  <%-10s>\n", data[0], colorWithFatih(data[1]), data[2])

} `

Output: ` desired formatted output

colorized field loses its 10 character width, so subsequent fields now misaligned

with fatih

` On screen the above 3 lines display the word "ten" in red as desired, but the field is no longer 10 wide.

Is there a way for Printf to accomplish this,

No

or another std Go package?

No

(What you call "display attributes" are part of the output stream of bytes, they are not "attributes", this is "inline data" interpreted by the terminal emulator. What you can do is filter out this inline data before printing.)

You could use https://github.com/olekukonko/tablewriter as an example for how to output tables or just use the package.

With the advice of Jason Walton the porter of chalk to gchalk. I got fmt.Printf %s to satisfy my need, although their may be issues if the field widths (%s) were narrow.

I wanted to concatenate at least two strings together to provide to one %s. The first string was plain text (sgCharToPrint) the next string was colorized so it was the actual screen text (missedRaw) (missed was the color string eg missedRaw wrapped with ansi formatting characters.

myLen = len(sgCharToPrint) + len(missedRaw)

padded = sgCharToPrint + missed + strings.Repeat(" ", 30 - olen)

fmt.Printf("%30s %4d %10s \n",padded, value, tail)

Now the "table" display stays in alignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM