Remove table from JSON filter

This commit is contained in:
Andrea Fazzi 2024-04-10 08:51:51 +02:00
parent 2331ca03b2
commit e161d936aa

View file

@ -3,7 +3,6 @@ package filter
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
@ -12,18 +11,15 @@ import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
teatable "github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/itchyny/gojq"
"github.com/remogatto/sugarfoam/components/group"
"github.com/remogatto/sugarfoam/components/header"
"github.com/remogatto/sugarfoam/components/statusbar"
"github.com/remogatto/sugarfoam/components/table"
"github.com/remogatto/sugarfoam/components/textinput"
"github.com/remogatto/sugarfoam/components/viewport"
"github.com/remogatto/sugarfoam/layout"
"github.com/remogatto/sugarfoam/layout/tiled"
)
type storeLoadedMsg struct {
@ -42,7 +38,6 @@ type FilterModel struct {
// UI
textInput *textinput.Model
viewport *viewport.Model
table *table.Model
group *group.Model
help help.Model
statusBar *statusbar.Model
@ -80,11 +75,11 @@ func (k *keyBindings) ShortHelp() []key.Binding {
current := k.group.Current()
switch item := current.(type) {
case *table.Model:
case *viewport.Model:
keys = append(
keys,
item.KeyMap.LineUp,
item.KeyMap.LineDown,
item.KeyMap.Up,
item.KeyMap.Down,
)
}
@ -121,37 +116,17 @@ func New(path string, filterType string, stdin string) *FilterModel {
textinput.WithPlaceholder("Write your jq filter here..."),
)
var tabl *table.Model
if filterType == "participants" {
tabl = table.New(table.WithRelWidths(10, 40, 40, 10))
tabl.Model.SetColumns([]teatable.Column{
{Title: "ID", Width: 20},
{Title: "Lastname", Width: 10},
{Title: "Firstname", Width: 10},
{Title: "Token", Width: 10},
})
} else if filterType == "quizzes" {
tabl = table.New(table.WithRelWidths(20, 80))
tabl.Model.SetColumns([]teatable.Column{
{Title: "ID", Width: 20},
{Title: "Question", Width: 10},
})
} else {
panic("Unknown filter type!")
}
viewport := viewport.New()
help := help.New()
group := group.New(
group.WithItems(textInput, viewport, tabl),
group.WithItems(textInput, viewport),
group.WithLayout(
layout.New(
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 0, 1, 0)}),
layout.WithItem(textInput),
layout.WithItem(tiled.New(viewport, tabl)),
layout.WithItem(viewport),
),
),
)
@ -192,7 +167,6 @@ func New(path string, filterType string, stdin string) *FilterModel {
return &FilterModel{
textInput: textInput,
viewport: viewport,
table: tabl,
group: group,
statusBar: statusBar,
spinner: s,
@ -285,18 +259,8 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
return errorMsg{err}
}
json, err := toJson(m.store)
if err != nil {
return errorMsg{err}
}
m.viewport.SetContent(coloredJson)
err = m.updateTableContent(json)
if err != nil {
return errorMsg{err}
}
return nil
}
}
@ -319,11 +283,6 @@ func (m *FilterModel) handleFiltered(msg tea.Msg) tea.Cmd {
m.viewport.SetContent(coloredJson)
err = m.updateTableContent(json)
if err != nil {
return errorMsg{err}
}
return nil
}
}
@ -344,48 +303,6 @@ func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
return cmds
}
func (m *FilterModel) updateTableContent(jsonData string) error {
elements := make([]map[string]string, 0)
columns := make([]teatable.Column, 0)
rows := make([]teatable.Row, 0)
err := json.Unmarshal([]byte(jsonData), &elements)
if err != nil {
return err
}
if len(elements) > 0 {
for title := range elements[0] {
columns = append(columns, teatable.Column{Title: title, Width: 5})
}
}
for _, el := range elements {
cells := make([]string, 0)
for _, cell := range el {
cells = append(cells, cell)
}
rows = append(rows, teatable.Row(cells))
}
percentage := 100 / len(columns)
percs := make([]int, 0)
for i := 0; i < len(columns); i++ {
percs = append(percs, percentage)
}
log.Println(percs)
m.table.Model.SetColumns(columns)
m.table.Model.SetRows(rows)
m.table.SetRelWidths(percs...)
return nil
}
func (m *FilterModel) updateSpinner(msg tea.Msg, cmd tea.Cmd, cmds []tea.Cmd) []tea.Cmd {
m.spinner, cmd = m.spinner.Update(msg)