-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlist.go
96 lines (78 loc) · 1.98 KB
/
list.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
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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
)
var (
listStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, true, false, false).
BorderForeground(subtle).
MarginRight(2)
listHeader = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
BorderForeground(subtle).
MarginRight(2).
Render
listItemStyle = lipgloss.NewStyle().PaddingLeft(2).Render
checkMark = lipgloss.NewStyle().SetString("✓").
Foreground(special).
PaddingRight(1).
String()
listDoneStyle = func(s string) string {
return checkMark + lipgloss.NewStyle().
Strikethrough(true).
Foreground(lipgloss.AdaptiveColor{Light: "#969B86", Dark: "#696969"}).
Render(s)
}
)
type listItem struct {
name string
done bool
}
type list struct {
id string
height int
width int
title string
items []listItem
}
func (m list) Init() tea.Cmd {
return nil
}
func (m list) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
}
for i, item := range m.items {
// Check each item to see if it's in bounds.
if zone.Get(m.id + item.name).InBounds(msg) {
m.items[i].done = !m.items[i].done
break
}
}
return m, nil
}
return m, nil
}
func (m list) View() string {
out := []string{listHeader(m.title)}
for _, item := range m.items {
if item.done {
out = append(out, zone.Mark(m.id+item.name, listDoneStyle(item.name)))
continue
}
out = append(out, zone.Mark(m.id+item.name, listItemStyle(item.name)))
}
return listStyle.Render(
lipgloss.JoinVertical(lipgloss.Left, out...),
)
}