-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtty_view.go
62 lines (46 loc) · 1.39 KB
/
tty_view.go
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
package view
import (
"fmt"
"os"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
tea "github.com/charmbracelet/bubbletea"
)
type TTYView struct {
program *tea.Program
logger logger.Logger
initialModel model
}
func NewTTYView() (*TTYView, error) {
c := &TTYView{}
c.initialModel = initialModel()
mw := &messageWriter{}
level, present := os.LookupEnv("LOG_LEVEL")
if !present {
level = logger.LogLevelInfo
}
c.logger = logger.NewTTYLogger(mw, level)
c.logger.SetLevel(level)
c.initialModel.logger = c.logger
c.program = tea.NewProgram(c.initialModel, tea.WithAltScreen(), tea.WithMouseAllMotion())
// once the program has been created set a reference to the writer so that
// log lines get directed to bubbletea
mw.program = c.program
return c, nil
}
// Display starts the view, this is a blocking function
func (c *TTYView) Display() error {
if _, err := c.program.Run(); err != nil {
return fmt.Errorf("unable to start bubbletea view: %s", err)
}
return nil
}
// Logger returns the logger used by the view
func (c *TTYView) Logger() logger.Logger {
return c.logger
}
// UpdateStatus shows the current status message, if withTimer is set
// the elapsed time that the the status has been shown for will also
// be displayed
func (c *TTYView) UpdateStatus(message string, withTimer bool) {
c.program.Send(StatusMsg{Message: message, ShowElapsed: withTimer})
}