github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/text/tabwriter/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 tabwriter_test 6 7 import ( 8 "github.com/shogo82148/std/fmt" 9 "github.com/shogo82148/std/os" 10 "github.com/shogo82148/std/text/tabwriter" 11 ) 12 13 func ExampleWriter_Init() { 14 w := new(tabwriter.Writer) 15 16 // タブストップ8でタブ区切りの列にフォーマットします。 17 w.Init(os.Stdout, 0, 8, 0, '\t', 0) 18 fmt.Fprintln(w, "a\tb\tc\td\t.") 19 fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.") 20 fmt.Fprintln(w) 21 w.Flush() 22 23 // 最小幅5のスペースで区切られた列に右揃えでフォーマットします。 24 // そして少なくとも1つの空白のパディング(よって、より広い列のエントリーが 25 // お互いに触れないようにします)。 26 w.Init(os.Stdout, 5, 0, 1, ' ', tabwriter.AlignRight) 27 fmt.Fprintln(w, "a\tb\tc\td\t.") 28 fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.") 29 fmt.Fprintln(w) 30 w.Flush() 31 32 // output: 33 // a b c d . 34 // 123 12345 1234567 123456789 . 35 // 36 // a b c d. 37 // 123 12345 1234567 123456789. 38 } 39 40 func Example_elastic() { 41 // bとdがどちらも各行の2番目のセルに表示されているにもかかわらず、 42 // 異なる列に属していることに注目してください。 43 w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, '.', tabwriter.AlignRight|tabwriter.Debug) 44 fmt.Fprintln(w, "a\tb\tc") 45 fmt.Fprintln(w, "aa\tbb\tcc") 46 fmt.Fprintln(w, "aaa\t") // trailing tab 47 fmt.Fprintln(w, "aaaa\tdddd\teeee") 48 w.Flush() 49 50 // output: 51 // ....a|..b|c 52 // ...aa|.bb|cc 53 // ..aaa| 54 // .aaaa|.dddd|eeee 55 } 56 57 func Example_trailingTab() { 58 // 3行目には末尾のタブがないことに注意してください。 59 // したがって、その最終セルは整列した列の一部ではありません。 60 const padding = 3 61 w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, '-', tabwriter.AlignRight|tabwriter.Debug) 62 fmt.Fprintln(w, "a\tb\taligned\t") 63 fmt.Fprintln(w, "aa\tbb\taligned\t") 64 fmt.Fprintln(w, "aaa\tbbb\tunaligned") // 末尾にタブがない 65 fmt.Fprintln(w, "aaaa\tbbbb\taligned\t") 66 w.Flush() 67 68 // output: 69 // ------a|------b|---aligned| 70 // -----aa|-----bb|---aligned| 71 // ----aaa|----bbb|unaligned 72 // ---aaaa|---bbbb|---aligned| 73 }