2024-03-25 15:53:20 +01:00
|
|
|
|
package filter
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-12 09:52:11 +02:00
|
|
|
|
"bytes"
|
2024-03-27 11:45:33 +01:00
|
|
|
|
"encoding/json"
|
2024-03-25 15:53:20 +01:00
|
|
|
|
"fmt"
|
2024-04-10 08:44:49 +02:00
|
|
|
|
"os"
|
2024-03-27 11:45:33 +01:00
|
|
|
|
"strings"
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/probo/pkg/store/file"
|
2024-04-12 09:52:11 +02:00
|
|
|
|
"github.com/alecthomas/chroma/quick"
|
2024-03-25 15:53:20 +01:00
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
2024-03-27 11:45:33 +01:00
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
2024-03-25 15:53:20 +01:00
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-03-27 11:45:33 +01:00
|
|
|
|
"github.com/itchyny/gojq"
|
2024-03-25 15:53:20 +01:00
|
|
|
|
"github.com/remogatto/sugarfoam/components/group"
|
|
|
|
|
"github.com/remogatto/sugarfoam/components/header"
|
|
|
|
|
"github.com/remogatto/sugarfoam/components/statusbar"
|
|
|
|
|
"github.com/remogatto/sugarfoam/components/textinput"
|
|
|
|
|
"github.com/remogatto/sugarfoam/components/viewport"
|
|
|
|
|
"github.com/remogatto/sugarfoam/layout"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
type storeLoadedMsg struct {
|
|
|
|
|
store []any
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
type resultMsg struct {
|
2024-04-10 08:44:49 +02:00
|
|
|
|
result []any
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type errorMsg struct {
|
|
|
|
|
error error
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FilterModel struct {
|
|
|
|
|
// UI
|
2024-03-27 11:45:33 +01:00
|
|
|
|
textInput *textinput.Model
|
|
|
|
|
viewport *viewport.Model
|
2024-03-25 15:53:20 +01:00
|
|
|
|
group *group.Model
|
|
|
|
|
help help.Model
|
|
|
|
|
statusBar *statusbar.Model
|
2024-03-27 11:45:33 +01:00
|
|
|
|
spinner spinner.Model
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
|
|
|
|
// Layout
|
|
|
|
|
document *layout.Layout
|
|
|
|
|
|
|
|
|
|
// Key bindings
|
|
|
|
|
bindings *keyBindings
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
// file store
|
2024-04-10 08:44:49 +02:00
|
|
|
|
store []any
|
|
|
|
|
result []any
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
// json
|
|
|
|
|
lastQuery string
|
|
|
|
|
FilteredJson string
|
|
|
|
|
InputJson string
|
2024-04-12 09:52:11 +02:00
|
|
|
|
Result string
|
|
|
|
|
|
|
|
|
|
// filter file
|
|
|
|
|
filterFilePath string
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
|
|
|
|
state int
|
2024-04-10 08:44:49 +02:00
|
|
|
|
|
|
|
|
|
filterType string
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type keyBindings struct {
|
|
|
|
|
group *group.Model
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
quit, enter key.Binding
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (k *keyBindings) ShortHelp() []key.Binding {
|
|
|
|
|
keys := make([]key.Binding, 0)
|
|
|
|
|
|
|
|
|
|
current := k.group.Current()
|
|
|
|
|
|
|
|
|
|
switch item := current.(type) {
|
2024-04-10 08:51:51 +02:00
|
|
|
|
case *viewport.Model:
|
2024-03-25 15:53:20 +01:00
|
|
|
|
keys = append(
|
|
|
|
|
keys,
|
2024-04-10 08:51:51 +02:00
|
|
|
|
item.KeyMap.Up,
|
|
|
|
|
item.KeyMap.Down,
|
2024-03-25 15:53:20 +01:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"),
|
|
|
|
|
),
|
2024-04-10 08:44:49 +02:00
|
|
|
|
enter: key.NewBinding(
|
|
|
|
|
key.WithKeys("enter"), key.WithHelp("enter", "Quit app and return the results"),
|
|
|
|
|
),
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func New(path string, filterType string, stdin string) *FilterModel {
|
2024-03-27 11:45:33 +01:00
|
|
|
|
textInput := textinput.New(
|
2024-03-25 15:53:20 +01:00
|
|
|
|
textinput.WithPlaceholder("Write your jq filter here..."),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
viewport := viewport.New()
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
2024-03-25 15:53:20 +01:00
|
|
|
|
help := help.New()
|
|
|
|
|
|
|
|
|
|
group := group.New(
|
2024-04-10 08:51:51 +02:00
|
|
|
|
group.WithItems(textInput, viewport),
|
2024-03-25 15:53:20 +01:00
|
|
|
|
group.WithLayout(
|
|
|
|
|
layout.New(
|
|
|
|
|
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 0, 1, 0)}),
|
2024-03-27 11:45:33 +01:00
|
|
|
|
layout.WithItem(textInput),
|
2024-04-10 08:51:51 +02:00
|
|
|
|
layout.WithItem(viewport),
|
2024-03-25 15:53:20 +01:00
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bindings := newBindings(group)
|
|
|
|
|
statusBar := statusbar.New(bindings)
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
s := spinner.New(
|
|
|
|
|
spinner.WithStyle(
|
|
|
|
|
lipgloss.NewStyle().Foreground(lipgloss.Color("265"))),
|
|
|
|
|
)
|
|
|
|
|
s.Spinner = spinner.Dot
|
|
|
|
|
|
2024-03-25 15:53:20 +01:00
|
|
|
|
header := header.New(
|
|
|
|
|
header.WithContent(
|
|
|
|
|
lipgloss.NewStyle().
|
|
|
|
|
Bold(true).
|
|
|
|
|
Border(lipgloss.NormalBorder(), false, false, true, false).
|
2024-04-10 08:44:49 +02:00
|
|
|
|
Render(filterTypeFormats[filterType]),
|
2024-03-25 15:53:20 +01:00
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
document := layout.New(
|
|
|
|
|
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}),
|
|
|
|
|
layout.WithItem(header),
|
|
|
|
|
layout.WithItem(group),
|
|
|
|
|
layout.WithItem(statusBar),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return &FilterModel{
|
2024-04-12 09:52:11 +02:00
|
|
|
|
textInput: textInput,
|
|
|
|
|
viewport: viewport,
|
|
|
|
|
group: group,
|
|
|
|
|
statusBar: statusBar,
|
|
|
|
|
spinner: s,
|
|
|
|
|
document: document,
|
|
|
|
|
bindings: bindings,
|
|
|
|
|
help: help,
|
|
|
|
|
filterType: filterType,
|
|
|
|
|
filterFilePath: path,
|
|
|
|
|
InputJson: stdin,
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *FilterModel) Init() tea.Cmd {
|
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
cmds = append(cmds, m.group.Init(), m.loadStore(), m.spinner.Tick)
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
|
|
|
|
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):
|
2024-04-10 08:44:49 +02:00
|
|
|
|
m.FilteredJson = ""
|
2024-03-25 15:53:20 +01:00
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
case key.Matches(msg, m.bindings.enter):
|
2024-04-12 09:52:11 +02:00
|
|
|
|
m.marshalJSON()
|
2024-04-10 08:44:49 +02:00
|
|
|
|
return m, tea.Quit
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
2024-04-12 09:52:11 +02:00
|
|
|
|
|
2024-03-25 15:53:20 +01:00
|
|
|
|
case storeLoadedMsg:
|
2024-04-10 08:44:49 +02:00
|
|
|
|
cmds = append(cmds, m.handleStoreLoaded(msg))
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
|
|
|
|
case resultMsg:
|
2024-04-10 08:44:49 +02:00
|
|
|
|
cmds = append(cmds, m.handleFiltered(msg))
|
|
|
|
|
m.state = FilterState
|
|
|
|
|
|
|
|
|
|
case errorMsg:
|
|
|
|
|
m.handleError(msg)
|
|
|
|
|
m.state = ErrorState
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmds = m.handleState(msg, cmds)
|
|
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *FilterModel) View() string {
|
|
|
|
|
return m.document.View()
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-12 09:52:11 +02:00
|
|
|
|
func (m *FilterModel) marshalJSON() {
|
|
|
|
|
if m.FilteredJson == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if m.InputJson != "" {
|
2024-04-22 14:24:29 +02:00
|
|
|
|
// result := make([]interface{}, 2)
|
|
|
|
|
// err := json.Unmarshal([]byte(m.InputJson), &result[0])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// filtered := fmt.Sprintf("{\"%s\": %s}", m.filterType, m.FilteredJson)
|
|
|
|
|
// err = json.Unmarshal([]byte(filtered), &result[1])
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// resultJson, err := json.Marshal(result)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
// m.Result = string(resultJson)
|
|
|
|
|
m.Result = fmt.Sprintf("{%s, \"%s\": %s}", strings.Trim(m.InputJson, "{}"), m.filterType, m.FilteredJson)
|
2024-04-12 09:52:11 +02:00
|
|
|
|
} else {
|
|
|
|
|
var result interface{}
|
|
|
|
|
|
|
|
|
|
filtered := fmt.Sprintf("{\"%s\": %s}", m.filterType, m.FilteredJson)
|
|
|
|
|
err := json.Unmarshal([]byte(filtered), &result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resultJson, err := json.Marshal(result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
m.Result = string(resultJson)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func (m *FilterModel) showErrorOnStatusBar(err error) {
|
|
|
|
|
m.statusBar.SetContent(
|
|
|
|
|
stateFormats[ErrorState][0],
|
|
|
|
|
fmt.Sprintf(stateFormats[ErrorState][1], err),
|
|
|
|
|
stateFormats[ErrorState][2],
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func (m *FilterModel) handleError(msg tea.Msg) {
|
|
|
|
|
err := msg.(errorMsg)
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
m.statusBar.SetContent(
|
|
|
|
|
stateFormats[ErrorState][0],
|
|
|
|
|
fmt.Sprintf(stateFormats[ErrorState][1], err.error),
|
|
|
|
|
stateFormats[ErrorState][2],
|
|
|
|
|
)
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
|
|
|
|
|
return func() tea.Msg {
|
|
|
|
|
storeMsg := msg.(storeLoadedMsg)
|
|
|
|
|
m.store = storeMsg.store
|
|
|
|
|
m.state = FilterState
|
|
|
|
|
|
2024-04-12 09:52:11 +02:00
|
|
|
|
if m.filterFilePath != "" {
|
|
|
|
|
jq, err := os.ReadFile(m.filterFilePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
m.textInput.SetValue(strings.TrimSpace(string(jq)))
|
|
|
|
|
return m.query(strings.TrimSpace(string(jq)))
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
coloredJson, err := toColoredJson(m.store)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errorMsg{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.viewport.SetContent(coloredJson)
|
|
|
|
|
|
2024-04-12 09:52:11 +02:00
|
|
|
|
return m.query(".")
|
2024-04-10 08:44:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
|
|
|
|
|
_, cmd := m.group.Update(msg)
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
if m.state == LoadingStoreState {
|
|
|
|
|
return m.updateSpinner(msg, cmd, cmds)
|
|
|
|
|
}
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
cmds = append(cmds, cmd, m.query(m.textInput.Value()))
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
if m.state != ErrorState {
|
|
|
|
|
m.statusBar.SetContent(stateFormats[FilterState]...)
|
|
|
|
|
}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
|
|
|
|
return cmds
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *FilterModel) updateSpinner(msg tea.Msg, cmd tea.Cmd, cmds []tea.Cmd) []tea.Cmd {
|
|
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
m.statusBar.SetContent(fmt.Sprintf(stateFormats[m.state][0], m.spinner.View()), stateFormats[m.state][1], stateFormats[m.state][2])
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
|
|
|
|
cmds = append(cmds, cmd)
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
|
|
|
|
return cmds
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
func (m *FilterModel) loadStore() tea.Cmd {
|
2024-03-25 15:53:20 +01:00
|
|
|
|
return func() tea.Msg {
|
2024-04-10 08:44:49 +02:00
|
|
|
|
var jsonStore []byte
|
2024-03-25 15:53:20 +01:00
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
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!")
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v := make([]any, 0)
|
2024-04-10 08:44:49 +02:00
|
|
|
|
|
|
|
|
|
err := json.Unmarshal(jsonStore, &v)
|
2024-03-27 11:45:33 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errorMsg{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return storeLoadedMsg{v}
|
2024-03-25 15:53:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
|
|
|
|
|
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 {
|
2024-04-10 08:44:49 +02:00
|
|
|
|
return errorMsg{fmt.Errorf("jq query parse error: %v", err)}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
|
|
iter := query.Run(m.store)
|
|
|
|
|
for {
|
|
|
|
|
v, ok := iter.Next()
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2024-04-10 08:44:49 +02:00
|
|
|
|
|
2024-03-27 11:45:33 +01:00
|
|
|
|
if err, ok := v.(error); ok {
|
2024-04-10 08:44:49 +02:00
|
|
|
|
return errorMsg{fmt.Errorf("jq query run error: %v", err)}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
b, err := json.MarshalIndent(v, "", " ")
|
2024-03-27 11:45:33 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errorMsg{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = append(result, string(b))
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
v := make([]any, 0)
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(strings.Join(result, "\n")), &v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errorMsg{err}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultMsg{v}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func toColoredJson(data []any) (string, error) {
|
2024-04-12 09:52:11 +02:00
|
|
|
|
result, err := json.MarshalIndent(data, "", " ")
|
2024-03-27 11:45:33 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2024-04-12 09:52:11 +02:00
|
|
|
|
|
|
|
|
|
coloredBytes := make([]byte, 0)
|
|
|
|
|
buffer := bytes.NewBuffer(coloredBytes)
|
|
|
|
|
|
|
|
|
|
err = quick.Highlight(buffer, string(result), "json", "terminal16m", "dracula")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sanitize(buffer.String()), nil
|
2024-03-27 11:45:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 08:44:49 +02:00
|
|
|
|
func toJson(data []any) (string, error) {
|
|
|
|
|
result, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return string(result), nil
|
|
|
|
|
}
|
2024-03-27 11:45:33 +01:00
|
|
|
|
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)
|
|
|
|
|
}
|