Skip to content

Commit

Permalink
file manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 10, 2024
1 parent 0145a86 commit 4404409
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 115 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Config command allows to perform different operations on the API limits
configuration file.
`,
Commands: []*base.Command{
CmdConfigNew,
CmdConfigCheck,
CmdConfigNew,
},
}

Expand Down
78 changes: 27 additions & 51 deletions cmd/slackdump/internal/apiconfig/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"errors"
"fmt"
"strings"
"os"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/bubbles/filemgr"
)
Expand Down Expand Up @@ -40,68 +42,42 @@ func runConfigCheck(ctx context.Context, cmd *base.Command, args []string) error
return errors.New("config filename must be specified")
}
filename := args[0]
if _, err := Load(filename); err != nil {
if err := checkFile(filename); err != nil {
base.SetExitStatus(base.SUserError)
return fmt.Errorf("config file %q not OK: %s", filename, err)
return err
}
fmt.Printf("Config file %q: OK\n", filename)
return nil
}

func wizConfigCheck(ctx context.Context, cmd *base.Command, args []string) error {
f := filemgr.NewModel("*.yaml", ".")
f.Height = 8
m := checkerModel{
files: f,
view: viewport.New(40, f.Height+2),
}
_, err := tea.NewProgram(m).Run()
if err != nil {
return err
func checkFile(filename string) error {
if _, err := Load(filename); err != nil {
return fmt.Errorf("config file %q not OK: %s", filename, err)
}

// return runConfigCheck(ctx, cmd, []string{fp.files})
return nil
}

type checkerModel struct {
files filemgr.Model
view viewport.Model
finishing bool
}

func (m checkerModel) Init() tea.Cmd {
return tea.Batch(m.files.Init(), m.view.Init())
}

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

var cmds []tea.Cmd
var cmd tea.Cmd
m.files, cmd = m.files.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
func wizConfigCheck(ctx context.Context, cmd *base.Command, args []string) error {
f := filemgr.NewModel(".", 15, "*.yaml", "*.yml")
f.ShowHelp = true
f.Debug = os.Getenv("DEBUG") != ""
f.Style = filemgr.Style{
Normal: cfg.Theme.Focused.File,
Directory: cfg.Theme.Focused.Directory,
Inverted: lipgloss.NewStyle().
Foreground(cfg.Theme.Focused.FocusedButton.GetForeground()).
Background(cfg.Theme.Focused.FocusedButton.GetBackground()),
}
m.view, cmd = m.view.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
vp := viewport.New(80-filemgr.Width, f.Height)
vp.Style = lipgloss.NewStyle().Border(lipgloss.DoubleBorder(), true).Margin(0, 2)
m := checkerModel{
files: f,
view: vp,
}
return m, tea.Batch(cmds...)
}

func (m checkerModel) View() string {
if m.finishing {
return ""
if _, err := tea.NewProgram(m).Run(); err != nil {
return err
}
var buf strings.Builder
fmt.Fprintf(&buf, "%s\n%s", m.view.View(), m.files.View())
return buf.String()

return nil
}
85 changes: 85 additions & 0 deletions cmd/slackdump/internal/apiconfig/checker_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package apiconfig

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

type checkerModel struct {
files filemgr.Model
view viewport.Model
width int
finishing bool
}

func (m checkerModel) Init() tea.Cmd {
return tea.Batch(m.files.Init(), m.view.Init())
}

func (m checkerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case wmSetText:
m.view.Style.Foreground(msg.style.GetForeground())
m.view.SetContent(msg.text)
case tea.WindowSizeMsg:
m.width = msg.Width
m.view.Width = msg.Width - filemgr.Width
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.finishing = true
return m, tea.Quit
}
case filemgr.WMSelected:
filename := msg.Filepath
if err := checkFile(filename); err != nil {
cmds = append(cmds, wcmdErr(filename, err))
} else {
cmds = append(cmds, wcmdOK(filename))
}
}

var cmd tea.Cmd
m.files, cmd = m.files.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
m.view, cmd = m.view.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}

func (m checkerModel) View() string {
if m.finishing {
return ""
}
return lipgloss.JoinHorizontal(lipgloss.Top, m.files.View(), m.view.View())
}

type wmSetText struct {
text string
style lipgloss.Style
}

func wcmdErr(_ string, err error) tea.Cmd {
return func() tea.Msg {
return wmSetText{
text: err.Error(),
style: lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")),
}
}
}

func wcmdOK(filename string) tea.Cmd {
return func() tea.Msg {
return wmSetText{
text: "Config file OK: " + filename,
style: lipgloss.NewStyle().Foreground(lipgloss.Color("#00ff00")),
}
}
}
Loading

0 comments on commit 4404409

Please sign in to comment.