Skip to content

Commit

Permalink
feat: WIP move directory items
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Mar 23, 2024
1 parent e629e0d commit f48f050
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
12 changes: 12 additions & 0 deletions filetree/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type statusMessageTimeoutMsg struct{}
type editorFinishedMsg struct{ err error }
type createFileMsg struct{}
type createDirectoryMsg struct{}
type moveDirectoryItemMsg 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 @@ -64,6 +65,17 @@ func (m *Model) CreateFileCmd(name string) tea.Cmd {
}
}

// MoveDirectoryItemCmd moves an item from one place to another.
func (m Model) MoveDirectoryItemCmd(source, destination string) tea.Cmd {
return func() tea.Msg {
if err := filesystem.MoveDirectoryItem(source, destination); err != nil {
return errorMsg(err.Error())
}

return moveDirectoryItemMsg{}
}
}

// 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 {
Expand Down
5 changes: 5 additions & 0 deletions filetree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
Render(string(msg))))
case statusMessageTimeoutMsg:
m.StatusMessage = ""
case moveDirectoryItemMsg:
m.CreatingNewFile = false
m.CreatingNewDirectory = false

return m, m.GetDirectoryListingCmd(filesystem.CurrentDirectory)
case copyToClipboardMsg:
cmds = append(cmds, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Expand Down
1 change: 0 additions & 1 deletion filetree/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,5 @@ func (m Model) View() string {

return lipgloss.NewStyle().
Width(m.width).
Height(m.height).
Render(fileList.String())
}
4 changes: 2 additions & 2 deletions internal/tui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type keyMap struct {
OpenFile key.Binding
ResetState key.Binding
ShowTextInput key.Binding
SubmitTextInput key.Binding
Submit key.Binding
GotoTop key.Binding
GotoBottom key.Binding
MoveDirectoryItem key.Binding
Expand All @@ -21,7 +21,7 @@ func defaultKeyMap() keyMap {
OpenFile: key.NewBinding(key.WithKeys("l", "right"), key.WithHelp("l", "open 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")),
SubmitTextInput: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "submit text input")),
Submit: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "submit text input")),
GotoTop: key.NewBinding(key.WithKeys("g"), key.WithHelp("g", "go to top")),
GotoBottom: key.NewBinding(key.WithKeys("G"), key.WithHelp("G", "go to bottom")),
MoveDirectoryItem: key.NewBinding(key.WithKeys("m"), key.WithHelp("m", "move directory item")),
Expand Down
1 change: 1 addition & 0 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type model struct {
state sessionState
keyMap keyMap
activePane int
height int
config Config
showTextInput bool
textinput textinput.Model
Expand Down
27 changes: 18 additions & 9 deletions internal/tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
halfSize := msg.Width / 2
height := msg.Height - statusbar.Height
m.height = height

cmds = append(cmds, m.image.SetSizeCmd(halfSize, height))
cmds = append(cmds, m.markdown.SetSizeCmd(halfSize, height))

m.filetree.SetSize(halfSize, height-3)
m.secondaryFiletree.SetSize(halfSize, height)
m.secondaryFiletree.SetSize(halfSize, height-3)
m.help.SetSize(halfSize, height)
m.code.SetSize(halfSize, height)
m.pdf.SetSize(halfSize, height)
Expand All @@ -37,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keyMap.Quit):
return m, tea.Quit
case key.Matches(msg, m.keyMap.OpenFile):
if !m.showTextInput {
if !m.showTextInput && m.activePane == 0 {
cmds = append(cmds, m.openFileCmd())
}
case key.Matches(msg, m.keyMap.ResetState):
Expand All @@ -59,9 +60,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.textinput.Reset()
case key.Matches(msg, m.keyMap.MoveDirectoryItem):
m.directoryBeforeMove = m.filetree.GetSelectedItem().CurrentDirectory
m.state = showMoveState
m.filetree.SetDisabled(true)
if m.activePane == 0 && !m.filetree.CreatingNewDirectory && !m.filetree.CreatingNewFile {
m.directoryBeforeMove = m.filetree.GetSelectedItem().CurrentDirectory
m.state = showMoveState
m.filetree.SetDisabled(true)
}
case key.Matches(msg, m.keyMap.ShowTextInput):
if m.activePane == 0 && !m.filetree.CreatingNewDirectory && !m.filetree.CreatingNewFile {
m.showTextInput = true
Expand All @@ -73,13 +76,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.textinput.Reset()
}
case key.Matches(msg, m.keyMap.SubmitTextInput):
case key.Matches(msg, m.keyMap.Submit):
if m.filetree.CreatingNewFile {

Check failure on line 80 in internal/tui/update.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)
cmds = append(cmds, m.filetree.CreateFileCmd(m.textinput.Value()))
}

if m.filetree.CreatingNewDirectory {
} else if m.filetree.CreatingNewDirectory {
cmds = append(cmds, m.filetree.CreateDirectoryCmd(m.textinput.Value()))
} else {
cmds = append(
cmds,
m.filetree.MoveDirectoryItemCmd(
m.filetree.GetSelectedItem().Path,
m.secondaryFiletree.CurrentDirectory+"/"+m.filetree.GetSelectedItem().Name,
),
)
}

m.resetViewports()
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (m model) View() string {
}

return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.JoinHorizontal(lipgloss.Top, leftBox, rightBox),
lipgloss.NewStyle().Height(m.height).Render(lipgloss.JoinHorizontal(lipgloss.Top, leftBox, rightBox)),
m.statusbar.View(),
)
}

0 comments on commit f48f050

Please sign in to comment.