2 次代碼提交 2331ca03b2 ... e5f6d3ffaf

作者 SHA1 備註 提交日期
  andrea e5f6d3ffaf Replace colorjson with chroma 1 月之前
  Andrea Fazzi e161d936aa Remove table from JSON filter 1 月之前
共有 2 個文件被更改,包括 85 次插入117 次删除
  1. 2 3
      cmd/filter.go
  2. 83 114
      cmd/filter/filter.go

+ 2 - 3
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)
 	}
 }

+ 83 - 114
cmd/filter/filter.go

@@ -1,29 +1,26 @@
 package filter
 
 import (
+	"bytes"
 	"encoding/json"
 	"fmt"
-	"log"
 	"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"
-	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 {
@@ -42,7 +39,6 @@ type FilterModel struct {
 	// UI
 	textInput *textinput.Model
 	viewport  *viewport.Model
-	table     *table.Model
 	group     *group.Model
 	help      help.Model
 	statusBar *statusbar.Model
@@ -62,6 +58,10 @@ type FilterModel struct {
 	lastQuery    string
 	FilteredJson string
 	InputJson    string
+	Result       string
+
+	// filter file
+	filterFilePath string
 
 	state int
 
@@ -80,11 +80,11 @@ func (k *keyBindings) ShortHelp() []key.Binding {
 	current := k.group.Current()
 
 	switch item := current.(type) {
-	case *table.Model:
+	case *viewport.Model:
 		keys = append(
 			keys,
-			item.KeyMap.LineUp,
-			item.KeyMap.LineDown,
+			item.KeyMap.Up,
+			item.KeyMap.Down,
 		)
 	}
 
@@ -121,37 +121,17 @@ func New(path string, filterType string, stdin string) *FilterModel {
 		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.WithItems(textInput, viewport),
 		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)),
+				layout.WithItem(viewport),
 			),
 		),
 	)
@@ -181,26 +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,
-		table:      tabl,
-		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,
 	}
 
 }
@@ -230,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))
 
@@ -256,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],
@@ -280,24 +290,23 @@ func (m *FilterModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
 		m.store = storeMsg.store
 		m.state = FilterState
 
-		coloredJson, err := toColoredJson(m.store)
-		if err != nil {
-			return errorMsg{err}
+		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)))
 		}
 
-		json, err := toJson(m.store)
+		coloredJson, err := toColoredJson(m.store)
 		if err != nil {
 			return errorMsg{err}
 		}
 
 		m.viewport.SetContent(coloredJson)
 
-		err = m.updateTableContent(json)
-		if err != nil {
-			return errorMsg{err}
-		}
-
-		return nil
+		return m.query(".")
 	}
 }
 
@@ -319,11 +328,6 @@ func (m *FilterModel) handleFiltered(msg tea.Msg) tea.Cmd {
 
 		m.viewport.SetContent(coloredJson)
 
-		err = m.updateTableContent(json)
-		if err != nil {
-			return errorMsg{err}
-		}
-
 		return nil
 	}
 }
@@ -344,48 +348,6 @@ func (m *FilterModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
 	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)
 
@@ -486,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) {