Skip to content

Commit

Permalink
feat: tree state
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Mar 30, 2024
1 parent 1495df5 commit 4b7db0d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
19 changes: 13 additions & 6 deletions filetree/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/charmbracelet/lipgloss"

"github.com/mistakenelf/fm/filesystem"
"github.com/mistakenelf/fm/keys"
)

type treeState int

const (
IdleState treeState = iota
CreateFileState
CreateDirectoryState
MoveState
)

type DirectoryItem struct {
Expand All @@ -31,9 +41,7 @@ type Model struct {
showDirectoriesOnly bool
showFilesOnly bool
showIcons bool
CreatingNewFile bool
CreatingNewDirectory bool
keyMap KeyMap
keyMap keys.KeyMap
startDir string
StatusMessage string
selectionPath string
Expand All @@ -44,6 +52,7 @@ type Model struct {
inactiveItemColor lipgloss.AdaptiveColor
err error
CurrentDirectory string
State treeState
}

func New(startDir string) Model {
Expand All @@ -56,7 +65,7 @@ func New(startDir string) Model {
return Model{
Cursor: 0,
Disabled: false,
keyMap: DefaultKeyMap(),
keyMap: keys.DefaultKeyMap(),
min: 0,
max: 0,
startDir: startingDirectory,
Expand All @@ -69,7 +78,5 @@ func New(startDir string) Model {
unselectedItemColor: lipgloss.AdaptiveColor{Light: "ffffff", Dark: "#000000"},
inactiveItemColor: lipgloss.AdaptiveColor{Light: "243", Dark: "243"},
showIcons: true,
CreatingNewFile: false,
CreatingNewDirectory: false,
}
}
63 changes: 29 additions & 34 deletions filetree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case statusMessageTimeoutMsg:
m.StatusMessage = ""
case moveDirectoryItemMsg:
m.CreatingNewFile = false
m.CreatingNewDirectory = false
m.State = IdleState

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case copyToClipboardMsg:
Expand All @@ -43,13 +42,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
Bold(true).
Render(string(msg))))
case createFileMsg:
m.CreatingNewFile = false
m.CreatingNewDirectory = false
m.State = IdleState

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case createDirectoryMsg:
m.CreatingNewDirectory = false
m.CreatingNewFile = false
m.State = IdleState

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case getDirectoryListingMsg:
Expand All @@ -66,7 +63,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keyMap.Down):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -81,7 +78,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.max++
}
case key.Matches(msg, m.keyMap.Up):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -95,24 +92,24 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.min--
m.max--
}
case key.Matches(msg, m.keyMap.GoToTop):
if m.CreatingNewFile || m.CreatingNewDirectory {
case key.Matches(msg, m.keyMap.GotoTop):
if m.State != IdleState {
return m, nil
}

m.Cursor = 0
m.min = 0
m.max = m.height
case key.Matches(msg, m.keyMap.GoToBottom):
if m.CreatingNewFile || m.CreatingNewDirectory {
case key.Matches(msg, m.keyMap.GotoBottom):
if m.State != IdleState {
return m, nil
}

m.Cursor = len(m.files) - 1
m.min = len(m.files) - m.height
m.max = len(m.files) - 1
case key.Matches(msg, m.keyMap.PageDown):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -130,7 +127,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.min = m.max - m.height
}
case key.Matches(msg, m.keyMap.PageUp):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -148,49 +145,49 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.max = m.min + m.height
}
case key.Matches(msg, m.keyMap.GoToHomeDirectory):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

return m, m.GetDirectoryListingCmd(filesystem.HomeDirectory)
case key.Matches(msg, m.keyMap.GoToRootDirectory):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

return m, m.GetDirectoryListingCmd(filesystem.RootDirectory)
case key.Matches(msg, m.keyMap.ToggleHidden):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

m.showHidden = !m.showHidden

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case key.Matches(msg, m.keyMap.OpenDirectory):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

if m.files[m.Cursor].IsDirectory {
return m, m.GetDirectoryListingCmd(m.files[m.Cursor].Path)
}
case key.Matches(msg, m.keyMap.PreviousDirectory):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

return m, m.GetDirectoryListingCmd(
filepath.Dir(m.CurrentDirectory),
)
case key.Matches(msg, m.keyMap.CopyPathToClipboard):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

return m, copyToClipboardCmd(m.files[m.Cursor].Path)
case key.Matches(msg, m.keyMap.CopyDirectoryItem):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -199,7 +196,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.GetDirectoryListingCmd(m.CurrentDirectory),
)
case key.Matches(msg, m.keyMap.DeleteDirectoryItem):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -208,7 +205,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.GetDirectoryListingCmd(m.CurrentDirectory),
)
case key.Matches(msg, m.keyMap.ZipDirectoryItem):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -217,7 +214,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.GetDirectoryListingCmd(m.CurrentDirectory),
)
case key.Matches(msg, m.keyMap.UnzipDirectoryItem):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -226,7 +223,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.GetDirectoryListingCmd(m.CurrentDirectory),
)
case key.Matches(msg, m.keyMap.ShowDirectoriesOnly):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -235,7 +232,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case key.Matches(msg, m.keyMap.ShowFilesOnly):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -244,7 +241,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

