Skip to content

Commit

Permalink
feat: lots of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Mar 23, 2024
1 parent 63123a6 commit e0dce84
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 209 deletions.
5 changes: 5 additions & 0 deletions code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (m *Model) GotoTop() {
m.Viewport.GotoTop()
}

// GotoBottom jumps to the bottom of the viewport.
func (m *Model) GotoBottom() {
m.Viewport.GotoBottom()
}

// SetViewportDisabled toggles the state of the viewport.
func (m *Model) SetViewportDisabled(disabled bool) {
m.ViewportDisabled = disabled
Expand Down
107 changes: 56 additions & 51 deletions filetree/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,58 @@ import (
"github.com/mistakenelf/fm/filesystem"
)

type getDirectoryListingMsg []DirectoryItem
type getDirectoryListingMsg struct {
files []DirectoryItem
workingDirectory string
}
type errorMsg string
type copyToClipboardMsg string
type statusMessageTimeoutMsg struct{}
type editorFinishedMsg struct{ err error }
type createFileMsg struct{}
type createDirectoryMsg struct{}

// getDirectoryListingCmd updates the directory listing based on the name of the directory provided.
func getDirectoryListingCmd(directoryName string, showHidden, directoriesOnly, filesOnly bool) tea.Cmd {
// NewStatusMessageCmd sets a new status message, which will show for a limited
// amount of time. Note that this also returns a command.
func (m *Model) NewStatusMessageCmd(s string) tea.Cmd {
m.StatusMessage = s

if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}

m.statusMessageTimer = time.NewTimer(m.StatusMessageLifetime)

return func() tea.Msg {
<-m.statusMessageTimer.C
return statusMessageTimeoutMsg{}
}
}

// CreateDirectoryCmd creates a directory based on the name provided.
func (m *Model) CreateDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := filesystem.CreateDirectory(name); err != nil {
return errorMsg(err.Error())
}

return createDirectoryMsg{}
}
}

// CreateFileCmd creates a file based on the name provided.
func (m *Model) CreateFileCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := filesystem.CreateFile(name); err != nil {
return errorMsg(err.Error())
}

return createFileMsg{}
}
}

// GetDirectoryListingCmd updates the directory listing based on the name of the directory provided.
func (m Model) GetDirectoryListingCmd(directoryName string) tea.Cmd {
return func() tea.Msg {
var err error
var directoryItems []DirectoryItem
Expand All @@ -50,19 +92,19 @@ func getDirectoryListingCmd(directoryName string, showHidden, directoriesOnly, f
return errorMsg(err.Error())
}

if !directoriesOnly && !filesOnly {
files, err = filesystem.GetDirectoryListing(directoryName, showHidden)
if !m.showDirectoriesOnly && !m.showFilesOnly {
files, err = filesystem.GetDirectoryListing(directoryName, m.showHidden)
if err != nil {
return errorMsg(err.Error())
}
} else {
listingType := filesystem.DirectoriesListingType

if filesOnly {
if m.showFilesOnly {
listingType = filesystem.FilesListingType
}

files, err = filesystem.GetDirectoryListingByType(directoryName, listingType, showHidden)
files, err = filesystem.GetDirectoryListingByType(directoryName, listingType, m.showHidden)
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -79,22 +121,24 @@ func getDirectoryListingCmd(directoryName string, showHidden, directoriesOnly, f
continue
}

status := fmt.Sprintf("%s %s",
ConvertBytesToSizeString(fileInfo.Size()),
fileInfo.Mode().String())
fileSize := ConvertBytesToSizeString(fileInfo.Size())

directoryItems = append(directoryItems, DirectoryItem{
Name: file.Name(),
Details: status,
Details: fileInfo.Mode().String(),
Path: filepath.Join(workingDirectory, file.Name()),
Extension: filepath.Ext(fileInfo.Name()),
IsDirectory: fileInfo.IsDir(),
CurrentDirectory: workingDirectory,
FileInfo: fileInfo,
FileSize: fileSize,
})
}

return getDirectoryListingMsg(directoryItems)
return getDirectoryListingMsg{
files: directoryItems,
workingDirectory: workingDirectory,
}
}
}

Expand All @@ -115,28 +159,6 @@ func deleteDirectoryItemCmd(name string, isDirectory bool) 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 := filesystem.CreateDirectory(name); err != nil {
return errorMsg(err.Error())
}

return createDirectoryMsg{}
}
}

// CreateFileCmd creates a file based on the name provided.
func (m *Model) CreateFileCmd(name string) tea.Cmd {
return func() tea.Msg {
if err := filesystem.CreateFile(name); err != nil {
return errorMsg(err.Error())
}

return createFileMsg{}
}
}

// zipDirectoryCmd zips a directory based on the name provided.
func zipDirectoryCmd(name string) tea.Cmd {
return func() tea.Msg {
Expand Down Expand Up @@ -205,23 +227,6 @@ func writeSelectionPathCmd(selectionPath, filePath string) tea.Cmd {
}
}

// NewStatusMessage sets a new status message, which will show for a limited
// amount of time.
func (m *Model) NewStatusMessageCmd(s string) tea.Cmd {
m.StatusMessage = s

if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}

m.statusMessageTimer = time.NewTimer(m.StatusMessageLifetime)

return func() tea.Msg {
<-m.statusMessageTimer.C
return statusMessageTimeoutMsg{}
}
}

func openEditorCmd(file string) tea.Cmd {
editor := os.Getenv("EDITOR")
if editor == "" {
Expand Down
2 changes: 1 addition & 1 deletion filetree/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func (m Model) Init() tea.Cmd {
return getDirectoryListingCmd(m.startDir, true, false, false)
return m.GetDirectoryListingCmd(m.startDir)
}
2 changes: 1 addition & 1 deletion filetree/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m Model) GetTotalItems() int {

// SetSize Sets the size of the filetree.
func (m *Model) SetSize(width, height int) {
m.height = height - 2
m.height = height
m.width = width
m.max = m.height - 1
}
Expand Down
2 changes: 2 additions & 0 deletions filetree/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type DirectoryItem struct {
Details string
Path string
Extension string
FileSize string
CurrentDirectory string
IsDirectory bool
FileInfo os.FileInfo
Expand Down Expand Up @@ -43,6 +44,7 @@ type Model struct {
unselectedItemColor lipgloss.AdaptiveColor
inactiveItemColor lipgloss.AdaptiveColor
err error
CurrentDirectory string
}

func New(startDir string) Model {
Expand Down

0 comments on commit e0dce84

Please sign in to comment.