Skip to content

Commit

Permalink
feat: rename directory item
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Apr 6, 2024
1 parent f4fae48 commit d4c4b82
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions filetree/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type editorFinishedMsg struct{ err error }
type createFileMsg struct{}
type createDirectoryMsg struct{}
type moveDirectoryItemMsg struct{}
type renameDirectoryItemMsg struct{}

// NewStatusMessageCmd sets a new status message, which will show for a limited
// amount of time. Note that this also returns a command.
Expand Down Expand Up @@ -65,6 +66,17 @@ func (m *Model) CreateFileCmd(name string) tea.Cmd {
}
}

// RenameDirectoryItemCmd renames a file or folder given a name.
func (m *Model) RenameDirectoryItemCmd(originalName, newName string) tea.Cmd {
return func() tea.Msg {
if err := filesystem.RenameDirectoryItem(originalName, newName); err != nil {
return errorMsg(err.Error())
}

return renameDirectoryItemMsg{}
}
}

// MoveDirectoryItemCmd moves an item from one place to another.
func (m Model) MoveDirectoryItemCmd(source, destination string) tea.Cmd {
return func() tea.Msg {
Expand Down
1 change: 1 addition & 0 deletions filetree/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
CreateFileState
CreateDirectoryState
MoveState
RenameState
)

type DirectoryItem struct {
Expand Down
10 changes: 10 additions & 0 deletions filetree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case createDirectoryMsg:
m.State = IdleState

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case renameDirectoryItemMsg:
m.State = IdleState

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case getDirectoryListingMsg:
if msg.files != nil {
Expand Down Expand Up @@ -273,6 +277,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.State = CreateDirectoryState

return m, nil
case key.Matches(msg, m.keyMap.RenameDirectoryItem):
if m.State != IdleState {
return m, nil
}

m.State = RenameState
}
}

Expand Down
11 changes: 10 additions & 1 deletion internal/tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.secondaryFiletree.CurrentDirectory+"/"+m.filetree.GetSelectedItem().Name,
),
)
case m.filetree.State == filetree.RenameState:
cmds = append(cmds,
m.filetree.RenameDirectoryItemCmd(
m.filetree.GetSelectedItem().Path,
m.filetree.CurrentDirectory+"/"+m.textinput.Value(),
),
)
default:
return m, nil
}
Expand Down Expand Up @@ -157,7 +164,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

if m.filetree.State == filetree.CreateDirectoryState || m.filetree.State == filetree.CreateFileState {
if m.filetree.State == filetree.CreateDirectoryState ||
m.filetree.State == filetree.CreateFileState ||
m.filetree.State == filetree.RenameState {
m.textinput, cmd = m.textinput.Update(msg)
cmds = append(cmds, cmd)
}
Expand Down
4 changes: 3 additions & 1 deletion keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type KeyMap struct {
GotoTop key.Binding
GotoBottom key.Binding
MoveDirectoryItem key.Binding
RenameDirectoryItem key.Binding
}

func DefaultKeyMap() KeyMap {
Expand All @@ -42,7 +43,7 @@ func DefaultKeyMap() KeyMap {
TogglePane: key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "Toggle between l/r panes")),
OpenFile: key.NewBinding(key.WithKeys("l", "right"), key.WithHelp("l", "Preview file")),
ResetState: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "Reset state")),
ShowTextInput: key.NewBinding(key.WithKeys("N", "M"), key.WithHelp("N, M", "Show text input to create file or directory")),
ShowTextInput: key.NewBinding(key.WithKeys("N", "M", "R"), key.WithHelp("N, M", "Show text input to create file or directory")),
Submit: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "Submit text input value")),
GotoTop: key.NewBinding(key.WithKeys("g"), key.WithHelp("g", "Go to top of pane")),
GotoBottom: key.NewBinding(key.WithKeys("G"), key.WithHelp("G", "Go to bottom of pane")),
Expand All @@ -67,5 +68,6 @@ func DefaultKeyMap() KeyMap {
OpenInEditor: key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "Open in $EDITOR")),
CreateFile: key.NewBinding(key.WithKeys("N"), key.WithHelp("N", "Create new file")),
CreateDirectory: key.NewBinding(key.WithKeys("M"), key.WithHelp("M", "Create new directory")),
RenameDirectoryItem: key.NewBinding(key.WithKeys("R"), key.WithHelp("R", "Rename directory items")),
}
}

0 comments on commit d4c4b82

Please sign in to comment.