return m, m.GetDirectoryListingCmd(m.CurrentDirectory)
case key.Matches(msg, m.keyMap.WriteSelectionPath):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

Expand All @@ -255,27 +252,25 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
)
}
case key.Matches(msg, m.keyMap.OpenInEditor):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

return m, openEditorCmd(m.files[m.Cursor].Name)
case key.Matches(msg, m.keyMap.CreateFile):
if m.CreatingNewFile || m.CreatingNewDirectory {
if m.State != IdleState {
return m, nil
}

m.CreatingNewFile = true
m.CreatingNewDirectory = false
m.State = CreateFileState

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

m.CreatingNewDirectory = true
m.CreatingNewFile = false
m.State = CreateDirectoryState

return m, nil
}
Expand Down
27 changes: 17 additions & 10 deletions internal/tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"

"github.com/mistakenelf/fm/filetree"
"github.com/mistakenelf/fm/statusbar"
)

Expand All @@ -27,16 +28,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.filetree.SetSize(halfSize, height-3)
m.secondaryFiletree.SetSize(halfSize, height-3)
m.help.SetSize(halfSize, height)
m.code.SetSize(halfSize, height)
m.pdf.SetSize(halfSize, height)
m.statusbar.SetSize(msg.Width)
m.help.SetSize(halfSize, height)

return m, tea.Batch(cmds...)
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keyMap.Quit):
case key.Matches(msg, m.keyMap.ForceQuit):
return m, tea.Quit
case key.Matches(msg, m.keyMap.Quit):
if m.filetree.State == filetree.IdleState {
return m, tea.Quit
}
case key.Matches(msg, m.keyMap.OpenFile):
if !m.showTextInput && m.activePane == 0 {
cmds = append(cmds, m.openFileCmd())
Expand All @@ -52,8 +57,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.resetViewports()
m.filetree.SetDisabled(false)
m.textinput.Blur()
m.filetree.CreatingNewDirectory = false
m.filetree.CreatingNewFile = false
m.filetree.State = filetree.IdleState
m.secondaryFiletree.SetDisabled(true)
m.activePane = 0

Expand All @@ -62,16 +66,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.textinput.Reset()
case key.Matches(msg, m.keyMap.MoveDirectoryItem):
if m.activePane == 0 && !m.filetree.CreatingNewDirectory && !m.filetree.CreatingNewFile {
if m.activePane == 0 && m.filetree.State == filetree.IdleState {
m.activePane = (m.activePane + 1) % 2
m.directoryBeforeMove = m.filetree.CurrentDirectory
m.state = showMoveState
m.filetree.State = filetree.MoveState
m.filetree.SetDisabled(true)
m.secondaryFiletree.SetDisabled(false)
cmds = append(cmds, m.secondaryFiletree.GetDirectoryListingCmd(m.filetree.CurrentDirectory))
}
case key.Matches(msg, m.keyMap.ShowTextInput):
if m.activePane == 0 && !m.filetree.CreatingNewDirectory && !m.filetree.CreatingNewFile {
if m.activePane == 0 && m.filetree.State == filetree.IdleState {
m.showTextInput = true
m.textinput.Focus()
m.disableAllViewports()
Expand All @@ -83,18 +88,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case key.Matches(msg, m.keyMap.Submit):
switch {
case m.filetree.CreatingNewFile:
case m.filetree.State == filetree.CreateFileState:
cmds = append(cmds, m.filetree.CreateFileCmd(m.textinput.Value()))
case m.filetree.CreatingNewDirectory:
case m.filetree.State == filetree.CreateDirectoryState:
cmds = append(cmds, m.filetree.CreateDirectoryCmd(m.textinput.Value()))
default:
case m.filetree.State == filetree.MoveState:
cmds = append(
cmds,
m.filetree.MoveDirectoryItemCmd(
m.filetree.GetSelectedItem().Path,
m.secondaryFiletree.CurrentDirectory+"/"+m.filetree.GetSelectedItem().Name,
),
)
default:
return m, nil
}

m.resetViewports()
Expand Down Expand Up @@ -150,7 +157,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

if m.filetree.CreatingNewDirectory || m.filetree.CreatingNewFile {
if m.filetree.State == filetree.CreateDirectoryState || m.filetree.State == filetree.CreateFileState {
m.textinput, cmd = m.textinput.Update(msg)
cmds = append(cmds, cmd)
}
Expand Down

0 comments on commit 4b7db0d

Please sign in to comment.