File: renderer.go

package info (click to toggle)
golang-github-alecaivazis-survey 2.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 652 kB
  • sloc: makefile: 12
file content (195 lines) | stat: -rw-r--r-- 5,023 bytes parent folder | download
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
package survey

import (
	"bytes"
	"fmt"
	"github.com/AlecAivazis/survey/v2/core"
	"github.com/AlecAivazis/survey/v2/terminal"
	"golang.org/x/term"
)

type Renderer struct {
	stdio          terminal.Stdio
	renderedErrors bytes.Buffer
	renderedText   bytes.Buffer
}

type ErrorTemplateData struct {
	Error error
	Icon  Icon
}

var ErrorTemplate = `{{color .Icon.Format }}{{ .Icon.Text }} Sorry, your reply was invalid: {{ .Error.Error }}{{color "reset"}}
`

func (r *Renderer) WithStdio(stdio terminal.Stdio) {
	r.stdio = stdio
}

func (r *Renderer) Stdio() terminal.Stdio {
	return r.stdio
}

func (r *Renderer) NewRuneReader() *terminal.RuneReader {
	return terminal.NewRuneReader(r.stdio)
}

func (r *Renderer) NewCursor() *terminal.Cursor {
	return &terminal.Cursor{
		In:  r.stdio.In,
		Out: r.stdio.Out,
	}
}

func (r *Renderer) Error(config *PromptConfig, invalid error) error {
	// cleanup the currently rendered errors
	r.resetPrompt(r.countLines(r.renderedErrors))
	r.renderedErrors.Reset()

	// cleanup the rest of the prompt
	r.resetPrompt(r.countLines(r.renderedText))
	r.renderedText.Reset()

	userOut, layoutOut, err := core.RunTemplate(ErrorTemplate, &ErrorTemplateData{
		Error: invalid,
		Icon:  config.Icons.Error,
	})
	if err != nil {
		return err
	}

	// send the message to the user
	if _, err := fmt.Fprint(terminal.NewAnsiStdout(r.stdio.Out), userOut); err != nil {
		return err
	}

	// add the printed text to the rendered error buffer so we can cleanup later
	r.appendRenderedError(layoutOut)

	return nil
}

func (r *Renderer) OffsetCursor(offset int) {
	cursor := r.NewCursor()
	for offset > 0 {
		cursor.PreviousLine(1)
		offset--
	}
}

func (r *Renderer) Render(tmpl string, data interface{}) error {
	// cleanup the currently rendered text
	lineCount := r.countLines(r.renderedText)
	r.resetPrompt(lineCount)
	r.renderedText.Reset()

	// render the template summarizing the current state
	userOut, layoutOut, err := core.RunTemplate(tmpl, data)
	if err != nil {
		return err
	}

	// print the summary
	if _, err := fmt.Fprint(terminal.NewAnsiStdout(r.stdio.Out), userOut); err != nil {
		return err
	}

	// add the printed text to the rendered text buffer so we can cleanup later
	r.AppendRenderedText(layoutOut)

	// nothing went wrong
	return nil
}

func (r *Renderer) RenderWithCursorOffset(tmpl string, data IterableOpts, opts []core.OptionAnswer, idx int) error {
	cursor := r.NewCursor()
	cursor.Restore() // clear any accessibility offsetting

	if err := r.Render(tmpl, data); err != nil {
		return err
	}
	cursor.Save()

	offset := computeCursorOffset(MultiSelectQuestionTemplate, data, opts, idx, r.termWidthSafe())
	r.OffsetCursor(offset)

	return nil
}

// appendRenderedError appends text to the renderer's error buffer
// which is used to track what has been printed. It is not exported
// as errors should only be displayed via Error(config, error).
func (r *Renderer) appendRenderedError(text string) {
	r.renderedErrors.WriteString(text)
}

// AppendRenderedText appends text to the renderer's text buffer
// which is used to track of what has been printed. The buffer is used
// to calculate how many lines to erase before updating the prompt.
func (r *Renderer) AppendRenderedText(text string) {
	r.renderedText.WriteString(text)
}

func (r *Renderer) resetPrompt(lines int) {
	// clean out current line in case tmpl didnt end in newline
	cursor := r.NewCursor()
	cursor.HorizontalAbsolute(0)
	terminal.EraseLine(r.stdio.Out, terminal.ERASE_LINE_ALL)
	// clean up what we left behind last time
	for i := 0; i < lines; i++ {
		cursor.PreviousLine(1)
		terminal.EraseLine(r.stdio.Out, terminal.ERASE_LINE_ALL)
	}
}

func (r *Renderer) termWidth() (int, error) {
	fd := int(r.stdio.Out.Fd())
	termWidth, _, err := term.GetSize(fd)
	return termWidth, err
}

func (r *Renderer) termWidthSafe() int {
	w, err := r.termWidth()
	if err != nil || w == 0 {
		// if we got an error due to terminal.GetSize not being supported
		// on current platform then just assume a very wide terminal
		w = 10000
	}
	return w
}

// countLines will return the count of `\n` with the addition of any
// lines that have wrapped due to narrow terminal width
func (r *Renderer) countLines(buf bytes.Buffer) int {
	w := r.termWidthSafe()

	bufBytes := buf.Bytes()

	count := 0
	curr := 0
	for curr < len(bufBytes) {
		var delim int
		// read until the next newline or the end of the string
		relDelim := bytes.IndexRune(bufBytes[curr:], '\n')
		if relDelim != -1 {
			count += 1 // new line found, add it to the count
			delim = curr + relDelim
		} else {
			delim = len(bufBytes) // no new line found, read rest of text
		}

		str := string(bufBytes[curr:delim])
		if lineWidth := terminal.StringWidth(str); lineWidth > w {
			// account for word wrapping
			count += lineWidth / w
			if (lineWidth % w) == 0 {
				// content whose width is exactly a multiplier of available width should not
				// count as having wrapped on the last line
				count -= 1
			}
		}
		curr = delim + 1
	}

	return count
}