forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.go
More file actions
106 lines (85 loc) · 2.53 KB
/
renderer.go
File metadata and controls
106 lines (85 loc) · 2.53 KB
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
package tea
import (
"fmt"
"github.com/charmbracelet/colorprofile"
)
const (
// defaultFramerate specifies the maximum interval at which we should
// update the view.
defaultFPS = 60
maxFPS = 120
)
// renderer is the interface for Bubble Tea renderers.
type renderer interface {
// close closes the renderer and flushes any remaining data.
close() error
// render renders a frame to the output.
render(string, *Cursor)
// flush flushes the renderer's buffer to the output.
flush() error
// reset resets the renderer's state to its initial state.
reset()
// insertAbove inserts unmanaged lines above the renderer.
insertAbove(string)
// enterAltScreen enters the alternate screen buffer.
enterAltScreen()
// exitAltScreen exits the alternate screen buffer.
exitAltScreen()
// showCursor shows the cursor.
showCursor()
// hideCursor hides the cursor.
hideCursor()
// resize notify the renderer of a terminal resize.
resize(int, int)
// setColorProfile sets the color profile.
setColorProfile(colorprofile.Profile)
// clearScreen clears the screen.
clearScreen()
// repaint forces a full repaint.
repaint()
}
// repaintMsg forces a full repaint.
type repaintMsg struct{}
type printLineMessage struct {
messageBody string
}
// Println prints above the Program. This output is unmanaged by the program and
// will persist across renders by the Program.
//
// Unlike fmt.Println (but similar to log.Println) the message will be print on
// its own line.
//
// If the altscreen is active no output will be printed.
func Println(args ...interface{}) Cmd {
return func() Msg {
return printLineMessage{
messageBody: fmt.Sprint(args...),
}
}
}
// Printf prints above the Program. It takes a format template followed by
// values similar to fmt.Printf. This output is unmanaged by the program and
// will persist across renders by the Program.
//
// Unlike fmt.Printf (but similar to log.Printf) the message will be print on
// its own line.
//
// If the altscreen is active no output will be printed.
func Printf(template string, args ...interface{}) Cmd {
return func() Msg {
return printLineMessage{
messageBody: fmt.Sprintf(template, args...),
}
}
}
// encodeCursorStyle returns the integer value for the given cursor style and
// blink state.
func encodeCursorStyle(style CursorShape, blink bool) int {
// We're using the ANSI escape sequence values for cursor styles.
// We need to map both [style] and [steady] to the correct value.
style = (style * 2) + 1 //nolint:mnd
if !blink {
style++
}
return int(style)
}