Refactoring

This commit is contained in:
andrea 2024-04-24 09:29:01 +02:00
parent be95c23d5f
commit 7eaeb36578
9 changed files with 216 additions and 169 deletions

View file

@ -9,38 +9,27 @@ import (
"git.andreafazzi.eu/andrea/probo/pkg/store/file" "git.andreafazzi.eu/andrea/probo/pkg/store/file"
"github.com/alecthomas/chroma/quick" "github.com/alecthomas/chroma/quick"
"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"
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"
foam "github.com/remogatto/sugarfoam"
"github.com/remogatto/sugarfoam/components/group" "github.com/remogatto/sugarfoam/components/group"
"github.com/remogatto/sugarfoam/components/header" "github.com/remogatto/sugarfoam/components/header"
"github.com/remogatto/sugarfoam/components/help"
"github.com/remogatto/sugarfoam/components/statusbar" "github.com/remogatto/sugarfoam/components/statusbar"
"github.com/remogatto/sugarfoam/components/textinput" "github.com/remogatto/sugarfoam/components/textinput"
"github.com/remogatto/sugarfoam/components/viewport" "github.com/remogatto/sugarfoam/components/viewport"
"github.com/remogatto/sugarfoam/layout" "github.com/remogatto/sugarfoam/layout"
) )
type storeLoadedMsg struct {
store []any
}
type resultMsg struct {
result []any
}
type errorMsg struct {
error error
}
type FilterModel struct { type FilterModel struct {
// UI // UI
textInput *textinput.Model textInput *textinput.Model
viewport *viewport.Model viewport *viewport.Model
group *group.Model group *group.Model
help help.Model help *help.Model
statusBar *statusbar.Model statusBar *statusbar.Model
spinner spinner.Model spinner spinner.Model
@ -68,54 +57,6 @@ type FilterModel struct {
filterType string filterType string
} }
type keyBindings struct {
group *group.Model
quit, enter key.Binding
}
func (k *keyBindings) ShortHelp() []key.Binding {
keys := make([]key.Binding, 0)
current := k.group.Current()
switch item := current.(type) {
case *viewport.Model:
keys = append(
keys,
item.KeyMap.Up,
item.KeyMap.Down,
)
}
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"),
),
enter: key.NewBinding(
key.WithKeys("enter"), key.WithHelp("enter", "Quit app and return the results"),
),
}
}
func New(path string, filterType string, stdin string) *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..."),
@ -123,8 +64,6 @@ func New(path string, filterType string, stdin string) *FilterModel {
viewport := viewport.New() viewport := viewport.New()
help := help.New()
group := group.New( group := group.New(
group.WithItems(textInput, viewport), group.WithItems(textInput, viewport),
group.WithLayout( group.WithLayout(
@ -154,10 +93,15 @@ func New(path string, filterType string, stdin string) *FilterModel {
), ),
) )
help := help.New(
bindings,
help.WithStyles(&foam.Styles{NoBorder: lipgloss.NewStyle().Padding(1, 1)}))
document := layout.New( document := layout.New(
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}), layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}),
layout.WithItem(header), layout.WithItem(header),
layout.WithItem(group), layout.WithItem(group),
layout.WithItem(help),
layout.WithItem(statusBar), layout.WithItem(statusBar),
) )

55
cmd/filter/keymap.go Normal file
View file

@ -0,0 +1,55 @@
package filter
import (
"github.com/charmbracelet/bubbles/key"
"github.com/remogatto/sugarfoam/components/group"
"github.com/remogatto/sugarfoam/components/viewport"
)
type keyBindings struct {
group *group.Model
quit, enter key.Binding
}
func (k *keyBindings) ShortHelp() []key.Binding {
keys := make([]key.Binding, 0)
current := k.group.Current()
switch item := current.(type) {
case *viewport.Model:
keys = append(
keys,
item.KeyMap.Up,
item.KeyMap.Down,
)
}
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"),
),
enter: key.NewBinding(
key.WithKeys("enter"), key.WithHelp("enter", "Quit app and return the results"),
),
}
}

13
cmd/filter/message.go Normal file
View file

@ -0,0 +1,13 @@
package filter
type storeLoadedMsg struct {
store []any
}
type resultMsg struct {
result []any
}
type errorMsg struct {
error error
}

