Skip to content

Commit

Permalink
add picklist updater
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 3, 2024
1 parent 05910cf commit bf7dc46
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/slackdump/internal/export/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"errors"
"regexp"

"github.com/charmbracelet/huh"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/cfgui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/dumpui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/updaters"
"github.com/rusq/slackdump/v3/internal/chunk/transform/fileproc"
)

func wizExport(ctx context.Context, cmd *base.Command, args []string) error {
Expand All @@ -30,8 +32,15 @@ func (fl *exportFlags) configuration() cfgui.Configuration {
Name: "Export Storage Type",
Value: fl.ExportStorageType.String(),
Description: "Export file storage type",
Inline: true,
// TODO: V3 Implement Updater for ExportStorageType
Inline: false,
Updater: updaters.NewPicklist(&fl.ExportStorageType, huh.NewSelect[fileproc.StorageType]().
Title("Choose File storage type").
Description("test").
Options(
huh.NewOption("Mattermost", fileproc.STmattermost),
huh.NewOption("Standard", fileproc.STstandard),
huh.NewOption("Disable", fileproc.STnone),
)),
},
{
Name: "Member Only",
Expand Down
31 changes: 31 additions & 0 deletions cmd/slackdump/internal/ui/updaters/examples/picklist/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"log"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/updaters"
"github.com/rusq/slackdump/v3/internal/chunk/transform/fileproc"
)

func main() {
var result fileproc.StorageType

updaters.OnClose = tea.Quit

l := updaters.NewPicklist(&result, huh.NewSelect[fileproc.StorageType]().
Title("Title").
Description("Description").
Options(
huh.NewOption("None", fileproc.STnone),
huh.NewOption("Standard", fileproc.STstandard),
huh.NewOption("Mattermost", fileproc.STmattermost),
))

if _, err := tea.NewProgram(l).Run(); err != nil {
log.Fatal(err)
}
fmt.Println("selected: ", result)
}
71 changes: 71 additions & 0 deletions cmd/slackdump/internal/ui/updaters/picklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package updaters

import (
"github.com/charmbracelet/bubbles/help"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
)

type Model[T comparable] struct {
s *huh.Select[T]
finishing bool
help help.Model

// value
initial T
ptr *T
}

func NewPicklist[T comparable](v *T, s *huh.Select[T]) *Model[T] {
s = s.Value(v).
WithTheme(ui.HuhTheme).
WithKeyMap(huh.NewDefaultKeyMap()).(*huh.Select[T])

m := &Model[T]{
s: s,
help: help.New(),

initial: *v,
ptr: v,
}
return m
}

func (m *Model[T]) Init() tea.Cmd {
return tea.Batch(m.s.Init(), m.s.Focus())
}

func (m *Model[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
// restore initial value
*m.ptr = m.initial
m.finishing = true
cmds = append(cmds, OnClose)
case "enter":
m.finishing = true
cmds = append(cmds, OnClose)
}
}
{
// update the select control
mod, cmd := m.s.Update(msg)
if mod, ok := mod.(*huh.Select[T]); ok {
m.s = mod
}
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}

func (m *Model[T]) View() string {
if m.finishing {
return ""
}
return lipgloss.JoinVertical(lipgloss.Left, m.s.View(), m.help.ShortHelpView(m.s.KeyBinds()))
}

0 comments on commit bf7dc46

Please sign in to comment.