1package model
2
3import (
4 "github.com/charmbracelet/crush/internal/ui/common"
5 "github.com/charmbracelet/crush/internal/ui/lazylist"
6 uv "github.com/charmbracelet/ultraviolet"
7)
8
9// Chat represents the chat UI model that handles chat interactions and
10// messages.
11type Chat struct {
12 com *common.Common
13 list *lazylist.List
14}
15
16// NewChat creates a new instance of [Chat] that handles chat interactions and
17// messages.
18func NewChat(com *common.Common) *Chat {
19 l := lazylist.NewList()
20 l.SetGap(1)
21 return &Chat{
22 com: com,
23 list: l,
24 }
25}
26
27// Height returns the height of the chat view port.
28func (m *Chat) Height() int {
29 return m.list.Height()
30}
31
32// Draw renders the chat UI component to the screen and the given area.
33func (m *Chat) Draw(scr uv.Screen, area uv.Rectangle) {
34 uv.NewStyledString(m.list.Render()).Draw(scr, area)
35}
36
37// SetSize sets the size of the chat view port.
38func (m *Chat) SetSize(width, height int) {
39 m.list.SetSize(width, height)
40}
41
42// Len returns the number of items in the chat list.
43func (m *Chat) Len() int {
44 return m.list.Len()
45}
46
47// PrependItems prepends new items to the chat list.
48func (m *Chat) PrependItems(items ...lazylist.Item) {
49 m.list.PrependItems(items...)
50 m.list.ScrollToIndex(0)
51}
52
53// AppendMessages appends a new message item to the chat list.
54func (m *Chat) AppendMessages(msgs ...MessageItem) {
55 items := make([]lazylist.Item, len(msgs))
56 for i, msg := range msgs {
57 items[i] = msg
58 }
59 m.list.AppendItems(items...)
60}
61
62// AppendItems appends new items to the chat list.
63func (m *Chat) AppendItems(items ...lazylist.Item) {
64 m.list.AppendItems(items...)
65 m.list.ScrollToIndex(m.list.Len() - 1)
66}
67
68// Focus sets the focus state of the chat component.
69func (m *Chat) Focus() {
70 m.list.Focus()
71}
72
73// Blur removes the focus state from the chat component.
74func (m *Chat) Blur() {
75 m.list.Blur()
76}
77
78// ScrollToTop scrolls the chat view to the top.
79func (m *Chat) ScrollToTop() {
80 m.list.ScrollToTop()
81}
82
83// ScrollToBottom scrolls the chat view to the bottom.
84func (m *Chat) ScrollToBottom() {
85 m.list.ScrollToBottom()
86}
87
88// ScrollBy scrolls the chat view by the given number of line deltas.
89func (m *Chat) ScrollBy(lines int) {
90 m.list.ScrollBy(lines)
91}
92
93// ScrollToSelected scrolls the chat view to the selected item.
94func (m *Chat) ScrollToSelected() {
95 m.list.ScrollToSelected()
96}
97
98// SelectedItemInView returns whether the selected item is currently in view.
99func (m *Chat) SelectedItemInView() bool {
100 return m.list.SelectedItemInView()
101}
102
103// SetSelected sets the selected message index in the chat list.
104func (m *Chat) SetSelected(index int) {
105 m.list.SetSelected(index)
106}
107
108// SelectPrev selects the previous message in the chat list.
109func (m *Chat) SelectPrev() {
110 m.list.SelectPrev()
111}
112
113// SelectNext selects the next message in the chat list.
114func (m *Chat) SelectNext() {
115 m.list.SelectNext()
116}
117
118// SelectFirst selects the first message in the chat list.
119func (m *Chat) SelectFirst() {
120 m.list.SelectFirst()
121}
122
123// SelectLast selects the last message in the chat list.
124func (m *Chat) SelectLast() {
125 m.list.SelectLast()
126}
127
128// SelectFirstInView selects the first message currently in view.
129func (m *Chat) SelectFirstInView() {
130 m.list.SelectFirstInView()
131}
132
133// SelectLastInView selects the last message currently in view.
134func (m *Chat) SelectLastInView() {
135 m.list.SelectLastInView()
136}
137
138// HandleMouseDown handles mouse down events for the chat component.
139func (m *Chat) HandleMouseDown(x, y int) {
140 m.list.HandleMouseDown(x, y)
141}
142
143// HandleMouseUp handles mouse up events for the chat component.
144func (m *Chat) HandleMouseUp(x, y int) {
145 m.list.HandleMouseUp(x, y)
146}
147
148// HandleMouseDrag handles mouse drag events for the chat component.
149func (m *Chat) HandleMouseDrag(x, y int) {
150 m.list.HandleMouseDrag(x, y)
151}