Remove table from JSON filter
This commit is contained in:
parent
2331ca03b2
commit
e161d936aa
1 changed files with 5 additions and 88 deletions
|
@ -3,7 +3,6 @@ package filter
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -12,18 +11,15 @@ import (
|
||||||
"github.com/charmbracelet/bubbles/help"
|
"github.com/charmbracelet/bubbles/help"
|
||||||
"github.com/charmbracelet/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"github.com/charmbracelet/bubbles/spinner"
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
teatable "github.com/charmbracelet/bubbles/table"
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/itchyny/gojq"
|
"github.com/itchyny/gojq"
|
||||||
"github.com/remogatto/sugarfoam/components/group"
|
"github.com/remogatto/sugarfoam/components/group"
|
||||||
"github.com/remogatto/sugarfoam/components/header"
|
"github.com/remogatto/sugarfoam/components/header"
|
||||||
"github.com/remogatto/sugarfoam/components/statusbar"
|
"github.com/remogatto/sugarfoam/components/statusbar"
|
||||||
"github.com/remogatto/sugarfoam/components/table"
|
|
||||||
"github.com/remogatto/sugarfoam/components/textinput"
|
"github.com/remogatto/sugarfoam/components/textinput"
|
||||||
"github.com/remogatto/sugarfoam/components/viewport"
|
"github.com/remogatto/sugarfoam/components/viewport"
|
||||||
"github.com/remogatto/sugarfoam/layout"
|
"github.com/remogatto/sugarfoam/layout"
|
||||||
"github.com/remogatto/sugarfoam/layout/tiled"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type storeLoadedMsg struct {
|
type storeLoadedMsg struct {
|
||||||
|
@ -42,7 +38,6 @@ type FilterModel struct {
|
||||||
// UI
|
// UI
|
||||||
textInput *textinput.Model
|
textInput *textinput.Model
|
||||||
viewport *viewport.Model
|
viewport *viewport.Model
|
||||||
table *table.Model
|
|
||||||
group *group.Model
|
group *group.Model
|
||||||
help help.Model
|
help help.Model
|
||||||
statusBar *statusbar.Model
|
statusBar *statusbar.Model
|
||||||
|
@ -80,11 +75,11 @@ func (k *keyBindings) ShortHelp() []key.Binding {
|
||||||
current := k.group.Current()
|
current := k.group.Current()
|
||||||
|
|
||||||
switch item := current.(type) {
|
switch item := current.(type) {
|
||||||
case *table.Model:
|
case *viewport.Model:
|
||||||
keys = append(
|
keys = append(
|
||||||
keys,
|
keys,
|
||||||
item.KeyMap.LineUp,
|
item.KeyMap.Up,
|
||||||
item.KeyMap.LineDown,
|
item.KeyMap.Down,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,37 +116,17 @@ func New(path string, filterType string, stdin string) *FilterModel {
|
||||||
textinput.WithPlaceholder("Write your jq filter here..."),
|
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()
|
viewport := viewport.New()
|
||||||
|
|
||||||
help := help.New()
|
help := help.New()
|
||||||
|
|
||||||
group := group.New(
|
group := group.New(
|
||||||
group.WithItems(textInput, viewport, tabl),
|
group.WithItems(textInput, viewport),
|
||||||
group.WithLayout(
|
group.WithLayout(
|
||||||
layout.New(
|
layout.New(
|
||||||
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 0, 1, 0)}),
|
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 0, 1, 0)}),
|
||||||
layout.WithItem(textInput),
|
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{
|
return &FilterModel{
|
||||||
textInput: textInput,
|
textInput: textInput,
|
||||||
viewport: viewport,
|
viewport: viewport,
|
||||||
table: tabl,
|
|
||||||
group: group,
|
group: group,
|
||||||
statusBar: statusBar,
|
statusBar: statusBar,
|
||||||
spinner: s,
|
spinner: s,
|
||||||
|
@ -285,18 +259,8 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
|
||||||
return errorMsg{err}
|
return errorMsg{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := toJson(m.store)
|
|
||||||
if err != nil {
|
|
||||||
return errorMsg{err}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.viewport.SetContent(coloredJson)
|
m.viewport.SetContent(coloredJson)
|
||||||
|
|
||||||
err = m.updateTableContent(json)
|
|
||||||
if err != nil {
|
|
||||||
return errorMsg{err}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -319,11 +283,6 @@ func (m *FilterModel) handleFiltered(msg tea.Msg) tea.Cmd {
|
||||||
|
|
||||||
m.viewport.SetContent(coloredJson)
|
m.viewport.SetContent(coloredJson)
|
||||||
|
|
||||||
err = m.updateTableContent(json)
|
|
||||||
if err != nil {
|
|
||||||
return errorMsg{err}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -344,48 +303,6 @@ func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
|
||||||
return cmds
|
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 {
|
func (m *FilterModel) updateSpinner(msg tea.Msg, cmd tea.Cmd, cmds []tea.Cmd) []tea.Cmd {
|
||||||
m.spinner, cmd = m.spinner.Update(msg)
|
m.spinner, cmd = m.spinner.Update(msg)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue