Replace colorjson with chroma

This commit is contained in:
andrea 2024-04-12 09:52:11 +02:00
parent e161d936aa
commit e5f6d3ffaf
2 changed files with 81 additions and 30 deletions

View file

@ -84,7 +84,6 @@ func createFilter(cmd *cobra.Command, args []string) {
} }
lipgloss.SetColorProfile(termenv.TrueColor) lipgloss.SetColorProfile(termenv.TrueColor)
model, err := tea.NewProgram( model, err := tea.NewProgram(
filter.New(path, filterType, strings.TrimSpace(b.String())), filter.New(path, filterType, strings.TrimSpace(b.String())),
tea.WithOutput(os.Stderr), tea.WithOutput(os.Stderr),
@ -95,7 +94,7 @@ func createFilter(cmd *cobra.Command, args []string) {
} }
result := model.(*filter.FilterModel) result := model.(*filter.FilterModel)
if result.FilteredJson != "" { if result.Result != "" {
fmt.Fprintf(os.Stdout, result.FilteredJson) fmt.Fprintf(os.Stdout, result.Result)
} }
} }

View file

@ -1,13 +1,14 @@
package filter package filter
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"git.andreafazzi.eu/andrea/probo/pkg/store/file" "git.andreafazzi.eu/andrea/probo/pkg/store/file"
"github.com/TylerBrock/colorjson" "github.com/alecthomas/chroma/quick"
"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"
@ -57,6 +58,10 @@ type FilterModel struct {
lastQuery string lastQuery string
FilteredJson string FilteredJson string
InputJson string InputJson string
Result string
// filter file
filterFilePath string
state int state int
@ -156,25 +161,18 @@ func New(path string, filterType string, stdin string) *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,
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, filterType: filterType,
InputJson: stdin, filterFilePath: path,
InputJson: stdin,
} }
} }
@ -204,11 +202,10 @@ func (m *FilterModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
case key.Matches(msg, m.bindings.enter): case key.Matches(msg, m.bindings.enter):
m.FilteredJson = strings.Join([]string{ m.marshalJSON()
fmt.Sprintf("{\"%s\": %s,", m.filterType, m.FilteredJson),
strings.TrimLeft(m.InputJson, "{")}, "\n")
return m, tea.Quit return m, tea.Quit
} }
case storeLoadedMsg: case storeLoadedMsg:
cmds = append(cmds, m.handleStoreLoaded(msg)) cmds = append(cmds, m.handleStoreLoaded(msg))
@ -230,6 +227,45 @@ func (m *FilterModel) View() string {
return m.document.View() return m.document.View()
} }
func (m *FilterModel) marshalJSON() {
if m.FilteredJson == "" {
return
}
if m.InputJson != "" {
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)
} 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)
}
}
func (m *FilterModel) showErrorOnStatusBar(err error) { func (m *FilterModel) showErrorOnStatusBar(err error) {
m.statusBar.SetContent( m.statusBar.SetContent(
stateFormats[ErrorState][0], stateFormats[ErrorState][0],
@ -254,6 +290,15 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
m.store = storeMsg.store m.store = storeMsg.store
m.state = FilterState m.state = FilterState
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)))
}
coloredJson, err := toColoredJson(m.store) coloredJson, err := toColoredJson(m.store)
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
@ -261,7 +306,7 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
m.viewport.SetContent(coloredJson) m.viewport.SetContent(coloredJson)
return nil return m.query(".")
} }
} }
@ -403,13 +448,20 @@ func (m *FilterModel) query(input string) tea.Cmd {
} }
func toColoredJson(data []any) (string, error) { func toColoredJson(data []any) (string, error) {
f := colorjson.NewFormatter() result, err := json.MarshalIndent(data, "", " ")
f.Indent = 2
result, err := f.Marshal(data)
if err != nil { if err != nil {
return "", err return "", err
} }
return sanitize(string(result)), nil
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
} }
func toJson(data []any) (string, error) { func toJson(data []any) (string, error) {