...

Source file src/github.com/liggitt/tabwriter/example_test.go

Documentation: github.com/liggitt/tabwriter

     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  	"fmt"
     9  	"os"
    10  
    11  	"github.com/liggitt/tabwriter"
    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  }
    75  
    76  func Example_rememberWidth() {
    77  	// Observe that the columns continue to line up after the Flush() call,
    78  	// as long as they are no wider than previously written columns
    79  	const padding = 3
    80  	w := tabwriter.NewWriter(os.Stdout, 4, 6, 3, ' ', tabwriter.RememberWidths|tabwriter.Debug)
    81  	fmt.Fprintln(w, "a\tb\tc")
    82  	fmt.Fprintln(w, "ddd\teeeee\tfff")
    83  	w.Flush()
    84  	fmt.Fprintln(w, "g\thh\tii")
    85  	fmt.Fprintln(w, "jjj\tk\tl")
    86  	w.Flush()
    87  	fmt.Fprintln(w, "mmmmm\tn\to")
    88  	fmt.Fprintln(w, "p\tqqq\trrr")
    89  	w.Flush()
    90  
    91  	// output:
    92  	// a     |b       |c
    93  	// ddd   |eeeee   |fff
    94  	// g     |hh      |ii
    95  	// jjj   |k       |l
    96  	// mmmmm   |n       |o
    97  	// p       |qqq     |rrr
    98  }
    99  

View as plain text