Skip to content

Commit

Permalink
toggle between directories and files
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Oct 21, 2021
1 parent 8dfacb0 commit 7d3cb67
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
25 changes: 25 additions & 0 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ func GetDirectoryListing(dir string, showHidden bool) ([]fs.DirEntry, error) {
return files, nil
}

// GetDirectoryListingByType returns a directory listing based on type (directories | files).
func GetDirectoryListingByType(dir, listType string) ([]fs.DirEntry, error) {
n := 0

// Read files from the directory.
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}

for _, file := range files {
if file.IsDir() && listType == "directories" {
files[n] = file
n++
}

if !file.IsDir() && listType == "files" {
files[n] = file
n++
}
}

return files[:n], nil
}

// DeleteDirectory deletes a directory given a name.
func DeleteDirectory(name string) error {
err := os.RemoveAll(name)
Expand Down
17 changes: 17 additions & 0 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,20 @@ func (m Model) copyToClipboardCmd(name string) tea.Cmd {
return copyToClipboardMsg(fmt.Sprintf("%s %s %s", "Successfully copied", filePath, "to clipboard"))
}
}

// getDirectoryListingByTypeCmd returns only directories in the current directory.
func (m Model) getDirectoryListingByType(listType string) tea.Cmd {
return func() tea.Msg {
workingDir, err := dirfs.GetWorkingDirectory()
if err != nil {
return errorMsg(err.Error())
}

directories, err := dirfs.GetDirectoryListingByType(workingDir, listType)
if err != nil {
return errorMsg(err.Error())
}

return updateDirectoryListingMsg(directories)
}
}
14 changes: 14 additions & 0 deletions internal/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type keyMap struct {
EditFile key.Binding
PreviewDirectory key.Binding
CopyToClipboard key.Binding
ShowOnlyDirectories key.Binding
ShowOnlyFiles key.Binding
}

// ShortHelp returns keybindings to be shown in the mini help view. It's part
Expand Down Expand Up @@ -62,6 +64,8 @@ func (k keyMap) ShortHelp() []key.Binding {
k.EditFile,
k.PreviewDirectory,
k.CopyToClipboard,
k.ShowOnlyDirectories,
k.ShowOnlyFiles,
}
}

Expand Down Expand Up @@ -96,6 +100,8 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.EditFile,
k.PreviewDirectory,
k.CopyToClipboard,
k.ShowOnlyDirectories,
k.ShowOnlyFiles,
},
}
}
Expand Down Expand Up @@ -207,5 +213,13 @@ func getDefaultKeyMap() keyMap {
key.WithKeys("y"),
key.WithHelp("y", "copy selected directory items path to clipboard"),
),
ShowOnlyDirectories: key.NewBinding(
key.WithKeys("S"),
key.WithHelp("S", "only show directories in the directory tree"),
),
ShowOnlyFiles: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "only show files in the directory tree"),
),
}
}
19 changes: 12 additions & 7 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.secondaryPane.GotoTop()

return m, nil

// Jump to the bottom of a pane.
case key.Matches(msg, m.keys.JumpToBottom):
if !m.showCommandInput && m.primaryPane.GetIsActive() {
Expand Down Expand Up @@ -547,9 +545,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if err != nil {
return m, m.handleErrorCmd(err)
}
}

return m, m.updateDirectoryListingCmd(dirfs.CurrentDirectory)
return m, m.updateDirectoryListingCmd(dirfs.CurrentDirectory)
}

case key.Matches(msg, m.keys.PreviewDirectory):
if !m.showCommandInput && m.primaryPane.GetIsActive() && m.dirTree.GetSelectedFile().IsDir() {
Expand All @@ -563,14 +561,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
}

return m, nil

case key.Matches(msg, m.keys.CopyToClipboard):
if !m.showCommandInput && m.primaryPane.GetIsActive() && m.dirTree.GetTotalFiles() > 0 {
return m, m.copyToClipboardCmd(m.dirTree.GetSelectedFile().Name())
}

return m, nil
case key.Matches(msg, m.keys.ShowOnlyDirectories):
if !m.showCommandInput && m.primaryPane.GetIsActive() && m.dirTree.GetTotalFiles() > 0 {
return m, m.getDirectoryListingByType("directories")
}

case key.Matches(msg, m.keys.ShowOnlyFiles):
if !m.showCommandInput && m.primaryPane.GetIsActive() && m.dirTree.GetTotalFiles() > 0 {
return m, m.getDirectoryListingByType("files")
}

// Reset FM to its initial state.
case key.Matches(msg, m.keys.Escape):
Expand All @@ -596,6 +600,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.renderer.SetContent("")
m.dirTreePreview.SetContent(nil)
m.updateStatusBarContent()
cmds = append(cmds, m.updateDirectoryListingCmd(dirfs.CurrentDirectory))
}
}

Expand Down

0 comments on commit 7d3cb67

Please sign in to comment.