-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (71 loc) · 1.75 KB
/
Copy pathmain.go
File metadata and controls
85 lines (71 loc) · 1.75 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
package main
import (
"bytes"
_ "embed"
"fmt"
"image"
_ "image/jpeg"
"os"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
bubblekitten "github.com/arthursfares/bubblekitten"
)
//go:embed kenobi.jpeg
var exampleJPEG []byte
const imgCols, imgRows = 60, 17
type model struct {
img bubblekitten.Model
initCmd tea.Cmd
altScreen bool
}
func initialModel() model {
img, cmd := bubblekitten.New().SetImage(loadExampleImage())
return model{img: img, initCmd: cmd}
}
func (m model) Init() tea.Cmd {
return m.initCmd
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
var cmd tea.Cmd
m.img, cmd = m.img.SetSize(imgCols, imgRows)
cmds = append(cmds, cmd)
case tea.KeyPressMsg:
switch msg.String() {
case "q", "ctrl+c":
cmds = append(cmds, m.img.Close(), tea.Quit)
return m, tea.Sequence(cmds...)
case "f":
m.altScreen = !m.altScreen
var cmd tea.Cmd
m.img, cmd = m.img.SetAltScreen(m.altScreen)
cmds = append(cmds, cmd)
}
case bubblekitten.ErrMsg:
fmt.Fprintln(os.Stderr, "image error:", msg.Err)
}
var cmd tea.Cmd
m.img, cmd = m.img.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() tea.View {
help := lipgloss.NewStyle().Faint(true).Render("f: toggle AltScreen • q: quit")
content := lipgloss.JoinVertical(lipgloss.Left, m.img.View(), "", help)
v := tea.NewView(content)
v.AltScreen = m.altScreen
return v
}
func loadExampleImage() image.Image {
img, _, err := image.Decode(bytes.NewReader(exampleJPEG))
if err != nil { panic(err) }
return img
}
func main() {
if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}