github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/columns/formatter/textcolumns/doc.go (about)

     1  // Copyright 2022 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /*
    16  Package textcolumns helps to output structs (and events of structs) using metadata from a `Columns` instance in a
    17  tabular way suitable for consoles or other frontends using fixed-width characters / fonts.
    18  
    19  It can automatically size the output tables according to either screen size or content and provides some helpful tools
    20  to get a consistent output.
    21  
    22  # Initializing
    23  
    24  You can create a new formatter by calling
    25  
    26  	tc := textcolumns.NewFormatter(columnMap)
    27  
    28  You can specify options by adding one or more of the WithX() functions to the initializer.
    29  The [columns.ColumnMap] can be obtained by calling [columns.GetColumpMap] on your `Column` instance.
    30  
    31  # Output
    32  
    33  After you have initialized the formatter, you can use
    34  
    35  	tc.FormatHeader()
    36  
    37  to obtain the header line as string, which will look something like this:
    38  
    39  	NODE                PID COMM             NAME                                 TIME
    40  
    41  You can also pass a filled struct to
    42  
    43  	tc.FormatEntry(&event)
    44  
    45  to get a string like this:
    46  
    47  	Node1                 2 AAA                                                   12ns
    48  
    49  Even simpler, use
    50  
    51  	tc.WriteTable(os.Stdout, entries)
    52  
    53  to directly print the whole table:
    54  
    55  	NODE                PID COMM             NAME                                 TIME
    56  	----------------------------------------------------------------------------------
    57  	Node1                 2 AAA                                                   12ns
    58  	Node1                 1 AAA              Yay                           14.677772ms
    59  	Node1                 2 AnotherComm                                    24.645772ms
    60  	Node2                 4 BBB                                           3.462217772s
    61  	Node2                 3 BBB                                                  333ns
    62  
    63  # Custom Columns
    64  
    65  By default, Columns will show all fields that have a column tag without the `hide` attribute. Using
    66  
    67  	tc.SetShowColumns("node,time")
    68  
    69  you can adjust the output to contain exactly the specified columns.
    70  */
    71  package textcolumns