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