From ac1123f5e04e7ef7a695f02bb66eb7fe1134899c Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Fri, 22 Nov 2024 12:43:09 +0200 Subject: Ctrl+C is now a global keybind --- internal/client/client.go | 12 +++++--- internal/client/gateway/gateway.go | 4 ++- internal/client/ui/auth/auth.go | 3 -- internal/client/ui/chat/chat.go | 2 -- internal/client/ui/core/core.go | 47 ++++++++++++++++++++--------- internal/client/ui/loadscreen/loadscreen.go | 2 +- 6 files changed, 44 insertions(+), 26 deletions(-) (limited to 'internal') diff --git a/internal/client/client.go b/internal/client/client.go index d658427..57ee5ab 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -63,6 +63,11 @@ func (m model) View() string { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.KeyMsg: + if msg.Type == tea.KeyCtrlC { + return m, tea.Quit + } + case tea.WindowSizeMsg: ui.Width, ui.Height = msg.Width, msg.Height return m, nil @@ -72,9 +77,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.model = msg.Model return m, m.model.Init() - default: - var cmd tea.Cmd - m.model, cmd = m.model.Update(msg) - return m, cmd } + var cmd tea.Cmd + m.model, cmd = m.model.Update(msg) + return m, cmd } diff --git a/internal/client/gateway/gateway.go b/internal/client/gateway/gateway.go index 441395a..22d851f 100644 --- a/internal/client/gateway/gateway.go +++ b/internal/client/gateway/gateway.go @@ -53,8 +53,10 @@ func init() { } } -func Connect(ctx context.Context, privKey ed25519.PrivateKey) tea.Cmd { +func Connect(privKey ed25519.PrivateKey, timeout time.Duration) tea.Cmd { return func() tea.Msg { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() err := connect(ctx, privKey) if err != nil { return ConnectionFailed(err) diff --git a/internal/client/ui/auth/auth.go b/internal/client/ui/auth/auth.go index 2e81a16..2656b76 100644 --- a/internal/client/ui/auth/auth.go +++ b/internal/client/ui/auth/auth.go @@ -248,9 +248,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmd := m.SetSignup(!m.signup) return m, cmd - case tea.KeyCtrlC: - return m, tea.Quit - case tea.KeyCtrlT: if m.popup == nil && (m.focusIndex == passphraseField || m.focusIndex == passphraseConfirmField) { m.fields[m.focusIndex].SetRevealed(!m.fields[m.focusIndex].Revealed()) diff --git a/internal/client/ui/chat/chat.go b/internal/client/ui/chat/chat.go index b87071e..c69d0fd 100644 --- a/internal/client/ui/chat/chat.go +++ b/internal/client/ui/chat/chat.go @@ -76,8 +76,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { case tea.KeyMsg: switch msg.Type { - case tea.KeyEsc, tea.KeyCtrlC: - return m, tea.Quit case tea.KeyEnter: value := m.textarea.Value() diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go index 1dbaef5..12b8b42 100644 --- a/internal/client/ui/core/core.go +++ b/internal/client/ui/core/core.go @@ -2,35 +2,52 @@ package core import ( "crypto/ed25519" - "fmt" + "time" tea "github.com/charmbracelet/bubbletea" + "github.com/kyren223/eko/internal/client/gateway" + "github.com/kyren223/eko/internal/client/ui/loadscreen" ) -type Model struct{} +var ( + connectingToServer = "Connecting to server.." + connectionFailed = "Connection failed - retrying in %d sec..." +) -func New(privKey ed25519.PrivateKey, name string) Model { +type Model struct { + privKey ed25519.PrivateKey + name string + + loading loadscreen.Model + connected bool +} - // Connectiong to server... - // Connection failed - retrying in 3 sec... - // Connection failed - retrying in 3 sec... - return Model{} +func New(privKey ed25519.PrivateKey, name string) Model { + return Model{ + privKey: privKey, + name: name, + loading: loadscreen.New(connectingToServer), + connected: false, + } } func (m Model) Init() tea.Cmd { - return nil + return tea.Batch(gateway.Connect(m.privKey, 5*time.Second), m.loading.Init()) } func (m Model) View() string { - return fmt.Sprintf( - "%s", - "", - ) + if !m.connected { + return m.loading.View() + } + + return "" } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - default: - return m, nil + if !m.connected { + var loadscreenCmd tea.Cmd + m.loading, loadscreenCmd = m.loading.Update(msg) + return m, loadscreenCmd } + return m, nil } diff --git a/internal/client/ui/loadscreen/loadscreen.go b/internal/client/ui/loadscreen/loadscreen.go index 6f4610a..4fe9a7d 100644 --- a/internal/client/ui/loadscreen/loadscreen.go +++ b/internal/client/ui/loadscreen/loadscreen.go @@ -144,7 +144,7 @@ func (m Model) View() string { ) } -func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { var cmd tea.Cmd m.sp, cmd = m.sp.Update(msg) return m, cmd -- cgit v1.3.1