aboutsummaryrefslogtreecommitdiff
path: root/cmd/format.go
blob: 993b955dda89ad956277804089c24cce77a9220c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * format.go - Functions for handling output formatting.
 *
 * Copyright 2017 Google Inc.
 * Author: Joe Richey (joerichey@google.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package cmd

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"
	"text/template"
	"unicode/utf8"

	"github.com/google/fscrypt/util"
	"golang.org/x/crypto/ssh/terminal"
)

// Suffixes for questions with a yes or no default
const (
	defaultYesSuffix = "[Y/n]"
	defaultNoSuffix  = "[y/N]"
)

// Variables which control how output is formmatted and where it goes.
var (
	// TabWidth is the number of spaces used to display a tab.
	TabWidth = 8
	// LineLength is the maximum length of any output. If not set, the width
	// of the terminal be detected and assigned to LineLength.
	LineLength int
	// DefaultLineLength is the LineLength we use if we cannot detect the
	// terminal width. By default we fall back to punch cards.
	DefaultLineLength = 80
	// Output is the io.Writer all commands should use for their normal
	// output (errors should just return the appropriate error). If not set,
	// it is automatically set based on the provided flags.
	Output io.Writer
)

// We use the width of the terminal unless we cannot get the width.
func init() {
	if LineLength == 0 {
		var err error
		LineLength, _, err = terminal.GetSize(int(os.Stdout.Fd()))
		if err != nil {
			LineLength = DefaultLineLength
		}
	}
}

// MaxSubcommandLength returns the length of the longest subcommand (where the
// length of the command is Name + Title). Return 0 if there aren't subcommands.
func (c *Command) MaxNameLength() (max int) {
	for _, s := range c.SubCommands {
		max = util.MaxInt(max, len(s.Name))
	}
	return
}

// WrapText wraps an input string so that each line begins with numTabs tabs
// (except the first line) and ends with a newline (except the last line), and
// each line has length less than lineLength. If the text contains a word which
// is too long, that word gets its own line. The first line's calculated length
// is startSpaces less (to account for strange offsets on the first line).
func WrapText(startSpaces, numTabs int, text string) string {
	// We use a buffer to format the wrapped text so we get O(n) runtime
	var buffer bytes.Buffer
	spaceLeft := 0
	maxTextLen := LineLength - startSpaces
	delimiter := strings.Repeat("\t", numTabs)
	for i, word := range strings.Fields(text) {
		wordLen := utf8.RuneCountInString(word)
		if i == 0 {
			buffer.WriteString(word)
			spaceLeft = maxTextLen - wordLen
		} else if wordLen >= spaceLeft {
			// If no room left, write the word on the next line.
			buffer.WriteString("\n")
			buffer.WriteString(delimiter)
			buffer.WriteString(word)
			spaceLeft = maxTextLen - wordLen
		} else {
			// Write word on this line
			buffer.WriteByte(' ')
			buffer.WriteString(word)
			spaceLeft -= 1 + wordLen
		}
	}

	return buffer.String()
}

// Add words to this map if pluralization does not just involve adding an s.
var plurals = map[string]string{
	"policy": "policies",
}

// Pluralize returns the correct pluralization of a work along with the
// specified count. This means Pluralize(1, "policy") = "1 policy" but
// Pluralize(2, "policy") = "2 policies".
func Pluralize(count int, word string) string {
	if count == 1 {
		return fmt.Sprintf("%d %s", count, word)
	}
	if plural, ok := plurals[word]; ok {
		return fmt.Sprintf("%d %s", count, plural)
	}
	return fmt.Sprintf("%d %ss", count, word)
}

// ReadLine returns a line of input from standard input. An empty string is
// returned if the user didn't insert anything, we're in quiet mode or on error.
// This function should be the only way user input is acquired from an
// application (except for passwords).
func ReadLine() (string, error) {
	if QuietFlag.Value {
		return "", nil
	}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	return scanner.Text(), scanner.Err()
}

// AskQuestion asks the user a yes or no question. Returning a boolean on a
// successful answer and an error if there was not a response from the user.
// Returns the defaultChoice on empty input (or in quiet mode).
func AskQuestion(question string, defaultChoice bool) (bool, error) {
	// Loop until failure or valid input.
	for {
		if defaultChoice {
			fmt.Fprintf(Output, "%s %s ", question, defaultYesSuffix)
		} else {
			fmt.Fprintf(Output, "%s %s ", question, defaultNoSuffix)
		}

		input, err := ReadLine()
		if err != nil {
			return false, err
		}

		switch strings.ToLower(input) {
		case "y", "yes":
			return true, nil
		case "n", "no":
			return false, nil
		case "":
			return defaultChoice, nil
		}
	}
}

// AskConfirmation asks the user for confirmation before performing a specific
// action. An error is returned if the user declines or IO fails.
func AskConfirmation(question, warning string, defaultChoice bool) error {
	// All confirmations are "yes" if we are forcing.
	if ForceFlag.Value {
		return nil
	}

	if warning != "" {
		fmt.Fprintln(Output, "WARNING: "+warning)
	}

	confirmed, err := AskQuestion(question, defaultChoice)
	if err != nil {
		return err
	}
	if !confirmed {
		// To override a "false" default, use ForceFlag.
		if QuietFlag.Value {
			return ErrMustForce
		}
		return ErrCanceled
	}
	return nil
}

// ExecuteTemplate creates an anonymous template from the text, and runs it with
// the provided Context and writer. Panics if text cannot be executed.
func ExecuteTemplate(w io.Writer, text string, ctx *Context) {
	tmpl := template.Must(template.New("").Funcs(template.FuncMap{
		"WrapText":   WrapText,
		"LineLength": func() int { return LineLength },
		"add": func(nums ...int) (sum int) {
			for _, num := range nums {
				sum += num
			}
			return
		},
	}).Parse(text))
	if err := tmpl.Execute(w, ctx); err != nil {
		panic(err)
	}
}