Skip to content

Commit

Permalink
use model
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 10, 2024
1 parent e9727ce commit 8f9b261
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
76 changes: 76 additions & 0 deletions cmd/slackdump/internal/wizard/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package wizard

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
)

type model struct {
form *huh.Form
val string
}

func newModel(m *menu) model {
var options []huh.Option[string]
for i, name := range m.names {
var text = fmt.Sprintf("%-10s - %s", name, m.items[i].Description)
if m.items[i].Description == "" {
text = fmt.Sprintf("%-10s", name)
}
options = append(options, huh.NewOption(text, name))
}
return model{
form: huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Key("selection").
Title(m.title).
Description(currentWsp()).
Options(options...),
),
),
}
}

func (m *model) Init() tea.Cmd {
return m.form.Init()
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "ctrl+c", "q":
return m, tea.Quit
}
}

var cmds []tea.Cmd
form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.form = f
cmds = append(cmds, cmd)
}

if m.form.State == huh.StateCompleted {
m.val = m.form.GetString("selection")
cmds = append(cmds, tea.Quit)
}

return m, tea.Batch(cmds...)
}

func (m *model) View() string {
return m.form.View()
}

func currentWsp() string {
if current, err := workspace.Current(cfg.CacheDir(), cfg.Workspace); err == nil {
return fmt.Sprintf("Slack Workspace: %s", current)
}
return ""
}
24 changes: 5 additions & 19 deletions cmd/slackdump/internal/wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

"github.com/charmbracelet/huh"
tea "github.com/charmbracelet/bubbletea"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -112,26 +112,13 @@ func makeMenu(cmds []*base.Command, parent string, title string) (m *menu) {
}

func show(m *menu, onMatch func(cmd *base.Command) error) error {
var options []huh.Option[string]
for i, name := range m.names {
var text = fmt.Sprintf("%-10s - %s", name, m.items[i].Description)
if m.items[i].Description == "" {
text = fmt.Sprintf("%-10s", name)
}
options = append(options, huh.NewOption(text, name))
}
for {
var resp string
err := huh.NewSelect[string]().
Title(m.title).
// Options(huh.NewOptions(m.names...)...).
Options(options...).
Value(&resp).
Run()
if err != nil {
mod := newModel(m)
p := tea.NewProgram(&mod)
if _, err := p.Run(); err != nil {
return err
}
if err := run(m, resp, onMatch); err != nil {
if err := run(m, mod.val, onMatch); err != nil {
if errors.Is(err, errBack) {
return nil
} else if errors.Is(err, errInvalid) {
Expand All @@ -141,7 +128,6 @@ func show(m *menu, onMatch func(cmd *base.Command) error) error {
}
}
}

}

var (
Expand Down

0 comments on commit 8f9b261

Please sign in to comment.