Skip to content

Commit

Permalink
fix: key bindings in plain text view
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw committed Nov 16, 2024
1 parent b859986 commit 07f80b4
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 98 deletions.
5 changes: 3 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/hedhyw/json-log-viewer/internal/keymap"
"github.com/hedhyw/json-log-viewer/internal/pkg/source"

"github.com/hedhyw/json-log-viewer/internal/pkg/config"
Expand All @@ -22,7 +23,7 @@ type Application struct {
Entries source.LazyLogEntries
Version string

keys KeyMap
keys keymap.KeyMap
help help.Model
}

Expand All @@ -49,7 +50,7 @@ func newApplication(
},

Version: version,
keys: defaultKeys,
keys: keymap.GetDefaultKeys(),
help: help.New(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (app *Application) handleOpenJSONRowRequestedMsg(

func (app *Application) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
switch {
case key.Matches(msg, defaultKeys.Exit):
case key.Matches(msg, app.keys.Exit):
return tea.Quit
case key.Matches(msg, defaultKeys.Filter):
case key.Matches(msg, app.keys.Filter):
return events.FilterKeyClicked
case key.Matches(msg, defaultKeys.Open):
case key.Matches(msg, app.keys.Open):
return events.EnterKeyClicked
case key.Matches(msg, defaultKeys.ToggleViewArrow):
case key.Matches(msg, app.keys.ToggleViewArrow):
return events.ArrowRightKeyClicked
default:
return nil
Expand Down
80 changes: 0 additions & 80 deletions internal/app/keymap.go

This file was deleted.

5 changes: 3 additions & 2 deletions internal/app/statefiltering.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"

"github.com/hedhyw/json-log-viewer/internal/keymap"
"github.com/hedhyw/json-log-viewer/internal/pkg/events"
)

Expand All @@ -16,7 +17,7 @@ type StateFilteringModel struct {
table logsTableModel

textInput textinput.Model
keys KeyMap
keys keymap.KeyMap
}

func newStateFiltering(
Expand All @@ -32,7 +33,7 @@ func newStateFiltering(
table: previousState.table,

textInput: textInput,
keys: defaultKeys,
keys: previousState.getApplication().keys,
}
}

Expand Down
15 changes: 11 additions & 4 deletions internal/app/stateviewrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
tea "github.com/charmbracelet/bubbletea"

"github.com/hedhyw/json-log-viewer/internal/keymap"
"github.com/hedhyw/json-log-viewer/internal/pkg/events"
"github.com/hedhyw/json-log-viewer/internal/pkg/source"
"github.com/hedhyw/json-log-viewer/internal/pkg/widgets"
Expand All @@ -18,25 +19,31 @@ type StateViewRowModel struct {
logEntry source.LogEntry
jsonView tea.Model

keys KeyMap
keys keymap.KeyMap
}

func newStateViewRow(
logEntry source.LogEntry,
previousState stateModel,
) StateViewRowModel {
jsonViewModel, cmd := widgets.NewJSONViewModel(logEntry.Line, previousState.getApplication().LastWindowSize)
app := previousState.getApplication()

jsonViewModel, cmd := widgets.NewJSONViewModel(
logEntry.Line,
app.LastWindowSize,
app.keys,
)

return StateViewRowModel{
Application: previousState.getApplication(),
Application: app,

previousState: previousState,
initCmd: cmd,

logEntry: logEntry,
jsonView: jsonViewModel,

keys: defaultKeys,
keys: previousState.getApplication().keys,
}
}

Expand Down
84 changes: 84 additions & 0 deletions internal/keymap/keymap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package keymap

import "github.com/charmbracelet/bubbles/key"

// KeyMap of the app.
type KeyMap struct {
Exit key.Binding
Back key.Binding
Open key.Binding
ToggleViewArrow key.Binding
Up key.Binding
Reverse key.Binding
Down key.Binding
Filter key.Binding
ToggleFullHelp key.Binding
GotoTop key.Binding
GotoBottom key.Binding
ShowPreview key.Binding
}

// GetDefaultKeys returns default KeyMap.
func GetDefaultKeys() KeyMap {
return KeyMap{
Exit: key.NewBinding(
key.WithKeys("ctrl+c", "f10"),
key.WithHelp("Ctrl+C", "Exit"),
),
Back: key.NewBinding(
key.WithKeys("esc", "q"),
key.WithHelp("esc", "Back"),
),
Open: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "Open"),
),
ToggleViewArrow: key.NewBinding(
key.WithKeys("right"),
),
Up: key.NewBinding(
key.WithKeys("up"),
key.WithHelp("↑", "Up"),
),
Reverse: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "Reverse"),
),
Down: key.NewBinding(
key.WithKeys("down"),
key.WithHelp("↓", "Down"),
),
Filter: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "Filter"),
),
ToggleFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "Help"),
),
GotoTop: key.NewBinding(
key.WithKeys("home"),
key.WithHelp("home", "go to start"),
),
GotoBottom: key.NewBinding(
key.WithKeys("end", "G"),
key.WithHelp("end", "go to end"),
),
}
}

func (k KeyMap) ShortHelp() []key.Binding {
return []key.Binding{
k.Back, k.Open, k.Up, k.Down, k.ToggleFullHelp,
}
}

func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down},
{k.Back, k.Open},
{k.Filter, k.Reverse},
{k.GotoTop, k.GotoBottom},
{k.ToggleFullHelp, k.Exit},
}
}
10 changes: 8 additions & 2 deletions internal/pkg/widgets/jsonview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

fx "github.com/antonmedv/fx/pkg/model"
tea "github.com/charmbracelet/bubbletea"

"github.com/hedhyw/json-log-viewer/internal/keymap"
)

const themeFX = "1"
Expand All @@ -16,13 +18,17 @@ func init() {

// NewJSONViewModel creates a new JSON view widget if a content is the correct json,
// or plain text view otherwise.
func NewJSONViewModel(content []byte, lastWindowSize tea.WindowSizeMsg) (tea.Model, tea.Cmd) {
func NewJSONViewModel(
content []byte,
lastWindowSize tea.WindowSizeMsg,
keyMap keymap.KeyMap,
) (tea.Model, tea.Cmd) {
fxModel, err := fx.New(fx.Config{
FileName: "",
Source: bytes.NewReader(content),
})
if err != nil {
return NewPlainLogModel(string(content), lastWindowSize)
return NewPlainLogModel(string(content), lastWindowSize, keyMap)
}

return fxModel.Update(lastWindowSize)
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/widgets/jsonview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package widgets_test
import (
"testing"

"github.com/hedhyw/json-log-viewer/internal/keymap"
"github.com/hedhyw/json-log-viewer/internal/pkg/widgets"

"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +15,11 @@ func TestNewJSONViewModel(t *testing.T) {
t.Run("plain_text", func(t *testing.T) {
t.Parallel()

model, _ := widgets.NewJSONViewModel([]byte(text), getFakeTeaWindowSizeMsg())
model, _ := widgets.NewJSONViewModel(
[]byte(text),
getFakeTeaWindowSizeMsg(),
keymap.GetDefaultKeys(),
)

_, ok := model.(widgets.PlainLogModel)
assert.Truef(t, ok, "actual type: %T", model)
Expand All @@ -26,6 +31,7 @@ func TestNewJSONViewModel(t *testing.T) {
model, _ := widgets.NewJSONViewModel(
[]byte(`{"hello":"world"}`),
getFakeTeaWindowSizeMsg(),
keymap.GetDefaultKeys(),
)

_, ok := model.(widgets.PlainLogModel)
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/widgets/plain.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package widgets

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/reflow/wordwrap"

"github.com/hedhyw/json-log-viewer/internal/keymap"
)

// PlainLogModel is a widget that shows multiline text in a viewport.
type PlainLogModel struct {
viewport viewport.Model
text string
keyMap keymap.KeyMap
}

// NewPlainLogModel initializes a new PlainLogModel with the given text.
// It updates a widget with the message `tea.WindowSizeMsg`.
func NewPlainLogModel(
text string,
windowSize tea.WindowSizeMsg,
keyMap keymap.KeyMap,
) (PlainLogModel, tea.Cmd) {
m := PlainLogModel{
text: text,
viewport: viewport.New(windowSize.Width, windowSize.Height),
keyMap: keyMap,
}

m = m.refreshText(windowSize.Width)
Expand All @@ -47,6 +53,12 @@ func (m PlainLogModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
m = m.refreshText(msg.Width)
case tea.KeyMsg:
if key.Matches(msg, m.keyMap.Exit) ||
key.Matches(msg, m.keyMap.Back) ||
key.Matches(msg, m.keyMap.Open) {
return m, tea.Quit
}
}

var cmd tea.Cmd
Expand Down
Loading

0 comments on commit 07f80b4

Please sign in to comment.