diff --git a/cmd/filter.go b/cmd/filter.go index 71578f5..fd99380 100644 --- a/cmd/filter.go +++ b/cmd/filter.go @@ -84,7 +84,6 @@ func createFilter(cmd *cobra.Command, args []string) { } lipgloss.SetColorProfile(termenv.TrueColor) - model, err := tea.NewProgram( filter.New(path, filterType, strings.TrimSpace(b.String())), tea.WithOutput(os.Stderr), @@ -95,7 +94,7 @@ func createFilter(cmd *cobra.Command, args []string) { } result := model.(*filter.FilterModel) - if result.FilteredJson != "" { - fmt.Fprintf(os.Stdout, result.FilteredJson) + if result.Result != "" { + fmt.Fprintf(os.Stdout, result.Result) } } diff --git a/cmd/filter/filter.go b/cmd/filter/filter.go index af6b8d4..bcd318f 100644 --- a/cmd/filter/filter.go +++ b/cmd/filter/filter.go @@ -1,13 +1,14 @@ package filter import ( + "bytes" "encoding/json" "fmt" "os" "strings" "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/key" "github.com/charmbracelet/bubbles/spinner" @@ -57,6 +58,10 @@ type FilterModel struct { lastQuery string FilteredJson string InputJson string + Result string + + // filter file + filterFilePath string state int @@ -156,25 +161,18 @@ func New(path string, filterType string, stdin string) *FilterModel { 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, - group: group, - statusBar: statusBar, - spinner: s, - document: document, - bindings: bindings, - help: help, - filterType: filterType, - InputJson: stdin, + textInput: textInput, + viewport: viewport, + group: group, + statusBar: statusBar, + spinner: s, + document: document, + bindings: bindings, + help: help, + filterType: filterType, + filterFilePath: path, + InputJson: stdin, } } @@ -204,11 +202,10 @@ func (m *FilterModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 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") + m.marshalJSON() return m, tea.Quit } + case storeLoadedMsg: cmds = append(cmds, m.handleStoreLoaded(msg)) @@ -230,6 +227,45 @@ func (m *FilterModel) View() string { 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) { m.statusBar.SetContent( stateFormats[ErrorState][0], @@ -254,6 +290,15 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd { m.store = storeMsg.store 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) if err != nil { return errorMsg{err} @@ -261,7 +306,7 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd { 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) { - f := colorjson.NewFormatter() - f.Indent = 2 - result, err := f.Marshal(data) + result, err := json.MarshalIndent(data, "", " ") if err != nil { 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) {