Skip to content

Commit

Permalink
filepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 10, 2024
1 parent f6b24d0 commit bc8f6ee
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
11 changes: 5 additions & 6 deletions cmd/slackdump/internal/apiconfig/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
)
Expand Down Expand Up @@ -46,14 +47,12 @@ func runConfigCheck(ctx context.Context, cmd *base.Command, args []string) error
}

func wizConfigCheck(ctx context.Context, cmd *base.Command, args []string) error {
filename, err := ui.FileSelector(
"Input a config file name to check",
"Enter the config file name. It must exist and be a regular file.",
ui.WithMustExist(true),
)
fp := ui.NewFilePicker("Select a config file to check", ".", "yaml", "yml")
ret, err := tea.NewProgram(fp).Run()
if err != nil {
return err
}
fp = ret.(ui.FileSystemModel)

return runConfigCheck(ctx, cmd, []string{filename})
return runConfigCheck(ctx, cmd, []string{fp.SelectedFile})
}
93 changes: 93 additions & 0 deletions cmd/slackdump/internal/ui/filepicker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ui

import (
"errors"
"strings"
"time"

"github.com/charmbracelet/bubbles/filepicker"
tea "github.com/charmbracelet/bubbletea"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
)

type FileSystemModel struct {
filepicker filepicker.Model
SelectedFile string
quitting bool
err error
Propmt string
}

func NewFilePicker(prompt string, homedir string, allowedExt ...string) FileSystemModel {
fp := filepicker.New()
fp.AllowedTypes = allowedExt
fp.CurrentDirectory = homedir
fp.Styles.Cursor = cfg.Theme.Focused.SelectedOption

return FileSystemModel{
filepicker: fp,
Propmt: prompt,
}
}

type clearErrorMsg struct{}

func clearErrorAfter(t time.Duration) tea.Cmd {
return tea.Tick(t, func(_ time.Time) tea.Msg {
return clearErrorMsg{}
})
}

func (m FileSystemModel) Init() tea.Cmd {
return m.filepicker.Init()
}

func (m FileSystemModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.quitting = true
return m, tea.Quit
}
case clearErrorMsg:
m.err = nil
}

var cmd tea.Cmd
m.filepicker, cmd = m.filepicker.Update(msg)

// Did the user select a file?
if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {
// Get the path of the selected file.
m.SelectedFile = path
}

// Did the user select a disabled file?
// This is only necessary to display an error to the user.
if didSelect, path := m.filepicker.DidSelectDisabledFile(msg); didSelect {
// Let's clear the selectedFile and display an error.
m.err = errors.New(path + " is not valid.")
m.SelectedFile = ""
return m, tea.Batch(cmd, clearErrorAfter(2*time.Second))
}

return m, cmd
}

func (m FileSystemModel) View() string {
if m.quitting {
return ""
}
var s strings.Builder
s.WriteString("\n ")
if m.err != nil {
s.WriteString(m.filepicker.Styles.DisabledFile.Render(m.err.Error()))
} else if m.SelectedFile == "" {
s.WriteString(m.Propmt)
} else {
s.WriteString("Selected file: " + m.filepicker.Styles.Selected.Render(m.SelectedFile))
}
s.WriteString("\n\n" + m.filepicker.View() + "\n")
return s.String()
}

0 comments on commit bc8f6ee

Please sign in to comment.