Skip to content

Commit a69dda5

Browse files
committed
bugfix: wrap value detail
1 parent ea010d0 commit a69dda5

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var rootCmd = &cobra.Command{
4343
log.Fatal("connect to redis failed: ", err)
4444
}
4545

46-
p := tea.NewProgram(tui.New(rdb), tea.WithAltScreen())
46+
p := tea.NewProgram(tui.New(rdb), tea.WithAltScreen(), tea.WithMouseAllMotion())
4747
if err := p.Start(); err != nil {
4848
log.Fatal("start failed: ", err)
4949
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/charmbracelet/lipgloss v0.4.0
99
github.com/go-redis/redis/v8 v8.11.4
1010
github.com/json-iterator/go v1.1.12
11+
github.com/muesli/reflow v0.3.0
1112
github.com/spf13/cobra v1.3.0
1213
github.com/spf13/viper v1.10.1
1314
)

internal/tui/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ const (
1414
const (
1515
defaultScanCount = 500
1616
)
17+
18+
const (
19+
listProportion = 0.3
20+
statusBarHeight = 1
21+
)

internal/tui/tui.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ package tui
88
import (
99
"github.com/charmbracelet/bubbles/list"
1010
"github.com/charmbracelet/bubbles/textinput"
11+
"github.com/charmbracelet/bubbles/viewport"
1112
"github.com/charmbracelet/lipgloss"
1213
"github.com/go-redis/redis/v8"
1314
)
1415

1516
type model struct {
17+
width, height int
18+
1619
list list.Model
1720
textinput textinput.Model
21+
viewport viewport.Model
1822

1923
rdb *redis.Client
2024
searchValue string
21-
valueDetail string
2225
stateDesc string
2326

2427
keyMap

internal/tui/update.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ package tui
77
import (
88
"github.com/charmbracelet/bubbles/key"
99
"github.com/charmbracelet/bubbles/textinput"
10+
"github.com/charmbracelet/bubbles/viewport"
1011
tea "github.com/charmbracelet/bubbletea"
1112
)
1213

1314
// handleMouse handles all mouse interaction.
1415
func (m *model) handleMouse(msg tea.MouseMsg) {
1516
switch msg.Type {
1617
case tea.MouseWheelUp:
17-
m.list.CursorUp()
18+
m.viewport.ViewUp()
1819
case tea.MouseWheelDown:
19-
m.list.CursorDown()
20+
m.viewport.ViewDown()
2021
}
2122
}
2223

@@ -63,7 +64,6 @@ func (m *model) handleKeys(msg tea.KeyMsg) tea.Cmd {
6364
m.textinput.Blur()
6465
m.textinput.Reset()
6566
m.state = defaultState
66-
m.stateDesc = ""
6767
default:
6868
m.textinput, cmd = m.textinput.Update(msg)
6969
cmds = append(cmds, cmd)
@@ -82,7 +82,15 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8282
switch msg := msg.(type) {
8383
case tea.WindowSizeMsg:
8484
topGap, rightGap, bottomGap, leftGap := appStyle.GetPadding()
85-
m.list.SetSize(msg.Width-leftGap-rightGap, msg.Height-topGap-bottomGap-1)
85+
m.width = msg.Width - leftGap - rightGap
86+
m.height = msg.Height - topGap - bottomGap
87+
88+
listWidth := int(listProportion * float64(m.width))
89+
m.list.SetSize(listWidth, m.height-statusBarHeight)
90+
91+
viewportWidth := m.width - listWidth
92+
viewportBorderLeft := viewportStyle.GetBorderLeftSize()
93+
m.viewport = viewport.New(viewportWidth-viewportBorderLeft, m.height-statusBarHeight)
8694
case scanMsg:
8795
m.list.SetItems(msg.items)
8896
case tea.MouseMsg:
@@ -96,6 +104,9 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
96104

97105
m.textinput, cmd = m.textinput.Update(msg)
98106
cmds = append(cmds, cmd)
107+
108+
m.viewport, cmd = m.viewport.Update(msg)
109+
cmds = append(cmds, cmd)
99110
}
100111

101112
return m, tea.Batch(cmds...)

internal/tui/view.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ import (
88
"fmt"
99

1010
"github.com/charmbracelet/lipgloss"
11+
"github.com/muesli/reflow/wordwrap"
1112
)
1213

1314
var (
1415
appStyle = lipgloss.NewStyle().Padding(1, 2)
16+
17+
viewportStyle = lipgloss.NewStyle().Border(lipgloss.HiddenBorder(), false, false, false, true)
1518
)
1619

1720
func (m *model) View() string {
1821
if it := m.list.SelectedItem(); it != nil {
19-
m.valueDetail = fmt.Sprintf("KeyType: %s\nValue:\n%s", it.(item).keyType, it.(item).val)
22+
valueDetail := fmt.Sprintf("KeyType: %s\nValue:\n%s", it.(item).keyType, it.(item).val)
23+
m.viewport.SetContent(wordwrap.String(valueDetail, m.viewport.Width))
2024
}
2125

2226
if m.state == searchState {
@@ -26,7 +30,7 @@ func (m *model) View() string {
2630
return appStyle.Render(
2731
lipgloss.JoinVertical(
2832
lipgloss.Left,
29-
lipgloss.JoinHorizontal(lipgloss.Top, m.list.View(), m.valueDetail),
33+
lipgloss.JoinHorizontal(lipgloss.Top, m.list.View(), viewportStyle.Render(m.viewport.View())),
3034
m.stateDesc,
3135
),
3236
)

0 commit comments

Comments
 (0)