Skip to content

Commit

Permalink
move commands to key handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Sep 26, 2021
1 parent b0f4f70 commit b3e8a85
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 100 deletions.
30 changes: 2 additions & 28 deletions internal/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package statusbar
import (
"fmt"
"io/fs"
"strings"

"github.com/knipferrc/fm/dirfs"
"github.com/knipferrc/fm/icons"
Expand Down Expand Up @@ -46,7 +45,8 @@ func NewModel(firstColumnColors, secondColumnColors, thirdColumnColors, fourthCo
input := textinput.NewModel()
input.Prompt = "❯ "
input.CharLimit = 250
input.Placeholder = "Enter a command"
input.Placeholder = "enter a name"
input.PlaceholderStyle.Background(secondColumnColors.Background)

s := spinner.NewModel()
s.Spinner = spinner.Dot
Expand All @@ -70,32 +70,6 @@ func NewModel(firstColumnColors, secondColumnColors, thirdColumnColors, fourthCo
}
}

// ParseCommand parses the command and returns the command name and the arguments.
func ParseCommand(command string) (string, string) {
// Split the command string into an array.
cmdString := strings.Split(command, " ")

// If theres only one item in the array, its a singular
// command such as rm.
if len(cmdString) == 1 {
cmdName := cmdString[0]

return cmdName, ""
}

// This command has two values, first one is the name
// of the command, other is the value to pass back
// to the UI to update.
if len(cmdString) == 2 {
cmdName := cmdString[0]
cmdValue := cmdString[1]

return cmdName, cmdValue
}

return "", ""
}

// GetHeight returns the height of the statusbar.
func (m Model) GetHeight() int {
return m.Height
Expand Down
35 changes: 28 additions & 7 deletions internal/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type keyMap struct {
Right key.Binding
GotoBottom key.Binding
Enter key.Binding
OpenCommandBar key.Binding
OpenHomeDirectory key.Binding
OpenPreviousDirectory key.Binding
ToggleHidden key.Binding
Expand All @@ -22,6 +21,10 @@ type keyMap struct {
Unzip key.Binding
Copy key.Binding
Escape key.Binding
Delete key.Binding
CreateFile key.Binding
CreateDirectory key.Binding
Rename key.Binding
}

// ShortHelp returns keybindings to be shown in the mini help view. It's part
Expand All @@ -36,7 +39,6 @@ func (k keyMap) ShortHelp() []key.Binding {
k.Right,
k.GotoBottom,
k.Enter,
k.OpenCommandBar,
k.OpenHomeDirectory,
k.OpenPreviousDirectory,
k.ToggleHidden,
Expand All @@ -46,6 +48,10 @@ func (k keyMap) ShortHelp() []key.Binding {
k.Unzip,
k.Copy,
k.Escape,
k.Delete,
k.CreateFile,
k.CreateDirectory,
k.Rename,
}
}

Expand All @@ -62,7 +68,6 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.Right,
k.GotoBottom,
k.Enter,
k.OpenCommandBar,
k.OpenHomeDirectory,
k.OpenPreviousDirectory,
k.ToggleHidden,
Expand All @@ -72,6 +77,10 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.Unzip,
k.Copy,
k.Escape,
k.Delete,
k.CreateFile,
k.CreateDirectory,
k.Rename,
},
}
}
Expand Down Expand Up @@ -111,10 +120,6 @@ func getDefaultKeyMap() keyMap {
key.WithKeys("enter"),
key.WithHelp("enter", "handle move mode and command parsing"),
),
OpenCommandBar: key.NewBinding(
key.WithKeys(":"),
key.WithHelp(":", "open command bar in the status bar"),
),
OpenHomeDirectory: key.NewBinding(
key.WithKeys("~"),
key.WithHelp("~", "go to home directory"),
Expand Down Expand Up @@ -151,5 +156,21 @@ func getDefaultKeyMap() keyMap {
key.WithKeys("esc"),
key.WithHelp("esc", "reset to initial state"),
),
Delete: key.NewBinding(
key.WithKeys("D"),
key.WithHelp("D", "delete the selected file or directory"),
),
CreateFile: key.NewBinding(
key.WithKeys("n"),
key.WithHelp("n", "create a new file"),
),
CreateDirectory: key.NewBinding(
key.WithKeys("N"),
key.WithHelp("N", "create a new directory"),
),
Rename: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "rename the currently selected file or directory"),
),
}
}
58 changes: 32 additions & 26 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ type directoryItemSizeCtx struct {

// Model represents the state of the UI.
type Model struct {
keys keyMap
help help.Model
primaryPane pane.Model
secondaryPane pane.Model
loader spinner.Model
dirTree dirtree.Model
statusBar statusbar.Model
colorimage colorimage.Model
markdown markdown.Model
sourcecode sourcecode.Model
previousKey tea.KeyMsg
itemToMove fs.DirEntry
appConfig config.Config
directoryItemSizeCtx *directoryItemSizeCtx
theme theme.Theme
previousDirectory string
initialMoveDirectory string
showCommandBar bool
inMoveMode bool
ready bool
keys keyMap
help help.Model
primaryPane pane.Model
secondaryPane pane.Model
loader spinner.Model
dirTree dirtree.Model
statusBar statusbar.Model
colorimage colorimage.Model
markdown markdown.Model
sourcecode sourcecode.Model
previousKey tea.KeyMsg
itemToMove fs.DirEntry
appConfig config.Config
directoryItemSizeCtx *directoryItemSizeCtx
theme theme.Theme
previousDirectory string
initialMoveDirectory string
showCommandBar bool
inMoveMode bool
inCreateFileMode bool
inCreateDirectoryMode bool
inRenameMode bool
ready bool
}

// NewModel create an instance of the entire application model.
Expand Down Expand Up @@ -126,11 +129,14 @@ func NewModel() Model {
directoryItemSizeCtx: &directoryItemSizeCtx{
ctx: context.Background(),
},
theme: theme,
previousDirectory: "",
initialMoveDirectory: "",
showCommandBar: false,
inMoveMode: false,
ready: false,
theme: theme,
previousDirectory: "",
initialMoveDirectory: "",
showCommandBar: false,
inMoveMode: false,
inCreateFileMode: false,
inCreateDirectoryMode: false,
inRenameMode: false,
ready: false,
}
}
100 changes: 61 additions & 39 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/knipferrc/fm/dirfs"
"github.com/knipferrc/fm/internal/statusbar"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -60,6 +59,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// file operations.
case updateDirectoryListingMsg:
m.showCommandBar = false
m.inCreateFileMode = false
m.inCreateDirectoryMode = false
m.inRenameMode = false

