-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |