summaryrefslogtreecommitdiff
path: root/vendor/github.com/juju/ansiterm/context.go
blob: e61a867ffff254c04ebf2e36b3fe6079c6bc0dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package ansiterm

import (
	"fmt"
	"io"
)

// Context provides a way to specify both foreground and background colors
// along with other styles and write text to a Writer with those colors and
// styles.
type Context struct {
	Foreground Color
	Background Color
	Styles     []Style
}

// Foreground is a convenience function that creates a Context with the
// specified color as the foreground color.
func Foreground(color Color) *Context {
	return &Context{Foreground: color}
}

// Background is a convenience function that creates a Context with the
// specified color as the background color.
func Background(color Color) *Context {
	return &Context{Background: color}
}

// Styles is a convenience function that creates a Context with the
// specified styles set.
func Styles(styles ...Style) *Context {
	return &Context{Styles: styles}
}

// SetForeground sets the foreground to the specified color.
func (c *Context) SetForeground(color Color) *Context {
	c.Foreground = color
	return c
}

// SetBackground sets the background to the specified color.
func (c *Context) SetBackground(color Color) *Context {
	c.Background = color
	return c
}

// SetStyle replaces the styles with the new values.
func (c *Context) SetStyle(styles ...Style) *Context {
	c.Styles = styles
	return c
}

type sgrWriter interface {
	io.Writer
	writeSGR(value sgr)
}

// Fprintf will set the sgr values of the writer to the specified
// foreground, background and styles, then write the formatted string,
// then reset the writer.
func (c *Context) Fprintf(w sgrWriter, format string, args ...interface{}) {
	w.writeSGR(c)
	fmt.Fprintf(w, format, args...)
	w.writeSGR(reset)
}

// Fprint will set the sgr values of the writer to the specified foreground,
// background and styles, then formats using the default formats for its
// operands and writes to w. Spaces are added between operands when neither is
// a string. It returns the number of bytes written and any write error
// encountered.
func (c *Context) Fprint(w sgrWriter, args ...interface{}) {
	w.writeSGR(c)
	fmt.Fprint(w, args...)
	w.writeSGR(reset)
}

func (c *Context) sgr() string {
	var values attributes
	if foreground := c.Foreground.foreground(); foreground != unknownAttribute {
		values = append(values, foreground)
	}
	if background := c.Background.background(); background != unknownAttribute {
		values = append(values, background)
	}
	for _, style := range c.Styles {
		if value := style.enable(); value != unknownAttribute {
			values = append(values, value)
		}
	}
	return values.sgr()
}