// NewFileBrowser creates a new file browser from a string pointing to a file
// or directory. If it is a file, then that is the initial file selected by the
// returned FileBrowser. If it is a directory, then the index will start at 0.
NewFileBrowser creates a new file browser from a string pointing to a file
or directory. If it is a file, then that is the initial file selected by the
returned FileBrowser. If it is a directory, then the index will start at 0.
// NewFileBrowser creates a new file browser from a string pointing to a file // or directory . If it is a file then that is the initial file selected by the // returned FileBrowser . If it is a directory then the index will start at 0 .
func NewFileBrowser(filename string, extensions map[string]struct{}) (browser FileBrowser, err error) {
	var currentDir string
	absoluteFilename, err := absPath(filename)
	if err != nil {
		return browser, newFileBrowserError(filename, err)
	}
	currentFileStats, err := osStat(absoluteFilename)
	if err != nil {
		return browser, newFileBrowserError(filename, err)
	} else if currentFileStats.IsDir() {
		currentDir = absoluteFilename
	} else {
		currentDir = filepath.Dir(absoluteFilename)
	}
	matches, _ := filepath.Glob(filepath.Join(currentDir, "*"))
	for _, fpath := range matches {
		if _, ok := extensions[strings.ToLower(filepath.Ext(fpath))]; !ok {
			continue
		}
		absFpath, err := absPath(fpath)
		if err != nil {
			return browser, err
		}
		fileStats, err := os.Stat(absFpath)
		if err != nil {
			return browser, err
		}
		if fileStats.IsDir() {
			continue
		}
		if os.SameFile(currentFileStats, fileStats) {
			browser.index = len(browser.Filenames)
		}
		browser.Filenames = append(browser.Filenames, absFpath)
	}
	return
}
func NewFileBrowser ( filename string , extensions map [ string ] struct { } ) ( browser FileBrowser , err error ) { var currentDir string absoluteFilename , err := absPath ( filename ) if err != nil { return browser , newFileBrowserError ( filename , err ) } currentFileStats , err := osStat ( absoluteFilename ) if err != nil { return browser , newFileBrowserError ( filename , err ) } else if currentFileStats . IsDir ( ) { currentDir = absoluteFilename } else { currentDir = filepath . Dir ( absoluteFilename ) } matches , _ := filepath . Glob ( filepath . Join ( currentDir , " " ) ) for _ , fpath := range matches { if _ , ok := extensions [ strings . ToLower ( filepath . Ext ( fpath ) ) ] ; ! ok { continue } absFpath , err := absPath ( fpath ) if err != nil { return browser , err } fileStats , err := os . Stat ( absFpath ) if err != nil { return browser , err } if fileStats . IsDir ( ) { continue } if os . SameFile ( currentFileStats , fileStats ) { browser . index = len ( browser . Filenames ) } browser . Filenames = append ( browser . Filenames , absFpath ) } return }
NewFileBrowser creates a new file browser from a string pointing to a file
or directory.
NewFileBrowser creates a new file browser from a string pointing to a file or directory .
// NOTE Skips if extension is not supported