Home > Net >  golang tabwriter printing junk and not formatting properly
golang tabwriter printing junk and not formatting properly

Time:02-18

I'm trying to print a help message using tabwriter:

func printHelp() {
    writer := tabwriter.NewWriter(os.Stdout, 100, 8, 1, ' ', 0)
    fmt.Println(writer, "uni outputs Unicode information for characters.")
    fmt.Println(writer, "USAGE:")
    fmt.Println(writer, "  uni <input>\tOutputs Unicode info for the input.\t")
    fmt.Println(writer, "\tProcesses U xxxx sequences into the appropriate characters and treats control characters as control.\t")
    fmt.Println(writer, "  uni raw <input>\tOutputs Unicode info on the raw unporocessed input.\t")
    fmt.Println(writer, "  uni decomp <input\tOutputs the input with all characters decomposed.\t")
    fmt.Println(writer, "  uni comp <input>\tOutputs the input with all characters composed.\t")
    fmt.Println(writer, "  uni short <input>\tOutputs basic info about the input, one character per line.\t")
    fmt.Println(writer, "  uni rawshort <input>\tOutputs basic info about the raw unprocessed input, one character per line.\t")
    fmt.Println(writer, "  uni help\tPrints this help message.\t")
    writer.Flush()
}

However the output both contains a significant amount of internal junk that shouldn't be printed and isn't aligning the columns correctly.

Here's what I get:

&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} uni outputs Unicode information for characters.
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} USAGE:
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni <input>    Outputs Unicode info for the input. 
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}  Processes U xxxx sequences into the appropriate characters and treats control characters as control.    
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni raw <input>    Outputs Unicode info on the raw unporocessed input. 
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni decomp <input  Outputs the input with all characters decomposed.   
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni comp <input>   Outputs the input with all characters composed. 
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni short <input>  Outputs basic info about the input, one character per line. 
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni rawshort <input>   Outputs basic info about the raw unprocessed input, one character per line. 
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni help   Prints this help message.   

and here's roughly what I want

uni outputs Unicode information for characters.
USAGE:
  uni <input>           Outputs Unicode info for the input.
                        Processes U xxxx sequences into the appropriate characters and treats control characters as control.
  uni raw <input>       Outputs Unicdoe info on the raw unporocessed input.
  uni decomp <input     Outputs the input with all characters decomposed.
  uni comp <input>      Outputs the input with all characters composed.
  uni short <input>     Outputs basic info about the input, one character per line.
  uni rawshort <input>  Outputs basic info about the raw unprocessed input, one character per line.
  uni help              Prints this help message.

I've tried all changing the minimum cell width to all sorts of values, from 0 to multiple hundreds, and it changes the alignment but not to the point of working correctly. I've tried tab vs space separators and Right Alignment vs default (left) alignment with no differences.

CodePudding user response:

To write to an io.Writer use fmt.Fprintln (or fmt.Fprintf) instead of fmt.Println:

// fmt.Println(writer, "uni outputs Unicode information for characters.")

fmt.Fprintln(writer, "uni outputs Unicode information for characters.")

fmt.Println will do its best to just render each argument - hence why you are seeing the tabwriter's internal reflected state.

  •  Tags:  
  • go
  • Related