Skip to content

Commit

Permalink
Relative sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
mnbjhu committed Sep 9, 2024
1 parent 1d31d92 commit 0405bf4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
35 changes: 35 additions & 0 deletions input/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/charmbracelet/bubbles/table"
)

var configFile = ".plog.json"
Expand Down Expand Up @@ -67,3 +70,35 @@ func GetConfig() Config {
}
return config
}

func (c Config) GetColumns(expected int) []table.Column {
columns := make([]table.Column, len(c.Columns))
total := 0.0
for _, col := range c.Columns {
total += float64(col.Width)
}
expected -= len(c.Columns) * 2
prev := 0
rel := 0
for i, col := range c.Columns {
rel += col.Width
new := int(float64(rel) * float64(expected) / total)
width := new - prev
columns[i] = table.Column{Width: width, Title: col.Title}
prev = new
}
return columns
}

func (c Config) GetLevelColumnIndex() int {
for i, col := range c.Columns {
if strings.ToLower(col.Title) == "level" {
return i
}
}
return -1
}

func (c Config) GetMsgColumnIndex() int {
return len(c.Columns) - 1
}
22 changes: 2 additions & 20 deletions input/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package input
import (
"bufio"
"io"
"strings"

"github.com/acarl005/stripansi"
"github.com/charmbracelet/bubbles/table"
Expand All @@ -16,7 +15,7 @@ var (
Matcher = regroup.MustCompile(logRegex)
)

type Log4jHandler struct {
type LogHandler struct {
MsgAppender chan string
RowAppender chan table.Row
Reader io.Reader
Expand All @@ -25,24 +24,7 @@ type Log4jHandler struct {
LeadingSize int
}

func (h Log4jHandler) GetColumns() []string {
return h.Columns
}

func (h Log4jHandler) GetLevelColumnIndex() int {
for i, col := range h.GetColumns() {
if strings.ToLower(col) == "level" {
return i
}
}
return -1
}

func (h Log4jHandler) GetMsgColumnIndex() int {
return len(h.GetColumns()) - 1
}

func (h Log4jHandler) HandleLog() tea.Cmd {
func (h LogHandler) HandleLog() tea.Cmd {
return func() tea.Msg {
scanner := bufio.NewScanner(h.Reader)
for scanner.Scan() {
Expand Down
2 changes: 1 addition & 1 deletion view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewAppModel(out io.Reader, config input.Config) AppModel {
for _, col := range config.Columns {
columns = append(columns, col.Title)
}
handler := input.Log4jHandler{
handler := input.LogHandler{
MsgAppender: logs.MsgChannel,
RowAppender: logs.LogChannel,
Reader: out,
Expand Down
2 changes: 1 addition & 1 deletion view/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Init() {
Options(huh.NewOption("Word", "(?<%s>\\w+)"),
huh.NewOption("Integer", "(?<%s>\\d+)"),
huh.NewOption("Float", "(?<%s>\\d+\\.\\d+)"),
huh.NewOption("Any", "(?<%s>\\w+)"),
huh.NewOption("Any", "(?<%s>[^\\s]+)"),
huh.NewOption("Bracketed", "\\[(?<%s>[^\\[]+)\\]"),
huh.NewOption("Rest", "rest"),
).Value(&expr),
Expand Down
2 changes: 1 addition & 1 deletion view/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m SelectModel) View() string {
}

func (m SelectModel) Resize(width, height int) SelectModel {
m.Viewport.Width = width
m.Viewport.Width = width - 2
m.Viewport.Height = height
return m
}
22 changes: 9 additions & 13 deletions view/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type TableModel struct {
Table table.Model
MsgChannel chan string
LogChannel chan table.Row
LogHandler input.Log4jHandler
LogHandler input.LogHandler
Config *input.Config
}

func NewTableModel(config input.Config) TableModel {
Expand All @@ -42,7 +43,7 @@ func NewTableModel(config input.Config) TableModel {
table.WithFocused(true),
table.WithHeight(7),
table.WithStyleFunc(func(row, col int, value string) lipgloss.Style {
if col == 1 {
if col == config.GetLevelColumnIndex() {
switch value {
case "ERROR":
return lipgloss.NewStyle().Foreground(lipgloss.Color("196"))
Expand All @@ -63,7 +64,7 @@ func NewTableModel(config input.Config) TableModel {
t.SetColumns(columns)
msgChan := make(chan string)
logChan := make(chan table.Row)
m := TableModel{Table: t, MsgChannel: msgChan, LogChannel: logChan}
m := TableModel{Table: t, MsgChannel: msgChan, LogChannel: logChan, Config: &config}
return m
}

Expand Down Expand Up @@ -94,13 +95,13 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
text := m.Table.SelectedRow()[m.LogHandler.GetMsgColumnIndex()]
text := m.Table.SelectedRow()[m.Config.GetMsgColumnIndex()]
return m, SelectMsg(&text)
}
case appendLogMsg:
rows := m.Table.Rows()
if len(rows) > 0 {
index := m.LogHandler.GetMsgColumnIndex()
index := m.Config.GetMsgColumnIndex()
current := rows[len(rows)-1][index]
rows[len(rows)-1][index] = current + "\n" + msg.Text
m.Table.SetRows(rows)
Expand All @@ -125,14 +126,9 @@ func (m TableModel) View() string {
}

func (m TableModel) Resize(width, height int) TableModel {
m.Table.SetWidth(width - 2)
m.Table.SetWidth(width)
m.Table.SetHeight(height)
columns := m.Table.Columns()
colWidth := width - m.LogHandler.LeadingSize + 30
if colWidth < 4 {
colWidth = 4
}
columns[m.LogHandler.GetMsgColumnIndex()].Width = colWidth
m.Table.SetColumns(columns)

m.Table.SetColumns(m.Config.GetColumns(width))
return m
}

0 comments on commit 0405bf4

Please sign in to comment.