Skip to content

Filepicker stuck in home dir #905

@squeklow

Description

@squeklow

Describe the bug
When launching my application (Filepicker.Model implemented as it is), Sometimes the Filepicker will get stuck in the home dir and not go inside the selected user

Setup
Please complete the following information along with version numbers, if applicable.

  • OS - endaveour OS
  • Shell - bash
  • Terminal Emulator Kitty

To Reproduce
NO idea how you can reproduce will attach code and screenshots

Source Code

package main

import (
	"errors"
	"strings"
	"time"

	"charm.land/bubbles/v2/filepicker"
	tea "charm.land/bubbletea/v2"
)

type Explorer struct {
	Filepicker filepicker.Model
	File       Page
	err        error
}

func (e Explorer) Init() tea.Cmd {
	return e.Filepicker.Init()
}
func (e Explorer) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg.(type) {
	case clearErrorMsg:
		e.err = nil
	}

	var cmd tea.Cmd
	e.Filepicker, cmd = e.Filepicker.Update(msg)

	// Did the user select a file?
	if didSelect, path := e.Filepicker.DidSelectFile(msg); didSelect {
		// Get the path of the selected file.
		e.File.filePath = path
		FileSelected = true
		return e, cmd
	}

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

	return e, cmd
}
func (e Explorer) View() tea.View {
	var v tea.View
	var s strings.Builder
	s.WriteString("\n  ")
	if e.err != nil {
		s.WriteString(e.Filepicker.Styles.DisabledFile.Render(e.err.Error()))
	} else if e.File.filePath == "" {
		s.WriteString("Pick a file:")
	} else {
		s.WriteString("Selected file: " + e.Filepicker.Styles.Selected.Render(e.File.filePath))
	}
	s.WriteString("\n\n" + e.Filepicker.View() + "\n")
	v.Content = s.String()
	return v
}

func initExplorer() Explorer {
	fp := filepicker.New()
	fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}
	fp.CurrentDirectory = HOME
	return Explorer{
		Filepicker: fp,
	}
}

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

type clearErrorMsg struct{}

var FileSelected bool

func (r Root) OpnSel() {
	Files.Recent = r.Explorer.File
	r.Editor.File = r.Explorer.File
	Mode = "Editor"
}

type Data struct {
	Recent Page
	Pages  []Page
}
type Page struct {
	fileName string
	filePath string
	crd      string
}

Expected behavior
simple navigation

Screenshots

Screencast_20260311_231506.webm

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions