Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: colors #113

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 28 additions & 21 deletions internal/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
)

func (app *Application) getLogLevelStyle(
logEntries source.LazyLogEntries,
renderedRows []table.Row,
baseStyle lipgloss.Style,
rowID int,
) lipgloss.Style {
if rowID < 0 || rowID >= logEntries.Len() {
if rowID < 0 || rowID >= len(renderedRows) {
return baseStyle
}

entry := logEntries.Entries[rowID].LogEntry(logEntries.Seeker, app.Config)
row := renderedRows[rowID]

color := getColorForLogLevel(app.getLogLevelFromLogEntry(entry))
color := getColorForLogLevel(app.getLogLevelFromLogRow(row))
if color == "" {
return baseStyle
}
Expand Down Expand Up @@ -62,8 +62,8 @@ func getColorForLogLevel(level source.Level) lipgloss.Color {
}
}

func (app *Application) getLogLevelFromLogEntry(logEntry source.LogEntry) source.Level {
return source.Level(getFieldByKind(app.Config, config.FieldKindLevel, logEntry))
func (app *Application) getLogLevelFromLogRow(row table.Row) source.Level {
return source.Level(getCellByKind(app.Config, config.FieldKindLevel, row))
}

func (app *Application) handleErrorOccuredMsg(msg events.ErrorOccuredMsg) (tea.Model, tea.Cmd) {
Expand Down 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 Expand Up @@ -151,24 +151,31 @@ func removeClearSequence(value string) string {
return strings.ReplaceAll(value, "\x1b[0", "\x1b[39")
}

func getFieldByKind(
func getCellByKind(
cfg *config.Config,
kind config.FieldKind,
logEntry source.LogEntry,
row table.Row,
) string {
for i, f := range cfg.Fields {
if f.Kind != kind {
continue
}
index := getIndexByKind(cfg, kind)

if i >= len(logEntry.Fields) {
return "-"
}
if index < 0 || index >= len(row) {
return "-"
}

return row[index]
}

return logEntry.Fields[i]
func getIndexByKind(
cfg *config.Config,
kind config.FieldKind,
) int {
for i, f := range cfg.Fields {
if f.Kind == kind {
return i
}
}

return ""
return -1
}

func batched[T any](m T, cmd tea.Cmd) func(batch []tea.Cmd) (T, []tea.Cmd) {
Expand Down
80 changes: 0 additions & 80 deletions internal/app/keymap.go

This file was deleted.

34 changes: 33 additions & 1 deletion internal/app/lazytable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
tea "github.com/charmbracelet/bubbletea"

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

// rowGetter renders the row.
type rowGetter interface {
// Row return a rendered table row.
Row(cfg *config.Config, i int) table.Row
// Len returns the number of all rows.
Len() int
// LogEntry getter
LogEntry(cfg *config.Config, i int) source.LogEntry
}

// lazyTableModel lazily renders table rows.
Expand Down Expand Up @@ -67,6 +71,27 @@ func (m lazyTableModel) Update(msg tea.Msg) (lazyTableModel, tea.Cmd) {
return m, cmd
}

func (m lazyTableModel) getCellRenderer() func(table.Model, string, table.CellPosition) string {
cellIDLogLevel := getIndexByKind(m.Config, config.FieldKindLevel)
tableStyles := getTableStyles()

return func(_ table.Model, value string, position table.CellPosition) string {
style := tableStyles.Cell

if position.Column == cellIDLogLevel {
return removeClearSequence(
m.Application.getLogLevelStyle(
m.renderedRows,
style,
position.RowID,
).Render(value),
)
}

return style.Render(value)
}
}

func (m lazyTableModel) handleKey(msg tea.KeyMsg, render bool) (lazyTableModel, bool) {
// toggle the reverse display of items.
if key.Matches(msg, m.Application.keys.Reverse) {
Expand Down Expand Up @@ -170,13 +195,16 @@ func (m lazyTableModel) RenderedRows() lazyTableModel {
}
end := min(m.offset+m.table.Height(), m.entries.Len())

m.renderedRows = []table.Row{}
m.renderedRows = m.renderedRows[:0]
renderedEntries := make([]source.LogEntry, 0, cap(m.renderedRows))
for i := m.offset; i < end; i++ {
m.renderedRows = append(m.renderedRows, m.entries.Row(m.Config, i))
renderedEntries = append(renderedEntries, m.entries.LogEntry(m.Config, i))
}

if m.reverse {
slices.Reverse(m.renderedRows)
slices.Reverse(renderedEntries)
}

m.table.SetRows(m.renderedRows)
Expand All @@ -190,5 +218,9 @@ func (m lazyTableModel) RenderedRows() lazyTableModel {

m.lastCursor = m.table.Cursor()

tableStyles := getTableStyles()
tableStyles.RenderCell = m.getCellRenderer()
m.table.SetStyles(tableStyles)

return m
}
21 changes: 0 additions & 21 deletions internal/app/logstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type logsTableModel struct {
}

func newLogsTableModel(application *Application, logEntries source.LazyLogEntries) logsTableModel {
const cellIDLogLevel = 1

tableLogs := table.New(
table.WithColumns(getColumns(application.LastWindowSize.Width, application.Config)),
table.WithFocused(true),
Expand All @@ -34,25 +32,6 @@ func newLogsTableModel(application *Application, logEntries source.LazyLogEntrie

tableLogs.SetStyles(getTableStyles())

tableStyles := getTableStyles()
tableStyles.RenderCell = func(_ table.Model, value string, position table.CellPosition) string {
style := tableStyles.Cell

if position.Column == cellIDLogLevel {
return removeClearSequence(
application.getLogLevelStyle(
logEntries,
style,
position.RowID,
).Render(value),
)
}

return style.Render(value)
}

tableLogs.SetStyles(tableStyles)

lazyTable := lazyTableModel{
Application: application,
reverse: true,
Expand Down
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
Loading
Loading