Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/knipferrc/fm into feature/o…
Browse files Browse the repository at this point in the history
…pen-in-editor
  • Loading branch information
mistakenelf committed Oct 6, 2021
2 parents 8542cb6 + 36fe8a9 commit 22c0b72
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 105 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var rootCmd = &cobra.Command{
Use: "fm",
Short: "FM is a simple, configurable, and fun to use file manager",
Version: "0.5.2",
Version: "0.5.3",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config.SetDefaults()
Expand Down
37 changes: 15 additions & 22 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,30 +390,23 @@ func GetDirectoryItemSize(path string) (int64, error) {
return curFile.Size(), nil
}

entries, err := os.ReadDir(path)
if err != nil {
return 0, err
}

var total int64
for _, entry := range entries {
if entry.IsDir() {
size, err := GetDirectoryItemSize(filepath.Join(path, entry.Name()))
if err != nil {
return 0, err
}

total += size
} else {
info, err := entry.Info()
var size int64
err = filepath.WalkDir(path, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}

if err != nil {
return 0, err
}
fileInfo, err := d.Info()
if err != nil {
return err
}

total += info.Size()
if !d.IsDir() {
size += fileInfo.Size()
}
}

return total, nil
return err
})

return size, err
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/alecthomas/chroma v0.9.2
github.com/charmbracelet/bubbles v0.9.0
github.com/charmbracelet/bubbletea v0.16.0
github.com/charmbracelet/bubbletea v0.17.0
github.com/charmbracelet/glamour v0.3.0
github.com/charmbracelet/lipgloss v0.4.0
github.com/lucasb-eyer/go-colorful v1.2.0
Expand Down Expand Up @@ -41,8 +41,8 @@ require (
github.com/subosito/gotenv v1.2.0 // indirect
github.com/yuin/goldmark v1.4.1 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 // indirect
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70 // indirect
golang.org/x/net v0.0.0-20211005001312-d4b1ae081e3b // indirect
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/charmbracelet/bubbles v0.9.0 h1:lqJ8FXwoLceQF2J0A+dWo1Cuu1dNyjbW4Opgdi2vkhw=
github.com/charmbracelet/bubbles v0.9.0/go.mod h1:NWT/c+0rYEnYChz5qCyX4Lj6fDw9gGToh9EFJPajghU=
github.com/charmbracelet/bubbletea v0.14.1/go.mod h1:b5lOf5mLjMg1tRn1HVla54guZB+jvsyV0yYAQja95zE=
github.com/charmbracelet/bubbletea v0.16.0 h1:LAI0Rf6rPIiLYVamphgKYsLpPjkWRcxayzfmkbUkRqk=
github.com/charmbracelet/bubbletea v0.16.0/go.mod h1:YTZSs2p3odhwYZdhqJheYHVUjU37c9OLgS85kw6NGQY=
github.com/charmbracelet/bubbletea v0.17.0 h1:ABmd01l6K7Q8lSQxF7yG8yWayz5gXWSt3JfB/X1eYP4=
github.com/charmbracelet/bubbletea v0.17.0/go.mod h1:YTZSs2p3odhwYZdhqJheYHVUjU37c9OLgS85kw6NGQY=
github.com/charmbracelet/glamour v0.3.0 h1:3H+ZrKlSg8s+WU6V7eF2eRVYt8lCueffbi7r2+ffGkc=
github.com/charmbracelet/glamour v0.3.0/go.mod h1:TzF0koPZhqq0YVBNL100cPHznAAjVj7fksX2RInwjGw=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
Expand Down Expand Up @@ -455,8 +455,8 @@ golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 h1:Z04ewVs7JhXaYkmDhBERPi41gnltfQpMWDnTnQbaCqk=
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211005001312-d4b1ae081e3b h1:SXy8Ld8oKlcogOvUAh0J5Pm5RKzgYBMMxLxt6n5XW50=
golang.org/x/net v0.0.0-20211005001312-d4b1ae081e3b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -543,8 +543,8 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70 h1:pGleJoyD1yA5HfvuaksHxD0404gsEkNDerKsQ0N0y1s=
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef h1:fPxZ3Umkct3LZ8gK9nbk+DWDJ9fstZa2grBn+lWVKPs=
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
Expand Down
63 changes: 35 additions & 28 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type readFileContentMsg struct {
image image.Image
}

// updateDirectoryListing updates the directory listing based on the name of the direcoctory provided.
func (m Model) updateDirectoryListing(name string) tea.Cmd {
// updateDirectoryListingCmd updates the directory listing based on the name of the direcoctory provided.
func (m Model) updateDirectoryListingCmd(name string) tea.Cmd {
return func() tea.Msg {
files, err := dirfs.GetDirectoryListing(name, m.dirTree.ShowHidden)
if err != nil {
Expand All @@ -48,8 +48,8 @@ func (m Model) updateDirectoryListing(name string) tea.Cmd {
}
}

// renameFileOrDir renames a file or directory based on the name and value provided.
func (m Model) renameFileOrDir(name, value string) tea.Cmd {
// renameFileOrDirCmd renames a file or directory based on the name and value provided.
func (m Model) renameFileOrDirCmd(name, value string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.RenameDirOrFile(name, value); err != nil {
return errorMsg(err.Error())
Expand All @@ -59,8 +59,8 @@ func (m Model) renameFileOrDir(name, value string) tea.Cmd {
}
}

// moveDirectoryItem moves a file to the current working directory.
func (m Model) moveDirectoryItem(name string) tea.Cmd {
// moveDirectoryItemCmd moves a file to the current working directory.
func (m Model) moveDirectoryItemCmd(name string) tea.Cmd {
return func() tea.Msg {
workingDir, err := dirfs.GetWorkingDirectory()
if err != nil {
Expand Down Expand Up @@ -88,8 +88,8 @@ func (m Model) moveDirectoryItem(name string) tea.Cmd {
}
}

// deleteDir deletes a directory based on the name provided.
func (m Model) deleteDir(name string) tea.Cmd {
// deleteDirectoryCmd deletes a directory based on the name provided.
func (m Model) deleteDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.DeleteDirectory(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -99,8 +99,8 @@ func (m Model) deleteDir(name string) tea.Cmd {
}
}

// deleteFile deletes a file based on the name provided.
func (m Model) deleteFile(name string) tea.Cmd {
// deleteFileCmd deletes a file based on the name provided.
func (m Model) deleteFileCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.DeleteFile(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -110,8 +110,8 @@ func (m Model) deleteFile(name string) tea.Cmd {
}
}

// readFileContent reads the content of a file and returns it.
func (m Model) readFileContent(file os.FileInfo, width, height int) tea.Cmd {
// readFileContentCmd reads the content of a file and returns it.
func (m Model) readFileContentCmd(file os.FileInfo, width, height int) tea.Cmd {
return func() tea.Msg {
content, err := dirfs.ReadFileContent(file.Name())
if err != nil {
Expand Down Expand Up @@ -172,8 +172,8 @@ func (m Model) readFileContent(file os.FileInfo, width, height int) tea.Cmd {
}
}

// redrawImage redraws the image based on the width and height provided.
func (m Model) redrawImage(width, height int) tea.Cmd {
// redrawImageCmd redraws the image based on the width and height provided.
func (m Model) redrawImageCmd(width, height int) tea.Cmd {
return func() tea.Msg {
imageString, err := colorimage.ImageToString(uint(width), uint(height), m.colorimage.Image)
if err != nil {
Expand All @@ -184,8 +184,8 @@ func (m Model) redrawImage(width, height int) tea.Cmd {
}
}

// createDir creates a directory based on the name provided.
func (m Model) createDir(name string) tea.Cmd {
// createDirectoryCmd creates a directory based on the name provided.
func (m Model) createDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.CreateDirectory(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -195,8 +195,8 @@ func (m Model) createDir(name string) tea.Cmd {
}
}

// createFile creates a file based on the name provided.
func (m Model) createFile(name string) tea.Cmd {
// createFileCmd creates a file based on the name provided.
func (m Model) createFileCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.CreateFile(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -206,8 +206,8 @@ func (m Model) createFile(name string) tea.Cmd {
}
}

// zipDirectory zips a directory based on the name provided.
func (m Model) zipDirectory(name string) tea.Cmd {
// zipDirectoryCmd zips a directory based on the name provided.
func (m Model) zipDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
currentDir, err := dirfs.GetWorkingDirectory()
if err != nil {
Expand All @@ -223,8 +223,8 @@ func (m Model) zipDirectory(name string) tea.Cmd {
}
}

// unzipDirectory unzips a directory based on the name provided.
func (m Model) unzipDirectory(name string) tea.Cmd {
// unzipDirectoryCmd unzips a directory based on the name provided.
func (m Model) unzipDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
currentDir, err := dirfs.GetWorkingDirectory()
if err != nil {
Expand All @@ -240,8 +240,8 @@ func (m Model) unzipDirectory(name string) tea.Cmd {
}
}

// copyFile copies a file based on the name provided.
func (m Model) copyFile(name string) tea.Cmd {
// copyFileCmd copies a file based on the name provided.
func (m Model) copyFileCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.CopyFile(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -251,8 +251,8 @@ func (m Model) copyFile(name string) tea.Cmd {
}
}

// copyDirectory copies a directory based on the name provided.
func (m Model) copyDirectory(name string) tea.Cmd {
// copyDirectoryCmd copies a directory based on the name provided.
func (m Model) copyDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := dirfs.CopyDirectory(name); err != nil {
return errorMsg(err.Error())
Expand All @@ -262,8 +262,8 @@ func (m Model) copyDirectory(name string) tea.Cmd {
}
}

// getDirectoryItemSize calculates the size of a directory or file.
func (m Model) getDirectoryItemSize(name string) tea.Cmd {
// getDirectoryItemSizeCmd calculates the size of a directory or file.
func (m Model) getDirectoryItemSizeCmd(name string) tea.Cmd {
if m.directoryItemSizeCtx != nil && m.directoryItemSizeCtx.cancel != nil {
m.directoryItemSizeCtx.cancel()
}
Expand All @@ -288,3 +288,10 @@ func (m Model) getDirectoryItemSize(name string) tea.Cmd {
return nil
}
}

// handleErrorCmd returns an error message to the UI.
func (m Model) handleErrorCmd(err error) tea.Cmd {
return func() tea.Msg {
return errorMsg(err.Error())
}
}
8 changes: 4 additions & 4 deletions internal/ui/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m Model) Init() tea.Cmd {
}

if strings.HasPrefix(startDir, "/") {
cmds = append(cmds, m.updateDirectoryListing(startDir))
cmds = append(cmds, m.updateDirectoryListingCmd(startDir))
} else {
path, err := os.Getwd()
if err != nil {
Expand All @@ -39,17 +39,17 @@ func (m Model) Init() tea.Cmd {

filePath := filepath.Join(path, startDir)

cmds = append(cmds, m.updateDirectoryListing(filePath))
cmds = append(cmds, m.updateDirectoryListingCmd(filePath))
}
case m.appConfig.Settings.StartDir == dirfs.HomeDirectory:
homeDir, err := dirfs.GetHomeDirectory()
if err != nil {
log.Fatal(err)
}

cmds = append(cmds, m.updateDirectoryListing(homeDir))
cmds = append(cmds, m.updateDirectoryListingCmd(homeDir))
default:
cmds = append(cmds, m.updateDirectoryListing(m.appConfig.Settings.StartDir))
cmds = append(cmds, m.updateDirectoryListingCmd(m.appConfig.Settings.StartDir))
}

cmds = append(cmds, spinner.Tick)
Expand Down
Loading

0 comments on commit 22c0b72

Please sign in to comment.