package filter import ( "encoding/json" "fmt" "log" "os" "strings" "git.andreafazzi.eu/andrea/probo/pkg/store/file" "github.com/TylerBrock/colorjson" "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 { store []any } type resultMsg struct { result []any } type errorMsg struct { error error } type FilterModel struct { // UI textInput *textinput.Model viewport *viewport.Model table *table.Model group *group.Model help help.Model statusBar *statusbar.Model spinner spinner.Model // Layout document *layout.Layout // Key bindings bindings *keyBindings // file store store []any result []any // json lastQuery string FilteredJson string InputJson string state int filterType string } type keyBindings struct { group *group.Model quit, enter key.Binding } func (k *keyBindings) ShortHelp() []key.Binding { keys := make([]key.Binding, 0) current := k.group.Current() switch item := current.(type) { case *table.Model: keys = append( keys, item.KeyMap.LineUp, item.KeyMap.LineDown, ) } keys = append( keys, k.quit, ) return keys } func (k keyBindings) FullHelp() [][]key.Binding { return [][]key.Binding{ { k.quit, }, } } func newBindings(g *group.Model) *keyBindings { return &keyBindings{ group: g, quit: key.NewBinding( key.WithKeys("esc"), key.WithHelp("esc", "Quit app"), ), enter: key.NewBinding( key.WithKeys("enter"), key.WithHelp("enter", "Quit app and return the results"), ), } } func New(path string, filterType string, stdin string) *FilterModel { textInput := textinput.New( 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.WithLayout( layout.New( layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 0, 1, 0)}), layout.WithItem(textInput), layout.WithItem(tiled.New(viewport, tabl)), ), ), ) bindings := newBindings(group) statusBar := statusbar.New(bindings) s := spinner.New( spinner.WithStyle( lipgloss.NewStyle().Foreground(lipgloss.Color("265"))), ) s.Spinner = spinner.Dot header := header.New( header.WithContent( lipgloss.NewStyle(). Bold(true). Border(lipgloss.NormalBorder(), false, false, true, false). Render(filterTypeFormats[filterType]), ), ) document := layout.New( layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}), layout.WithItem(header), layout.WithItem(group), layout.WithItem(statusBar), ) if path != "" { jq, err := os.ReadFile(path) if err != nil { panic(err) } textInput.SetValue(strings.TrimSpace(string(jq))) } return &FilterModel{ textInput: textInput, viewport: viewport, table: tabl, group: group, statusBar: statusBar, spinner: s, document: document, bindings: bindings, help: help, filterType: filterType, InputJson: stdin, } } func (m *FilterModel) Init() tea.Cmd { var cmds []tea.Cmd cmds = append(cmds, m.group.Init(), m.loadStore(), m.spinner.Tick) m.group.Focus() return tea.Batch(cmds...) } func (m *FilterModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { case tea.WindowSizeMsg: m.document.SetSize(msg.Width, msg.Height) case tea.KeyMsg: switch { case key.Matches(msg, m.bindings.quit): m.FilteredJson = "" return m, tea.Quit case key.Matches(msg, m.bindings.enter): m.FilteredJson = strings.Join([]string{ fmt.Sprintf("{\"%s\": %s,", m.filterType, m.FilteredJson), strings.TrimLeft(m.InputJson, "{")}, "\n") return m, tea.Quit } case storeLoadedMsg: cmds = append(cmds, m.handleStoreLoaded(msg)) case resultMsg: cmds = append(cmds, m.handleFiltered(msg)) m.state = FilterState case errorMsg: m.handleError(msg) m.state = ErrorState } cmds = m.handleState(msg, cmds) return m, tea.Batch(cmds...) } func (m *FilterModel) View() string { return m.document.View() } func (m *FilterModel) showErrorOnStatusBar(err error) { m.statusBar.SetContent( stateFormats[ErrorState][0], fmt.Sprintf(stateFormats[ErrorState][1], err), stateFormats[ErrorState][2], ) } func (m *FilterModel) handleError(msg tea.Msg) { err := msg.(errorMsg) m.statusBar.SetContent( stateFormats[ErrorState][0], fmt.Sprintf(stateFormats[ErrorState][1], err.error), stateFormats[ErrorState][2], ) } func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd { return func() tea.Msg { storeMsg := msg.(storeLoadedMsg) m.store = storeMsg.store m.state = FilterState coloredJson, err := toColoredJson(m.store) if err != nil { 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 } } func (m *FilterModel) handleFiltered(msg tea.Msg) tea.Cmd { return func() tea.Msg { m.result = msg.(resultMsg).result coloredJson, err := toColoredJson(m.result) if err != nil { return errorMsg{err} } json, err := toJson(m.result) if err != nil { return errorMsg{err} } m.FilteredJson = json m.viewport.SetContent(coloredJson) err = m.updateTableContent(json) if err != nil { return errorMsg{err} } return nil } } func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd { _, cmd := m.group.Update(msg) if m.state == LoadingStoreState { return m.updateSpinner(msg, cmd, cmds) } cmds = append(cmds, cmd, m.query(m.textInput.Value())) if m.state != ErrorState { m.statusBar.SetContent(stateFormats[FilterState]...) } 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) m.statusBar.SetContent(fmt.Sprintf(stateFormats[m.state][0], m.spinner.View()), stateFormats[m.state][1], stateFormats[m.state][2]) cmds = append(cmds, cmd) return cmds } func (m *FilterModel) loadStore() tea.Cmd { return func() tea.Msg { var jsonStore []byte if m.filterType == "participants" { pStore, err := file.NewDefaultParticipantFileStore() if err != nil { return errorMsg{err} } jsonStore, err = pStore.Storer.Json() if err != nil { return errorMsg{err} } } else if m.filterType == "quizzes" { qStore, err := file.NewDefaultQuizFileStore() if err != nil { return errorMsg{err} } jsonStore, err = qStore.Storer.Json() if err != nil { return errorMsg{err} } } else { panic("Unknown filter type!") } v := make([]any, 0) err := json.Unmarshal(jsonStore, &v) if err != nil { return errorMsg{err} } return storeLoadedMsg{v} } } func (m *FilterModel) query(input string) tea.Cmd { return func() tea.Msg { if input == m.lastQuery { return nil } if m.state == LoadingStoreState { return nil } m.lastQuery = input query, err := gojq.Parse(input) if err != nil { return errorMsg{fmt.Errorf("jq query parse error: %v", err)} } var result []string iter := query.Run(m.store) for { v, ok := iter.Next() if !ok { break } if err, ok := v.(error); ok { return errorMsg{fmt.Errorf("jq query run error: %v", err)} } b, err := json.MarshalIndent(v, "", " ") if err != nil { return errorMsg{err} } result = append(result, string(b)) } v := make([]any, 0) err = json.Unmarshal([]byte(strings.Join(result, "\n")), &v) if err != nil { return errorMsg{err} } return resultMsg{v} } } func toColoredJson(data []any) (string, error) { f := colorjson.NewFormatter() f.Indent = 2 result, err := f.Marshal(data) if err != nil { return "", err } return sanitize(string(result)), nil } func toJson(data []any) (string, error) { result, err := json.Marshal(data) if err != nil { return "", err } return string(result), nil } func sanitize(text string) string { // FIXME: The use of a standard '-' character causes rendering // issues within the viewport. Further investigation is // required to resolve this problem. return strings.Replace(text, "-", "–", -1) }