github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/output/output.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package output
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/juju/ansiterm"
    11  	"github.com/juju/cmd"
    12  
    13  	"github.com/juju/juju/core/status"
    14  )
    15  
    16  // DefaultFormatters holds the formatters that can be
    17  // specified with the --format flag.
    18  var DefaultFormatters = map[string]cmd.Formatter{
    19  	"yaml": cmd.FormatYaml,
    20  	"json": cmd.FormatJson,
    21  }
    22  
    23  // TabWriter returns a new tab writer with common layout definition.
    24  func TabWriter(writer io.Writer) *ansiterm.TabWriter {
    25  	const (
    26  		// To format things into columns.
    27  		minwidth = 0
    28  		tabwidth = 1
    29  		padding  = 2
    30  		padchar  = ' '
    31  		flags    = 0
    32  	)
    33  	return ansiterm.NewTabWriter(writer, minwidth, tabwidth, padding, padchar, flags)
    34  }
    35  
    36  // Wrapper provides some helper functions for writing values out tab separated.
    37  type Wrapper struct {
    38  	*ansiterm.TabWriter
    39  }
    40  
    41  // Print writes each value followed by a tab.
    42  func (w *Wrapper) Print(values ...interface{}) {
    43  	for _, v := range values {
    44  		fmt.Fprintf(w, "%v\t", v)
    45  	}
    46  }
    47  
    48  // Printf writes the formatted text followed by a tab.
    49  func (w *Wrapper) Printf(format string, values ...interface{}) {
    50  	fmt.Fprintf(w, format+"\t", values...)
    51  }
    52  
    53  // Println writes many tab separated values finished with a new line.
    54  func (w *Wrapper) Println(values ...interface{}) {
    55  	for i, v := range values {
    56  		if i != len(values)-1 {
    57  			fmt.Fprintf(w, "%v\t", v)
    58  		} else {
    59  			fmt.Fprintf(w, "%v", v)
    60  		}
    61  	}
    62  	fmt.Fprintln(w)
    63  }
    64  
    65  // PrintColor writes the value out in the color context specified.
    66  func (w *Wrapper) PrintColor(ctx *ansiterm.Context, value interface{}) {
    67  	if ctx != nil {
    68  		ctx.Fprintf(w.TabWriter, "%v\t", value)
    69  	} else {
    70  		fmt.Fprintf(w, "%v\t", value)
    71  	}
    72  }
    73  
    74  // PrintStatus writes out the status value in the standard color.
    75  func (w *Wrapper) PrintStatus(status status.Status) {
    76  	w.PrintColor(statusColors[status], status)
    77  }
    78  
    79  // CurrentHighlight is the color used to show the current
    80  // controller, user or model in tabular
    81  var CurrentHighlight = ansiterm.Foreground(ansiterm.Green)
    82  
    83  // ErrorHighlight is the color used to show error conditions.
    84  var ErrorHighlight = ansiterm.Foreground(ansiterm.Red)
    85  
    86  // WarningHighlight is the color used to show warning conditions.
    87  // Generally things that the user should be aware of, but not necessarily
    88  // requiring any user action.
    89  var WarningHighlight = ansiterm.Foreground(ansiterm.Yellow)
    90  
    91  // GoodHighlight is used to indicate good or success conditions.
    92  var GoodHighlight = ansiterm.Foreground(ansiterm.Green)
    93  
    94  var statusColors = map[status.Status]*ansiterm.Context{
    95  	// good
    96  	status.Active:    GoodHighlight,
    97  	status.Running:   GoodHighlight,
    98  	status.Idle:      GoodHighlight,
    99  	status.Started:   GoodHighlight,
   100  	status.Executing: GoodHighlight,
   101  	status.Attaching: GoodHighlight,
   102  	status.Attached:  GoodHighlight,
   103  	// busy
   104  	status.Allocating:  WarningHighlight,
   105  	status.Lost:        WarningHighlight,
   106  	status.Maintenance: WarningHighlight,
   107  	status.Pending:     WarningHighlight,
   108  	status.Rebooting:   WarningHighlight,
   109  	status.Stopped:     WarningHighlight,
   110  	status.Unknown:     WarningHighlight,
   111  	status.Detaching:   WarningHighlight,
   112  	status.Detached:    WarningHighlight,
   113  	// bad
   114  	status.Blocked:    ErrorHighlight,
   115  	status.Down:       ErrorHighlight,
   116  	status.Error:      ErrorHighlight,
   117  	status.Failed:     ErrorHighlight,
   118  	status.Terminated: ErrorHighlight,
   119  }