-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
17 changed files
with
312 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ vendor/ | |
|
||
# Config | ||
.jlv.jsonc | ||
|
||
## Logs | ||
/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/hedhyw/json-log-viewer/internal/pkg/config" | ||
) | ||
|
||
// rowGetter renders the row. | ||
type rowGetter interface { | ||
// Row return a rendered table row. | ||
Row(cfg *config.Config) table.Row | ||
} | ||
|
||
// lazyTableModel lazily renders table rows. | ||
type lazyTableModel[T rowGetter] struct { | ||
helper | ||
|
||
table table.Model | ||
|
||
minRenderedRows int | ||
allEntries []T | ||
lastCursor int | ||
|
||
renderedRows []table.Row | ||
} | ||
|
||
// Init implements tea.Model. | ||
func (m lazyTableModel[T]) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
// View implements tea.Model. | ||
func (m lazyTableModel[T]) View() string { | ||
return m.table.View() | ||
} | ||
|
||
// Update implements tea.Model. | ||
func (m lazyTableModel[T]) Update(msg tea.Msg) (lazyTableModel[T], tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
m.table, cmd = m.table.Update(msg) | ||
|
||
if m.table.Cursor() != m.lastCursor { | ||
m = m.withRenderedRows() | ||
} | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m lazyTableModel[T]) withRenderedRows() lazyTableModel[T] { | ||
cursor := m.table.Cursor() | ||
|
||
start := max(len(m.renderedRows), cursor) | ||
end := min(cursor+m.minRenderedRows, len(m.allEntries)) | ||
|
||
for i := start; i < end; i++ { | ||
m.renderedRows = append(m.renderedRows, m.allEntries[i].Row(m.Config)) | ||
} | ||
|
||
m.table.SetRows(m.renderedRows) | ||
m.lastCursor = m.table.Cursor() | ||
|
||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.