summaryrefslogtreecommitdiff
path: root/internal/client/ui/core/chat/chat.go
blob: 70d66549864b46a71523323ff3dbe68ececac92f (plain)
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
package chat

import (
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/kyren223/eko/internal/client/ui/colors"
	"github.com/kyren223/eko/internal/client/ui/viminput"
)

var (
	ViBlurredBorder = lipgloss.NewStyle().
			BorderStyle(lipgloss.RoundedBorder()).UnsetBorderBottom()
	ViFocusedBorder = lipgloss.NewStyle().
			BorderStyle(lipgloss.ThickBorder()).UnsetBorderBottom()
)

type Model struct {
	vi     viminput.Model
	focus  bool
	locked bool
}

func New() Model {
	vi := viminput.New(90, 20)
	vi.Placeholder = "Send a message..."
	vi.PlaceholderStyle = lipgloss.NewStyle().Foreground(colors.Gray)

	return Model{
		vi:     vi,
		focus:  false,
		locked: false,
	}
}

func (m Model) Init() tea.Cmd {
	return nil
}

func (m Model) View() string {
	input := m.vi.View()

	if m.locked {
		input = ViFocusedBorder.Render(input)
	} else {
		input = ViBlurredBorder.Render(input)
	}

	result := input

	return result
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
	if !m.focus {
		return m, nil
	}

	if m.locked {
		InNormal := m.vi.Mode() == viminput.NormalMode
		if key, ok := msg.(tea.KeyMsg); ok && InNormal {
			if key.String() == "q" {
				m.locked = false
				return m, nil
			}
		}

		var cmd tea.Cmd
		m.vi, cmd = m.vi.Update(msg)
		return m, cmd
	}

	switch msg := msg.(type) {
	case tea.KeyMsg:
		key := msg.String()
		switch key {
		case "i":
			m.locked = true
			m.vi.SetMode(viminput.InsertMode)
		}
	}

	return m, nil
}

func (m *Model) Focus() {
	m.focus = true
	m.vi.Focus()
}

func (m *Model) Blur() {
	m.focus = false
	m.vi.Blur()
}

func (m Model) Locked() bool {
	return m.locked
}