Skip to content

Commit

Permalink
feat: show when topic is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Jan 26, 2025
1 parent df0498f commit 41586a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions kadmin/record _reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *offsets) newest() int64 {
return o.firstAvailable - 1
}

type NoRecordsReadableMsg struct {
type EmptyTopicMsg struct {
}

func (ka *SaramaKafkaAdmin) ReadRecords(ctx context.Context, rd ReadDetails) tea.Msg {
Expand Down Expand Up @@ -182,7 +182,7 @@ func (ka *SaramaKafkaAdmin) ReadRecords(ctx context.Context, rd ReadDetails) tea
if atLeastOnePartitionReadable {
return startedMsg
} else {
return NoRecordsReadableMsg{}
return EmptyTopicMsg{}
}
}

Expand Down
42 changes: 31 additions & 11 deletions ui/pages/consumption_page/consumption_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Model struct {
records []kadmin.ConsumerRecord
readDetails kadmin.ReadDetails
consuming bool
noRecordsAvailable bool
}

type ConsumerRecordReceived struct {
Expand All @@ -34,17 +35,30 @@ type ConsumerRecordReceived struct {
type ConsumptionEndedMsg struct{}

func (m *Model) View(ktx *kontext.ProgramKtx, renderer *ui.Renderer) string {
cmdBarView := m.cmdBar.View(ktx, renderer)

m.table.SetColumns([]table.Column{
{Title: "Key", Width: int(float64(ktx.WindowWidth-7) * 0.5)},
{Title: "Partition", Width: int(float64(ktx.WindowWidth-7) * 0.25)},
{Title: "Offset", Width: int(float64(ktx.WindowWidth-7) * 0.25)},
})
m.table.SetHeight(ktx.AvailableHeight - 2)
m.table.SetRows(m.rows)
tableRender := renderer.Render(styles.Table.Focus.Render(m.table.View()))
return ui.JoinVertical(lipgloss.Top, cmdBarView, tableRender)
var views []string
views = append(views, m.cmdBar.View(ktx, renderer))

if m.noRecordsAvailable {
views = append(views, lipgloss.NewStyle().
Width(ktx.WindowWidth-2).
Height(ktx.AvailableHeight).
AlignVertical(lipgloss.Center).
AlignHorizontal(lipgloss.Center).
Bold(true).
Foreground(lipgloss.Color(styles.ColorPink)).
Render("👀 Empty topic"))
} else if len(m.rows) > 0 {
m.table.SetColumns([]table.Column{
{Title: "Key", Width: int(float64(ktx.WindowWidth-7) * 0.5)},
{Title: "Partition", Width: int(float64(ktx.WindowWidth-7) * 0.25)},
{Title: "Offset", Width: int(float64(ktx.WindowWidth-7) * 0.25)},
})
m.table.SetHeight(ktx.AvailableHeight - 2)
m.table.SetRows(m.rows)
views = append(views, renderer.Render(styles.Table.Focus.Render(m.table.View())))
}

return ui.JoinVertical(lipgloss.Top, views...)
}

func (m *Model) Update(msg tea.Msg) tea.Cmd {
Expand Down Expand Up @@ -76,6 +90,8 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.table = &t
cmds = append(cmds, cmd)
}
case kadmin.EmptyTopicMsg:
m.noRecordsAvailable = true
case kadmin.ReadingStartedMsg:
m.consuming = true
m.consumerRecordChan = msg.ConsumerRecord
Expand Down Expand Up @@ -131,6 +147,10 @@ func (m *Model) Shortcuts() []statusbar.Shortcut {
{"Stop consuming", "F2"},
{"Go Back", "esc"},
}
} else if m.noRecordsAvailable {
return []statusbar.Shortcut{
{"Go Back", "esc"},
}
} else {
return []statusbar.Shortcut{
{"View Record", "enter"},
Expand Down
18 changes: 16 additions & 2 deletions ui/pages/consumption_page/consumption_page_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package consumption_page

import "testing"
import (
"github.com/stretchr/testify/assert"
"ktea/kadmin"
"ktea/ui"
"ktea/ui/components/statusbar"
"testing"
)

func TestConsumptionPage(t *testing.T) {
t.Run("", func(t *testing.T) {
t.Run("Display empty topic message and adjusted shortcuts", func(t *testing.T) {
m, _ := New(nil, kadmin.ReadDetails{})

m.Update(kadmin.EmptyTopicMsg{})

render := m.View(ui.NewTestKontext(), ui.TestRenderer)

assert.Contains(t, render, "Empty topic")

assert.Equal(t, []statusbar.Shortcut{{"Go Back", "esc"}}, m.Shortcuts())
})
}

0 comments on commit 41586a0

Please sign in to comment.