m.dirTree.GotoTop()
m.dirTree.SetContent(msg)
Expand Down Expand Up @@ -342,51 +344,34 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.secondaryPane.GotoBottom()

// If in move mode, pressing enter will place the selected item to move into
// the current directory. If the command bar is open, commands will be processed.
// If in move mode, place the selected item to move into
// the current directory. If the commandbar is open, process the command.
case key.Matches(msg, m.keys.Enter):
if m.inMoveMode {
switch {
case m.inMoveMode:
return m, m.moveDirectoryItem(m.itemToMove.Name())
}

// Parse the commands from the command bar, command is the name
// of the command and value is if the command requires input to it
// get its value, for example (rename test.txt) text.txt is the value.
command, value := statusbar.ParseCommand(m.statusBar.CommandBarValue())

// Nothing was input for a command.
if command == "" {
return m, nil
}

switch command {
// Exit FM.
case "exit", "q", "quit":
return m, tea.Quit

// Create a new directory based on the value passed.
case "mkdir":
case m.inCreateFileMode:
return m, tea.Sequentially(
m.createDir(value),
m.createFile(m.statusBar.CommandBarValue()),
m.updateDirectoryListing(dirfs.CurrentDirectory),
)

// Create a new file based on the value passed.
case "touch":
case m.inCreateDirectoryMode:
return m, tea.Sequentially(
m.createFile(value),
m.createDir(m.statusBar.CommandBarValue()),
m.updateDirectoryListing(dirfs.CurrentDirectory),
)

// Rename the currently selected file or folder based on the value passed.
case "mv", "rename":
case m.inRenameMode:
return m, tea.Sequentially(
m.renameFileOrDir(m.dirTree.GetSelectedFile().Name(), value),
m.renameFileOrDir(m.dirTree.GetSelectedFile().Name(), m.statusBar.CommandBarValue()),
m.updateDirectoryListing(dirfs.CurrentDirectory),
)
default:
return m, nil
}

// Delete the currently selected item.
case "rm", "delete":
// Delete the currently selected item.
case key.Matches(msg, m.keys.Delete):
if !m.showCommandBar && m.primaryPane.GetIsActive() {
if m.dirTree.GetSelectedFile().IsDir() {
return m, tea.Sequentially(
m.deleteDir(m.dirTree.GetSelectedFile().Name()),
Expand All @@ -398,14 +383,30 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.deleteFile(m.dirTree.GetSelectedFile().Name()),
m.updateDirectoryListing(dirfs.CurrentDirectory),
)
}

// Enter create file mode.
case key.Matches(msg, m.keys.CreateFile):
if !m.inMoveMode && !m.inCreateDirectoryMode && !m.showCommandBar {
m.inCreateFileMode = true
m.showCommandBar = true
m.statusBar.FocusCommandBar()
m.statusBar.SetContent(
m.dirTree.GetTotalFiles(),
m.dirTree.GetCursor(),
m.showCommandBar,
m.inMoveMode,
m.dirTree.GetSelectedFile(),
m.itemToMove,
)

default:
return m, nil
}

// Activate command bar if not in move mode.
case key.Matches(msg, m.keys.OpenCommandBar):
if !m.inMoveMode {
// Enter create directory mode.
case key.Matches(msg, m.keys.CreateDirectory):
if !m.inMoveMode && !m.inCreateFileMode && !m.showCommandBar {
m.inCreateDirectoryMode = true
m.showCommandBar = true
m.statusBar.FocusCommandBar()
m.statusBar.SetContent(
Expand All @@ -416,9 +417,27 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.dirTree.GetSelectedFile(),
m.itemToMove,
)

return m, nil
}

return m, cmd
// Enter create directory mode.
case key.Matches(msg, m.keys.Rename):
if !m.inMoveMode && !m.inCreateFileMode && !m.inCreateDirectoryMode && !m.showCommandBar {
m.inRenameMode = true
m.showCommandBar = true
m.statusBar.FocusCommandBar()
m.statusBar.SetContent(
m.dirTree.GetTotalFiles(),
m.dirTree.GetCursor(),
m.showCommandBar,
m.inMoveMode,
m.dirTree.GetSelectedFile(),
m.itemToMove,
)

return m, nil
}

// Shortcut to get back to the home directory if the
// command bar is not curently open.
Expand Down Expand Up @@ -510,6 +529,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.itemToMove = nil
m.initialMoveDirectory = ""
m.help.ShowAll = true
m.inCreateFileMode = false
m.inCreateDirectoryMode = false
m.inRenameMode = false
m.primaryPane.SetActive(true)
m.secondaryPane.SetActive(false)
m.statusBar.BlurCommandBar()
Expand Down

0 comments on commit b3e8a85

Please sign in to comment.