-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhistory.go
71 lines (60 loc) · 1.54 KB
/
history.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
// 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"
)
type history struct {
id string
height int
width int
active string
items []string
}
func (m history) Init() tea.Cmd {
return nil
}
func (m history) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
}
for _, item := range m.items {
// Check each item to see if it's in bounds.
if zone.Get(m.id + item).InBounds(msg) {
m.active = item
break
}
}
}
return m, nil
}
func (m history) View() string {
historyStyle := lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(lipgloss.Color("#FAFAFA")).
Background(subtle).
Margin(1).
Padding(1, 2).
Width((m.width / len(m.items)) - 2).
Height(m.height - 2).
MaxHeight(m.height)
out := []string{}
for _, item := range m.items {
if item == m.active {
// Customize the active item.
out = append(out, zone.Mark(m.id+item, historyStyle.Background(highlight).Render(item)))
} else {
// Make sure to mark all zones.
out = append(out, zone.Mark(m.id+item, historyStyle.Render(item)))
}
}
return lipgloss.JoinHorizontal(lipgloss.Top, out...)
}