github.com/influx6/npkg@v0.8.8/ntext/ntabwriter/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ntabwriter_test 6 7 import ( 8 "fmt" 9 "os" 10 11 tabwriter "github.com/influx6/npkg/ntext/ntabwriter" 12 ) 13 14 func ExampleWriter_Init() { 15 w := new(tabwriter.Writer) 16 17 // Format in tab-separated columns with a tab stop of 8. 18 w.Init(os.Stdout, 0, 8, 0, '\t', 0) 19 fmt.Fprintln(w, "a\tb\tc\td\t.") 20 fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.") 21 fmt.Fprintln(w) 22 w.Flush() 23 24 // Format right-aligned in space-separated columns of minimal width 5 25 // and at least one blank of padding (so wider column entries do not 26 // touch each other). 27 w.Init(os.Stdout, 5, 0, 1, ' ', tabwriter.AlignRight) 28 fmt.Fprintln(w, "a\tb\tc\td\t.") 29 fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.") 30 fmt.Fprintln(w) 31 w.Flush() 32 33 // output: 34 // a b c d . 35 // 123 12345 1234567 123456789 . 36 // 37 // a b c d. 38 // 123 12345 1234567 123456789. 39 } 40 41 func Example_elastic() { 42 // Observe how the b's and the d's, despite appearing in the 43 // second cell of each line, belong to different columns. 44 w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, '.', tabwriter.AlignRight|tabwriter.Debug) 45 fmt.Fprintln(w, "a\tb\tc") 46 fmt.Fprintln(w, "aa\tbb\tcc") 47 fmt.Fprintln(w, "aaa\t") // trailing tab 48 fmt.Fprintln(w, "aaaa\tdddd\teeee") 49 w.Flush() 50 51 // output: 52 // ....a|..b|c 53 // ...aa|.bb|cc 54 // ..aaa| 55 // .aaaa|.dddd|eeee 56 } 57 58 func Example_trailingTab() { 59 // Observe that the third line has no trailing tab, 60 // so its final cell is not part of an aligned column. 61 const padding = 3 62 w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, '-', tabwriter.AlignRight|tabwriter.Debug) 63 fmt.Fprintln(w, "a\tb\taligned\t") 64 fmt.Fprintln(w, "aa\tbb\taligned\t") 65 fmt.Fprintln(w, "aaa\tbbb\tunaligned") // no trailing tab 66 fmt.Fprintln(w, "aaaa\tbbbb\taligned\t") 67 w.Flush() 68 69 // output: 70 // ------a|------b|---aligned| 71 // -----aa|-----bb|---aligned| 72 // ----aaa|----bbb|unaligned 73 // ---aaaa|---bbbb|---aligned| 74 }