diff --git a/input/config.go b/input/config.go index 761fd00..dacc90a 100644 --- a/input/config.go +++ b/input/config.go @@ -4,6 +4,9 @@ import ( "encoding/json" "fmt" "os" + "strings" + + "github.com/charmbracelet/bubbles/table" ) var configFile = ".plog.json" @@ -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 +} diff --git a/input/handler.go b/input/handler.go index 09690ef..7cc2874 100644 --- a/input/handler.go +++ b/input/handler.go @@ -3,7 +3,6 @@ package input import ( "bufio" "io" - "strings" "github.com/acarl005/stripansi" "github.com/charmbracelet/bubbles/table" @@ -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 @@ -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() { diff --git a/view/app.go b/view/app.go index fe1581f..d94e7b1 100644 --- a/view/app.go +++ b/view/app.go @@ -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, diff --git a/view/init.go b/view/init.go index 157c8f9..81ed12b 100644 --- a/view/init.go +++ b/view/init.go @@ -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), diff --git a/view/select.go b/view/select.go index ea4d0c2..05a251a 100644 --- a/view/select.go +++ b/view/select.go @@ -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 } diff --git a/view/table.go b/view/table.go index 0e82cdd..a5f1ea8 100644 --- a/view/table.go +++ b/view/table.go @@ -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 { @@ -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")) @@ -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 } @@ -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) @@ -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 }