Generalize filters

This commit is contained in:
Andrea Fazzi 2024-04-10 08:44:49 +02:00
parent 588cf064c1
commit 2331ca03b2
5 changed files with 294 additions and 56 deletions

View file

@ -1,6 +1,5 @@
/* /*
Copyright © 2024 NAME HERE <EMAIL ADDRESS> Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/ */
package cmd package cmd
@ -32,7 +31,9 @@ func init() {
// Cobra supports Persistent Flags which will work for this command // Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.: // and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
createCmd.PersistentFlags().StringP("input", "i", "", "Specify an input file")
createCmd.PersistentFlags().Bool("stdin", false, "Consume a string from stdin")
// Cobra supports local flags which will only run when this command // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:

View file

@ -9,7 +9,13 @@ import (
"git.andreafazzi.eu/andrea/probo/cmd/filter" "git.andreafazzi.eu/andrea/probo/cmd/filter"
"bufio"
"io"
"strings"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -32,7 +38,7 @@ func init() {
// Cobra supports local flags which will only run when this command // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:
// filterCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") filterCmd.Flags().StringP("type", "t", "participants", "Select the type of filter (participants or quizzes)")
} }
func createFilter(cmd *cobra.Command, args []string) { func createFilter(cmd *cobra.Command, args []string) {
@ -45,9 +51,51 @@ func createFilter(cmd *cobra.Command, args []string) {
defer f.Close() defer f.Close()
} }
if _, err := tea.NewProgram(filter.New()).Run(); err != nil { var b strings.Builder
stat, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if stat.Mode()&os.ModeNamedPipe != 0 || stat.Size() != 0 {
reader := bufio.NewReader(os.Stdin)
for {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
fmt.Println("Error getting input:", err)
os.Exit(1)
}
}
}
path, err := cmd.Flags().GetString("input")
if err != nil {
panic(err)
}
filterType, err := cmd.Flags().GetString("type")
if err != nil {
panic(err)
}
lipgloss.SetColorProfile(termenv.TrueColor)
model, err := tea.NewProgram(
filter.New(path, filterType, strings.TrimSpace(b.String())),
tea.WithOutput(os.Stderr),
).Run()
if err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }
result := model.(*filter.FilterModel)
if result.FilteredJson != "" {
fmt.Fprintf(os.Stdout, result.FilteredJson)
}
} }

View file

