diff options
| -rw-r--r-- | internal/client/ui/core/chat/chat.go | 37 | ||||
| -rw-r--r-- | internal/client/ui/core/core.go | 55 | ||||
| -rw-r--r-- | internal/client/ui/core/frequencylist/frequencylist.go (renamed from internal/client/ui/core/network/network.go) | 12 | ||||
| -rw-r--r-- | internal/client/ui/viminput/linedecorations.go | 4 | ||||
| -rw-r--r-- | internal/client/ui/viminput/viminput.go | 38 |
5 files changed, 97 insertions, 49 deletions
diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go index b3597a8..04eedb7 100644 --- a/internal/client/ui/core/chat/chat.go +++ b/internal/client/ui/core/chat/chat.go @@ -1,9 +1,6 @@ package chat import ( - "log" - - "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -12,31 +9,34 @@ import ( ) type Model struct { - vi viminput.Model + vi viminput.Model + focus bool } func New() Model { vi := viminput.New(30, 3) vi.Placeholder = "Send a message..." vi.PlaceholderStyle = lipgloss.NewStyle().Foreground(colors.Gray) - vi.LineDecoration = func(lnum int, line string, cursorLnum int) string { - return "┃ " + vi.LineDecoration = func(lnum int, m viminput.Model) string { + lineNumberDecor := viminput.LineNumberDecoration(lipgloss.NewStyle()) + lineNumber := lineNumberDecor(lnum, m) + return lineNumber + " ┃ " } - // vi.ShowLineNumbers = false + + vi.SetLines(viminput.Line("test"), viminput.Line("best")) + // ta.Cursor.SetChar() // vi.CharLimit = 280 // vi.Focus() - log.Println("Placeholder:", vi.Placeholder) - return Model{ - vi: vi, + vi: vi, } } func (m Model) Init() tea.Cmd { - return tea.Batch(textarea.Blink, nil) + return nil } func (m Model) View() string { @@ -44,5 +44,18 @@ func (m Model) View() string { } func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { - return m, nil + if !m.focus { + return m, nil + } + var cmd tea.Cmd + m.vi, cmd = m.vi.Update(msg) + return m, cmd +} + +func (m *Model) Focus() { + m.focus = true +} + +func (m *Model) Blur() { + m.focus = false } diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go index b4390e0..dd29106 100644 --- a/internal/client/ui/core/core.go +++ b/internal/client/ui/core/core.go @@ -13,8 +13,9 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/kyren223/eko/internal/client/gateway" "github.com/kyren223/eko/internal/client/ui" + "github.com/kyren223/eko/internal/client/ui/core/chat" "github.com/kyren223/eko/internal/client/ui/core/frequencycreation" - "github.com/kyren223/eko/internal/client/ui/core/network" + "github.com/kyren223/eko/internal/client/ui/core/frequencylist" "github.com/kyren223/eko/internal/client/ui/core/networkcreation" "github.com/kyren223/eko/internal/client/ui/core/networklist" "github.com/kyren223/eko/internal/client/ui/core/state" @@ -35,7 +36,8 @@ var ( const ( FocusNetworkList = iota - FocusNetwork + FocusFrequencyList + FocusChat FocusMax ) @@ -52,7 +54,8 @@ type Model struct { networkCreationPopup *networkcreation.Model frequencyCreationPopup *frequencycreation.Model networkList networklist.Model - network network.Model + frequencyList frequencylist.Model + chat chat.Model focus int } @@ -68,7 +71,8 @@ func New(privKey ed25519.PrivateKey, name string) Model { networkCreationPopup: nil, frequencyCreationPopup: nil, networkList: networklist.New(), - network: network.New(), + frequencyList: frequencylist.New(), + chat: chat.New(), focus: FocusNetworkList, } m.move(0) // Update focus @@ -86,8 +90,9 @@ func (m Model) View() string { } networkList := m.networkList.View() - network := m.network.View() - result := lipgloss.JoinHorizontal(lipgloss.Top, networkList, network) + frequencyList := m.frequencyList.View() + chat := m.chat.View() + result := lipgloss.JoinHorizontal(lipgloss.Top, networkList, frequencyList, chat) result = lipgloss.Place( ui.Width, ui.Height, @@ -161,6 +166,9 @@ func (m *Model) updateNotConnected(msg tea.Msg) tea.Cmd { func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { switch msg := msg.(type) { + case ui.QuitMsg: + gateway.Disconnect() + case gateway.ConnectionLost: m.connected = false m.timeout = initialTimeout @@ -205,9 +213,6 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { network.Frequencies = frequencies } - case ui.QuitMsg: - gateway.Disconnect() - case tea.KeyMsg: key := msg.Type switch key { @@ -218,7 +223,7 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { popup := networkcreation.New() m.networkCreationPopup = &popup } - case FocusNetwork: + case FocusFrequencyList: if m.frequencyCreationPopup == nil { index := m.networkList.Index() network := state.State.Networks[index] @@ -260,12 +265,15 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { return cmd } - if msg.String() == "L" && m.focus == FocusNetworkList { - m.move(1) - } else if msg.String() == "H" && m.focus == FocusNetwork { - m.move(-1) + left := msg.String() == "H" + right := msg.String() == "L" + direction := 0 + if left { + direction = -1 + } else if right { + direction = 1 } - + m.move(direction) } } @@ -278,9 +286,11 @@ func (m *Model) updateConnected(msg tea.Msg) tea.Cmd { switch m.focus { case FocusNetworkList: m.networkList, cmd = m.networkList.Update(msg) - m.network.Set(m.networkList.Index()) - case FocusNetwork: - m.network, cmd = m.network.Update(msg) + m.frequencyList.Set(m.networkList.Index()) + case FocusFrequencyList: + m.frequencyList, cmd = m.frequencyList.Update(msg) + case FocusChat: + m.chat, cmd = m.chat.Update(msg) } return cmd } @@ -299,12 +309,15 @@ func (m *Model) move(direction int) { m.focus = max(0, min(FocusMax-1, focus)) m.networkList.Blur() - m.network.Blur() + m.frequencyList.Blur() + m.chat.Blur() switch m.focus { case FocusNetworkList: m.networkList.Focus() - case FocusNetwork: - m.network.Focus() + case FocusFrequencyList: + m.frequencyList.Focus() + case FocusChat: + m.chat.Focus() default: assert.Never("missing switch statement field in move", "focus", m.focus) } diff --git a/internal/client/ui/core/network/network.go b/internal/client/ui/core/frequencylist/frequencylist.go index 3c24350..3b7f800 100644 --- a/internal/client/ui/core/network/network.go +++ b/internal/client/ui/core/frequencylist/frequencylist.go @@ -1,4 +1,4 @@ -package network +package frequencylist import ( "strings" @@ -8,7 +8,6 @@ import ( "github.com/kyren223/eko/internal/client/gateway" "github.com/kyren223/eko/internal/client/ui" "github.com/kyren223/eko/internal/client/ui/colors" - "github.com/kyren223/eko/internal/client/ui/core/chat" "github.com/kyren223/eko/internal/client/ui/core/state" "github.com/kyren223/eko/internal/packet" ) @@ -32,16 +31,12 @@ type Model struct { networkIndex int index int focus bool - chat chat.Model } func New() Model { - chat := chat.New() - return Model{ focus: false, index: 0, - chat: chat, } } @@ -79,9 +74,7 @@ func (m Model) View() string { if m.focus { sep = sep.BorderForeground(colors.Focus) } - sidebar = lipgloss.JoinHorizontal(lipgloss.Top, sidebar, sep.String()) - - result := lipgloss.JoinHorizontal(lipgloss.Top, sidebar, m.chat.View()) + result := lipgloss.JoinHorizontal(lipgloss.Top, sidebar, sep.String()) return result } @@ -124,6 +117,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { } } + return m, nil } diff --git a/internal/client/ui/viminput/linedecorations.go b/internal/client/ui/viminput/linedecorations.go index 82e51f1..631c0ce 100644 --- a/internal/client/ui/viminput/linedecorations.go +++ b/internal/client/ui/viminput/linedecorations.go @@ -7,11 +7,11 @@ import ( ) func LineNumberDecoration(style lipgloss.Style) LineDecoration { - return func(lnum int, line string, cursorLnum int) string { + return func(lnum int, m Model) string { return style.Render(strconv.FormatInt(int64(lnum), 10)) } } -func EmptyLineDecoration(lnum int, line string, cursorLnum int) string { +func EmptyLineDecoration(lnum int, m Model) string { return "" } diff --git a/internal/client/ui/viminput/viminput.go b/internal/client/ui/viminput/viminput.go index e2b65a8..94c5413 100644 --- a/internal/client/ui/viminput/viminput.go +++ b/internal/client/ui/viminput/viminput.go @@ -1,13 +1,15 @@ package viminput import ( + "strings" + tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" ) type ( Keymap struct{} - LineDecoration = func(lnum int, line string, cursorLnum int) string + LineDecoration = func(lnum int, m Model) string ) type Line []byte @@ -21,7 +23,9 @@ type Model struct { Placeholder string LineDecoration LineDecoration - lines []Line + lines []Line + CursorLine int + CursorColumn int width int height int @@ -46,11 +50,23 @@ func (m Model) Init() tea.Cmd { } func (m Model) View() string { - placeholder := m.PlaceholderStyle.Render(m.Placeholder) - lineDecoration := m.LineDecoration(1, "", 1) + var lines []Line + if len(m.lines) == 0 { + placeholder := m.PlaceholderStyle.Render(m.Placeholder) + lines = append(lines, Line(placeholder)) + } else { + lines = m.lines + } - result := lipgloss.JoinHorizontal(lipgloss.Top, lineDecoration, placeholder) + var builder strings.Builder + for i, line := range lines { + lineDecoration := m.LineDecoration(i, m) + builder.WriteString(lineDecoration) + builder.Write(line) + builder.WriteByte('\n') + } + result := builder.String() result = lipgloss.NewStyle().Width(m.width).Height(m.height).Render(result) return result @@ -87,3 +103,15 @@ func (m *Model) Focus() { func (m *Model) Blur() { m.focus = false } + +func (m *Model) SetLines(lines ...Line) { + m.lines = lines +} + +func (m *Model) SetLine(lnum int, line Line) { + m.lines[lnum] = line +} + +func (m *Model) Line(lnum int) Line { + return m.lines[lnum] +} |
