diff options
| author | Kyren223 <Kyren223@proton.me> | 2024-12-24 16:24:09 +0200 |
|---|---|---|
| committer | Kyren223 <Kyren223@proton.me> | 2024-12-24 16:24:09 +0200 |
| commit | 54fb74018483e534e967ea1af957fd79286c79e4 (patch) | |
| tree | 9c4c01a7609c7c5387cb4bbaf6b1bbf423621959 /internal/client | |
| parent | 2618785d1069298f0c19f6fe1e31692b0ae575ad (diff) | |
Added ctrl+d/ctrl+u for scrolling up/down in messages view
Diffstat (limited to 'internal/client')
| -rw-r--r-- | internal/client/ui/core/chat/chat.go | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go index e435fd4..9d67468 100644 --- a/internal/client/ui/core/chat/chat.go +++ b/internal/client/ui/core/chat/chat.go @@ -161,31 +161,13 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { m.vi.SetMode(viminput.InsertMode) m.index = -1 case "k": - if m.index == -1 && m.offset != SnapToBottom { - m.index = m.offset - m.messagesHeight - } else if m.index == -1 { - m.index = 1 // Skip bottom blank line - } else { - m.index++ - maxHeight := m.offset - if m.offset == SnapToBottom { - maxHeight = m.messagesHeight - } - if m.index == maxHeight-1 { - m.offset = maxHeight + 1 - } - } + m.Scroll(1) case "j": - m.index = max(-1, m.index-1) - if m.offset != SnapToBottom { - diff := m.offset - m.messagesHeight - m.index - if diff > 0 { - m.offset -= diff - } - } - if m.index <= 1 { - m.offset = SnapToBottom - } + m.Scroll(-1) + case "ctrl+u": + m.Scroll(m.messagesHeight / 2) + case "ctrl+d": + m.Scroll(-m.messagesHeight / 2) } } @@ -582,3 +564,44 @@ func (m *Model) renderHeader(message data.Message, selected bool) []byte { buf = append(buf, '\n') return buf } + +func (m *Model) Scroll(amount int) { + if m.index == -1 { + if amount <= 0 { + return + } + + if m.offset != SnapToBottom { + m.index = m.offset - m.messagesHeight + } else { + m.index = 1 // Skip bottom blank line + } + amount-- + } + + m.index = max(-1, m.index+amount) + + if amount > 0 { + // Scrolling up + maxHeight := m.offset + if m.offset == SnapToBottom { + maxHeight = m.messagesHeight + } + if m.index >= maxHeight-1 { + m.offset = m.index+2 + } + } else { + // Scrolling down + if m.offset != SnapToBottom { + diff := m.offset - m.messagesHeight - m.index + if diff > 0 { + m.offset -= diff + } + } + } + + // If at bottom snap to it + if m.index <= 1 { + m.offset = SnapToBottom + } +} |