@ -3,6 +3,8 @@ package filter
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os"
"strings" "strings"
"git.andreafazzi.eu/andrea/probo/pkg/store/file" "git.andreafazzi.eu/andrea/probo/pkg/store/file"
@ -10,6 +12,7 @@ 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"
@ -28,7 +31,7 @@ type storeLoadedMsg struct {
} }
type resultMsg struct { type resultMsg struct {
result string result []any
} }
type errorMsg struct { type errorMsg struct {
@ -39,6 +42,7 @@ 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
@ -52,17 +56,22 @@ type FilterModel struct {
// file store // file store
store []any store []any
result []any
// jq // json
lastQuery string lastQuery string
FilteredJson string
InputJson string
state int state int
filterType string
} }
type keyBindings struct { type keyBindings struct {
group *group.Model group *group.Model
quit key.Binding quit, enter key.Binding
} }
func (k *keyBindings) ShortHelp() []key.Binding { func (k *keyBindings) ShortHelp() []key.Binding {
@ -101,26 +110,48 @@ func newBindings(g *group.Model) *keyBindings {
quit: key.NewBinding( quit: key.NewBinding(
key.WithKeys("esc"), key.WithHelp("esc", "Quit app"), 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() *FilterModel { func New(path string, filterType string, stdin string) *FilterModel {
textInput := textinput.New( textInput := textinput.New(
textinput.WithPlaceholder("Write your jq filter here..."), textinput.WithPlaceholder("Write your jq filter here..."),
) )
table := table.New() 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, table), group.WithItems(textInput, viewport, tabl),
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, table)), layout.WithItem(tiled.New(viewport, tabl)),
), ),
), ),
) )
@ -139,7 +170,7 @@ func New() *FilterModel {
lipgloss.NewStyle(). lipgloss.NewStyle().
Bold(true). Bold(true).
Border(lipgloss.NormalBorder(), false, false, true, false). Border(lipgloss.NormalBorder(), false, false, true, false).
Render("Participants filter 👫"), Render(filterTypeFormats[filterType]),
), ),
) )
@ -150,15 +181,26 @@ func New() *FilterModel {
layout.WithItem(statusBar), layout.WithItem(statusBar),
) )
if path != "" {
jq, err := os.ReadFile(path)
if err != nil {
panic(err)
}
textInput.SetValue(strings.TrimSpace(string(jq)))
}
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,
document: document, document: document,
bindings: bindings, bindings: bindings,
help: help, help: help,
filterType: filterType,
InputJson: stdin,
} }
} }
@ -184,14 +226,25 @@ func (m *FilterModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, m.bindings.quit): case key.Matches(msg, m.bindings.quit):
m.FilteredJson = ""
return m, tea.Quit 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: case storeLoadedMsg:
m.handleStoreLoaded(msg) cmds = append(cmds, m.handleStoreLoaded(msg))
case resultMsg: case resultMsg:
m.handleFiltered(msg) cmds = append(cmds, m.handleFiltered(msg))
m.state = FilterState
case errorMsg:
m.handleError(msg)
m.state = ErrorState
} }
cmds = m.handleState(msg, cmds) cmds = m.handleState(msg, cmds)
@ -203,21 +256,76 @@ func (m *FilterModel) View() string {
return m.document.View() return m.document.View()
} }
func (m *FilterModel) handleStoreLoaded(msg tea.Msg) { 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) storeMsg := msg.(storeLoadedMsg)
m.store = storeMsg.store m.store = storeMsg.store
m.state = FilterState m.state = FilterState
jsonStore, err := m.storeToJson() coloredJson, err := toColoredJson(m.store)
if err != nil { if err != nil {
panic(err) return errorMsg{err}
} }
m.viewport.SetContent(jsonStore) 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) { func (m *FilterModel) handleFiltered(msg tea.Msg) tea.Cmd {
m.viewport.SetContent(sanitize(msg.(resultMsg).result)) 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 { func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
@ -229,15 +337,59 @@ func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
cmds = append(cmds, cmd, m.query(m.textInput.Value())) cmds = append(cmds, cmd, m.query(m.textInput.Value()))
m.statusBar.SetContent(formats[FilterState]...) if m.state != ErrorState {
m.statusBar.SetContent(stateFormats[FilterState]...)
}
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)
m.statusBar.SetContent(fmt.Sprintf(formats[m.state][0], m.spinner.View()), formats[m.state][1], formats[m.state][2]) m.statusBar.SetContent(fmt.Sprintf(stateFormats[m.state][0], m.spinner.View()), stateFormats[m.state][1], stateFormats[m.state][2])
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
@ -246,18 +398,36 @@ func (m *FilterModel) updateSpinner(msg tea.Msg, cmd tea.Cmd, cmds []tea.Cmd) []
func (m *FilterModel) loadStore() tea.Cmd { func (m *FilterModel) loadStore() tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
var jsonStore []byte
if m.filterType == "participants" {
pStore, err := file.NewDefaultParticipantFileStore() pStore, err := file.NewDefaultParticipantFileStore()
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
} }
jsonStore, err := pStore.Storer.Json() jsonStore, err = pStore.Storer.Json()
if err != nil {
return errorMsg{err}
}
} else if m.filterType == "quizzes" {
qStore, err := file.NewDefaultQuizFileStore()
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
} }
jsonStore, err = qStore.Storer.Json()
if err != nil {
return errorMsg{err}
}
} else {
panic("Unknown filter type!")
}
v := make([]any, 0) v := make([]any, 0)
err = json.Unmarshal(jsonStore, &v)
err := json.Unmarshal(jsonStore, &v)
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
} }
@ -280,7 +450,7 @@ func (m *FilterModel) query(input string) tea.Cmd {
query, err := gojq.Parse(input) query, err := gojq.Parse(input)
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{fmt.Errorf("jq query parse error: %v", err)}
} }
var result []string var result []string
@ -291,13 +461,12 @@ func (m *FilterModel) query(input string) tea.Cmd {
if !ok { if !ok {
break break
} }
if err, ok := v.(error); ok { if err, ok := v.(error); ok {
return errorMsg{err} return errorMsg{fmt.Errorf("jq query run error: %v", err)}
} }
f := colorjson.NewFormatter() b, err := json.MarshalIndent(v, "", " ")
f.Indent = 2
b, err := f.Marshal(v)
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
} }
@ -305,20 +474,34 @@ func (m *FilterModel) query(input string) tea.Cmd {
result = append(result, string(b)) result = append(result, string(b))
} }
return resultMsg{strings.Join(result, "\n")} v := make([]any, 0)
err = json.Unmarshal([]byte(strings.Join(result, "\n")), &v)
if err != nil {
return errorMsg{err}
}
return resultMsg{v}
} }
} }
func (m *FilterModel) storeToJson() (string, error) { func toColoredJson(data []any) (string, error) {
f := colorjson.NewFormatter() f := colorjson.NewFormatter()
f.Indent = 2 f.Indent = 2
result, err := f.Marshal(m.store) result, err := f.Marshal(data)
if err != nil { if err != nil {
return "", err return "", err
} }
return sanitize(string(result)), nil 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 { func sanitize(text string) string {
// FIXME: The use of a standard '-' character causes rendering // FIXME: The use of a standard '-' character causes rendering
// issues within the viewport. Further investigation is // issues within the viewport. Further investigation is

View file

@ -1,8 +1,13 @@
package filter package filter
var ( var (
formats = map[int][]string{ stateFormats = map[int][]string{
FilterState: []string{"FILTER 📖", "Write your jq command in the input box to start filtering. Press enter to return the result.", "STORE 🟢"}, FilterState: []string{"FILTER 📖", "Write your jq command in the input box to start filtering. Press enter to return the result.", "STORE 🟢"},
LoadingStoreState: []string{"LOAD %s", "Loading the store...", "STORE 🔴"}, LoadingStoreState: []string{"LOAD %s", "Loading the store...", "STORE 🔴"},
ErrorState: []string{"ERROR 📖", "%v", "STORE 🟢"},
}
filterTypeFormats = map[string]string{
"participants": "👫👫 Participants filter 👫👫",
"quizzes": "❓❓ Quizzes filter ❓❓",
} }
) )

View file

@ -3,4 +3,5 @@ package filter
const ( const (
LoadingStoreState = iota LoadingStoreState = iota
FilterState FilterState
ErrorState
) )