View file

@ -42,7 +42,7 @@ func updateSession(cmd *cobra.Command, args []string) {
lipgloss.SetColorProfile(termenv.TrueColor) lipgloss.SetColorProfile(termenv.TrueColor)
model, err := tea.NewProgram( _, err = tea.NewProgram(
session.New(path, util.ReadStdin()), session.New(path, util.ReadStdin()),
tea.WithOutput(os.Stderr), tea.WithOutput(os.Stderr),
).Run() ).Run()
@ -50,10 +50,5 @@ func updateSession(cmd *cobra.Command, args []string) {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)
} }
result := model.(*session.SessionModel)
if result.Result != "" {
fmt.Fprintf(os.Stdout, result.Result)
}
fmt.Println("session called", path, model)
} }

View file

@ -2,7 +2,7 @@ package session
var ( var (
stateFormats = map[int][]string{ stateFormats = map[int][]string{
BrowseState: []string{"BROWSE 📖", "Use the navigation keys to browse the content. Press enter to return the result.", "STORE 🟢"}, BrowseState: []string{"BROWSE 📖", "Total sessions in the store: %d, Exams in the current session: %d", "STORE 🟢"},
LoadingStoreState: []string{"LOAD %s", "Loading the store...", "STORE 🔴"}, LoadingStoreState: []string{"LOAD %s", "Loading the store...", "STORE 🔴"},
ErrorState: []string{"ERROR 📖", "%v", "STORE 🟢"}, ErrorState: []string{"ERROR 📖", "%v", "STORE 🟢"},
} }

69
cmd/session/keymap.go Normal file
View file

@ -0,0 +1,69 @@
package session
import (
"github.com/charmbracelet/bubbles/key"
"github.com/remogatto/sugarfoam/components/form"
"github.com/remogatto/sugarfoam/components/group"
"github.com/remogatto/sugarfoam/components/table"
"github.com/remogatto/sugarfoam/components/viewport"
)
type keyBindings struct {
group *group.Model
quit, enter key.Binding
}
func (k *keyBindings) ShortHelp() []key.Binding {
keys := make([]key.Binding, 0)
current := k.group.Current()
switch item := current.(type) {
case *table.Model:
keys = append(
keys,
item.KeyMap.LineUp,
item.KeyMap.LineDown,
)
case *viewport.Model:
keys = append(
keys,
item.KeyMap.Up,
item.KeyMap.Down,
)
case *form.Model:
keys = append(
keys,
item.KeyBinds()...,
)
}
keys = append(
keys,
k.group.KeyMap.FocusNext,
k.group.KeyMap.FocusPrev,
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"),
),
}
}

View file

@ -3,6 +3,7 @@ package session
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -10,7 +11,7 @@ import (
"git.andreafazzi.eu/andrea/probo/pkg/models" "git.andreafazzi.eu/andrea/probo/pkg/models"
"git.andreafazzi.eu/andrea/probo/pkg/store/file" "git.andreafazzi.eu/andrea/probo/pkg/store/file"
"github.com/alecthomas/chroma/quick" "github.com/alecthomas/chroma/quick"
"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"
btTable "github.com/charmbracelet/bubbles/table" btTable "github.com/charmbracelet/bubbles/table"
@ -20,9 +21,11 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/d5/tengo/v2" "github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib" "github.com/d5/tengo/v2/stdlib"
foam "github.com/remogatto/sugarfoam"
"github.com/remogatto/sugarfoam/components/form" "github.com/remogatto/sugarfoam/components/form"
"github.com/remogatto/sugarfoam/components/group" "github.com/remogatto/sugarfoam/components/group"
"github.com/remogatto/sugarfoam/components/header" "github.com/remogatto/sugarfoam/components/header"
"github.com/remogatto/sugarfoam/components/help"
"github.com/remogatto/sugarfoam/components/statusbar" "github.com/remogatto/sugarfoam/components/statusbar"
"github.com/remogatto/sugarfoam/components/table" "github.com/remogatto/sugarfoam/components/table"
"github.com/remogatto/sugarfoam/components/viewport" "github.com/remogatto/sugarfoam/components/viewport"
@ -36,7 +39,7 @@ type SessionModel struct {
viewport *viewport.Model viewport *viewport.Model
table *table.Model table *table.Model
group *group.Model group *group.Model
help help.Model help *help.Model
statusBar *statusbar.Model statusBar *statusbar.Model
spinner spinner.Model spinner spinner.Model
@ -46,14 +49,14 @@ type SessionModel struct {
// Key bindings // Key bindings
bindings *keyBindings bindings *keyBindings
// file store // store
store *file.SessionFileStore store *file.SessionFileStore
lenStore int
result []any result []any
// json // json
FilteredJson string InputJson string
InputJson string
Result string
// session // session
session *models.Session session *models.Session
@ -67,65 +70,29 @@ type SessionModel struct {
state int state int
} }
type keyBindings struct {
group *group.Model
quit, enter key.Binding
}
func (k *keyBindings) ShortHelp() []key.Binding {
keys := make([]key.Binding, 0)
current := k.group.Current()
switch item := current.(type) {
case *viewport.Model:
keys = append(
keys,
item.KeyMap.Up,
item.KeyMap.Down,
)
}
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"),
),
enter: key.NewBinding(
key.WithKeys("enter"), key.WithHelp("enter", "Quit app and return the results"),
),
}
}
func New(path string, stdin string) *SessionModel { func New(path string, stdin string) *SessionModel {
form := form.New( form := form.New(
form.WithGroups(huh.NewGroup( form.WithGroups(huh.NewGroup(
huh.NewInput(). huh.NewInput().
Title("Session name"). Key("sessionTitle").
Description("Enter the name of the session"), Title("Session title").
Description("Enter the title of the session").
Validate(func(str string) error {
if str == "" {
return errors.New("You must set a session name!")
}
return nil
}),
))) )))
form. formBinding := huh.NewDefaultKeyMap()
WithShowHelp(false). formBinding.Input.Next = key.NewBinding(key.WithKeys("down"), key.WithHelp("down", "next"))
WithTheme(huh.ThemeDracula()) formBinding.Input.Prev = key.NewBinding(key.WithKeys("up"), key.WithHelp("up", "prev"))
formBinding.Confirm.Next = key.NewBinding(key.WithKeys("down"), key.WithHelp("down", "next"))
formBinding.Confirm.Prev = key.NewBinding(key.WithKeys("up"), key.WithHelp("up", "prev"))
form.WithShowHelp(false).WithTheme(huh.ThemeDracula()).WithKeyMap(formBinding)
viewport := viewport.New() viewport := viewport.New()
@ -137,13 +104,11 @@ func New(path string, stdin string) *SessionModel {
{Title: "Class", Width: 20}, {Title: "Class", Width: 20},
}) })
help := help.New()
group := group.New( group := group.New(
group.WithItems(form, table, viewport), group.WithItems(form, table, viewport),
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, 1)}),
layout.WithItem(form), layout.WithItem(form),
layout.WithItem(tiled.New(table, viewport)), layout.WithItem(tiled.New(table, viewport)),
), ),
@ -168,10 +133,15 @@ func New(path string, stdin string) *SessionModel {
), ),
) )
help := help.New(
bindings,
help.WithStyles(&foam.Styles{NoBorder: lipgloss.NewStyle().Padding(1, 1)}))
document := layout.New( document := layout.New(
layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}), layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Margin(1)}),
layout.WithItem(header), layout.WithItem(header),
layout.WithItem(group), layout.WithItem(group),
layout.WithItem(help),
layout.WithItem(statusBar), layout.WithItem(statusBar),
) )
@ -221,12 +191,7 @@ func (m *SessionModel) 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 = "" cmds = append(cmds, tea.Quit)
return m, tea.Quit
case key.Matches(msg, m.bindings.enter):
m.marshalJSON()
return m, tea.Quit
} }
case storeLoadedMsg: case storeLoadedMsg:
@ -271,7 +236,6 @@ func (m *SessionModel) executeScript(path string) tea.Cmd {
_ = s.Add("input", m.InputJson) _ = s.Add("input", m.InputJson)
_ = s.Add("output", string(sessionJson)) _ = s.Add("output", string(sessionJson))
// compile the source
c, err := s.Compile() c, err := s.Compile()
if err != nil { if err != nil {
return errorMsg{err} return errorMsg{err}
@ -286,20 +250,15 @@ func (m *SessionModel) executeScript(path string) tea.Cmd {
} }
} }
func (m *SessionModel) marshalJSON() { func (m *SessionModel) createSession() error {
var result interface{} m.session.Title = m.form.GetString("sessionTitle")
err := json.Unmarshal([]byte(m.FilteredJson), &result) _, err := m.store.Create(m.session)
if err != nil { if err != nil {
panic(err) return err
} }
resultJson, err := json.Marshal(result) return nil
if err != nil {
panic(err)
}
m.Result = string(resultJson)
} }
func (m *SessionModel) showErrorOnStatusBar(err error) { func (m *SessionModel) showErrorOnStatusBar(err error) {
@ -322,14 +281,16 @@ func (m *SessionModel) updateTableContent(session *models.Session) {
}) })
} }
m.table.SetRows(rows) m.table.SetRows(rows)
} }
func (m *SessionModel) updateViewportContent(session *models.Session) { func (m *SessionModel) updateViewportContent(session *models.Session) {
currentToken := m.table.SelectedRow()[0] currentToken := m.table.SelectedRow()[0]
currentExam := session.Exams[currentToken] currentExam := session.Exams[currentToken]
if currentExam == nil { if currentExam == nil {
panic("Current token is not associate to any exam") panic("Current token is not associate to any exam!")
} }
md, err := currentExam.ToMarkdown() md, err := currentExam.ToMarkdown()
@ -392,7 +353,9 @@ func (m *SessionModel) handleScriptExecuted(msg tea.Msg) {
func (m *SessionModel) handleStoreLoaded(msg tea.Msg) tea.Cmd { func (m *SessionModel) handleStoreLoaded(msg tea.Msg) tea.Cmd {
storeMsg := msg.(storeLoadedMsg) storeMsg := msg.(storeLoadedMsg)
m.store = storeMsg.store m.store = storeMsg.store
m.lenStore = len(m.store.ReadAll())
return m.executeScript(m.scriptFilePath) return m.executeScript(m.scriptFilePath)
@ -405,16 +368,37 @@ func (m *SessionModel) handleState(msg tea.Msg, cmds []tea.Cmd) []tea.Cmd {
return m.updateSpinner(msg, cmd, cmds) return m.updateSpinner(msg, cmd, cmds)
} }
if m.form.State == huh.StateCompleted {
err := m.createSession()
if err != nil {
panic(err)
}
cmds = append(cmds, tea.Quit)
}
if len(m.form.Errors()) > 0 {
m.state = ErrorState
for _, err := range m.form.Errors() {
m.showErrorOnStatusBar(err)
}
} else {
m.state = BrowseState
}
if m.state == BrowseState { if m.state == BrowseState {
m.updateViewportContent(m.session) m.updateViewportContent(m.session)
} }
cmds = append(cmds, cmd /*, m.query(m.textInput.Value())*/)
if m.state != ErrorState { if m.state != ErrorState {
m.statusBar.SetContent(stateFormats[BrowseState]...) m.statusBar.SetContent(
stateFormats[BrowseState][0],
fmt.Sprintf(stateFormats[BrowseState][1], m.lenStore, len(m.session.Exams)),
stateFormats[BrowseState][2],
)
} }
cmds = append(cmds, cmd)
return cmds return cmds
} }
@ -456,13 +440,6 @@ func toColoredJson(data []any) (string, error) {
return sanitize(buffer.String()), nil return sanitize(buffer.String()), 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

@ -9,7 +9,7 @@ import (
type Session struct { type Session struct {
Meta Meta
Title string `json:"name"` Title string `json:"title"`
Exams map[string]*Exam `json:"exams"` Exams map[string]*Exam `json:"exams"`
} }

View file

@ -30,12 +30,6 @@ type Storer[T Storable] interface {
Json() ([]byte, error) Json() ([]byte, error)
} }
// type FilterStorer[T Storable] interface {
// Storer[T]
// Filter([]T, func(T) bool) []T
// }
type Store[T Storable] struct { type Store[T Storable] struct {
ids map[string]T ids map[string]T
hashes map[string]T hashes map[string]T