Support deleting files and folders
This commit is contained in:
parent
5350889e5b
commit
77f327b10a
|
@ -11,21 +11,22 @@ ADB file manager
|
|||
- Download
|
||||
- Upload
|
||||
- Rename
|
||||
- Delete
|
||||
- New folder
|
||||
|
||||
## Download
|
||||
|
||||
```bash
|
||||
go get gitlab.com/tslocum/adbfm/cmd/adbfm
|
||||
go get gitlab.com/tslocum/adbfm
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
adbfm may be used with a keyboard or mouse.
|
||||
|
||||
- Navigation: Arrow keys or VIM-keys (H/J/K/L), Tab, Shift+Tab
|
||||
- Navigate: Arrow keys or VIM keys (H/J/K/L), Tab, Shift+Tab
|
||||
- Open context menu: Alt+Enter
|
||||
- Enter directory: Enter
|
||||
- Select file or option: Enter
|
||||
|
||||
## Support
|
||||
|
||||
|
|
16
adb.go
16
adb.go
|
@ -172,6 +172,22 @@ func (a *adbConn) move(old string, new string) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (a *adbConn) delete(path string) (err error) {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
|
||||
if a.device == nil {
|
||||
return errors.New("no device")
|
||||
}
|
||||
|
||||
_, err = a.device.RunCommand("rm", "-rf", path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete file or directory: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *adbConn) newDirectory(path string) (err error) {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
|
|
84
gui.go
84
gui.go
|
@ -32,6 +32,8 @@ var (
|
|||
renameField *cview.InputField
|
||||
renameItem int
|
||||
|
||||
deleteItem int
|
||||
|
||||
newFolderField *cview.InputField
|
||||
newFolderItem int
|
||||
|
||||
|
@ -44,7 +46,8 @@ const contextMenuTitleWidth = 16
|
|||
const (
|
||||
modeNormal = 0
|
||||
modeRename = 1
|
||||
modeNewFolder = 2
|
||||
modeDelete = 2
|
||||
modeNewFolder = 3
|
||||
)
|
||||
|
||||
func getMode() int {
|
||||
|
@ -57,7 +60,7 @@ func setMode(mode int) {
|
|||
switch currentMode {
|
||||
case modeNormal:
|
||||
appContainer.HidePanel("modal")
|
||||
case modeRename, modeNewFolder:
|
||||
case modeRename, modeDelete, modeNewFolder:
|
||||
appContainer.ShowPanel("modal")
|
||||
}
|
||||
|
||||
|
@ -107,6 +110,45 @@ func acceptNewFolder(buttonIndex int, buttonLabel string) {
|
|||
app.Draw()
|
||||
}
|
||||
|
||||
func cancelDelete() {
|
||||
setMode(modeNormal)
|
||||
}
|
||||
|
||||
func confirmDelete() {
|
||||
err := bridge.delete(path.Join(remotePath, remoteEntriesShown[deleteItem].Name))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
setMode(modeNormal)
|
||||
focusUpdated()
|
||||
|
||||
browseRemote(remotePath)
|
||||
app.Draw()
|
||||
}
|
||||
|
||||
func previousField(ev *tcell.EventKey) *tcell.EventKey {
|
||||
if currentMode == modeNormal && currentFocus > 0 {
|
||||
currentFocus--
|
||||
focusUpdated()
|
||||
return nil
|
||||
}
|
||||
return ev
|
||||
}
|
||||
|
||||
func nextField(ev *tcell.EventKey) *tcell.EventKey {
|
||||
if currentMode == modeNormal && currentFocus < 2 {
|
||||
currentFocus++
|
||||
focusUpdated()
|
||||
return nil
|
||||
}
|
||||
return ev
|
||||
}
|
||||
|
||||
func exit(_ *tcell.EventKey) *tcell.EventKey {
|
||||
return nil
|
||||
}
|
||||
|
||||
func initTUI() error {
|
||||
devices, err := bridge.listDevices()
|
||||
if err != nil {
|
||||
|
@ -149,6 +191,10 @@ func initTUI() error {
|
|||
localBuffer.SetTitleAlign(cview.AlignLeft)
|
||||
localBuffer.SetBorder(true)
|
||||
localBuffer.AddContextItem("Upload", 'k', func(index int) {
|
||||
if localEntriesShown[index].Name == ".." {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO Exists confirmation dialog
|
||||
|
||||
modalPopup.SetText(fmt.Sprintf("Uploading %s", localEntriesShown[index].Name))
|
||||
|
@ -259,6 +305,9 @@ func initTUI() error {
|
|||
remoteBuffer.SetBorder(true)
|
||||
remoteBuffer.AddContextItem("Download", 'j', func(index int) {
|
||||
// TODO Exists confirmation dialog
|
||||
if remoteEntriesShown[index].Name == ".." {
|
||||
return
|
||||
}
|
||||
|
||||
modalPopup.SetText(fmt.Sprintf("Downloading %s", remoteEntriesShown[index].Name))
|
||||
modalPopup.SetDoneFunc(acceptRename)
|
||||
|
@ -293,12 +342,27 @@ func initTUI() error {
|
|||
})
|
||||
remoteBuffer.AddContextItem("", 0, nil)
|
||||
remoteBuffer.AddContextItem("Delete", 'd', func(index int) {
|
||||
if remoteEntriesShown[index].Name == ".." {
|
||||
return
|
||||
}
|
||||
|
||||
if remoteEntriesShown[index].Mode&os.ModeDir != 0 {
|
||||
modalPopup.SetText("Are you sure you want to delete this directory?")
|
||||
} else {
|
||||
modalPopup.SetText("Are you sure you want to delete this file?")
|
||||
}
|
||||
modalPopup.GetForm().Clear(true)
|
||||
modalPopup.GetForm().AddButton("No", cancelDelete)
|
||||
modalPopup.GetForm().AddButton("Yes", confirmDelete)
|
||||
|
||||
deleteItem = index
|
||||
setMode(modeDelete)
|
||||
})
|
||||
remoteBuffer.AddContextItem("", 0, nil)
|
||||
remoteBuffer.AddContextItem("Rename", 'r', func(index int) {
|
||||
renameItem = index
|
||||
setMode(modeRename)
|
||||
if remoteEntriesShown[index].Name == ".." {
|
||||
return
|
||||
}
|
||||
|
||||
renameField.SetText(remoteEntriesShown[index].Name)
|
||||
|
||||
|
@ -311,14 +375,14 @@ func initTUI() error {
|
|||
acceptRename(0, "")
|
||||
})
|
||||
|
||||
renameItem = index
|
||||
setMode(modeRename)
|
||||
|
||||
focusUpdated()
|
||||
app.Draw()
|
||||
})
|
||||
remoteBuffer.AddContextItem("", 0, nil)
|
||||
remoteBuffer.AddContextItem("New folder", 'f', func(index int) {
|
||||
newFolderItem = index
|
||||
setMode(modeNewFolder)
|
||||
|
||||
newFolderField.SetLabel("Folder name")
|
||||
|
||||
modalPopup.SetDoneFunc(acceptNewFolder)
|
||||
|
@ -330,6 +394,9 @@ func initTUI() error {
|
|||
acceptNewFolder(0, "")
|
||||
})
|
||||
|
||||
newFolderItem = index
|
||||
setMode(modeNewFolder)
|
||||
|
||||
focusUpdated()
|
||||
app.Draw()
|
||||
})
|
||||
|
@ -404,6 +471,9 @@ func focusUpdated() {
|
|||
case modeNewFolder:
|
||||
app.SetFocus(newFolderField)
|
||||
return
|
||||
case modeDelete:
|
||||
app.SetFocus(modalPopup)
|
||||
return
|
||||
}
|
||||
|
||||
switch currentFocus {
|
||||
|
|
97
gui_key.go
97
gui_key.go
|
@ -1,97 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"gitlab.com/tslocum/cview"
|
||||
)
|
||||
|
||||
func selectItem(ev *tcell.EventKey) *tcell.EventKey {
|
||||
if currentFocus == 0 {
|
||||
// Select device
|
||||
} else if currentFocus == 1 {
|
||||
localLock.Lock()
|
||||
defer localLock.Unlock()
|
||||
|
||||
if localBuffer.GetItemCount() > 0 {
|
||||
currentItem := localBuffer.GetCurrentItemIndex()
|
||||
if currentItem >= 0 && currentItem < len(localEntriesShown) {
|
||||
newPath := localEntriesShown[currentItem].Name
|
||||
if localEntriesShown[currentItem].Mode&os.ModeDir != 0 {
|
||||
go browseLocal(path.Join(localPath, newPath))
|
||||
return nil
|
||||
}
|
||||
|
||||
align := cview.AlignCenter
|
||||
if runewidth.StringWidth(localEntriesShown[currentItem].Name) > contextMenuTitleWidth {
|
||||
align = cview.AlignLeft
|
||||
}
|
||||
localBuffer.ContextMenuList().SetTitleAlign(align)
|
||||
localBuffer.ContextMenuList().SetTitle(localEntriesShown[currentItem].Name)
|
||||
|
||||
localBuffer.ShowContextMenu(currentItem, -1, -1, func(primitive cview.Primitive) {
|
||||
app.SetFocus(primitive)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ev
|
||||
} else { // == 2
|
||||
remoteLock.Lock()
|
||||
defer remoteLock.Unlock()
|
||||
|
||||
if remoteBuffer.GetItemCount() > 0 {
|
||||
currentItem := remoteBuffer.GetCurrentItemIndex()
|
||||
if currentItem >= 0 && currentItem < len(remoteEntriesShown) {
|
||||
newPath := remoteEntriesShown[currentItem].Name
|
||||
if remoteEntriesShown[currentItem].Mode&os.ModeDir != 0 {
|
||||
go browseRemote(path.Join(remotePath, newPath))
|
||||
return nil
|
||||
}
|
||||
|
||||
align := cview.AlignCenter
|
||||
if runewidth.StringWidth(remoteEntriesShown[currentItem].Name) > contextMenuTitleWidth {
|
||||
align = cview.AlignLeft
|
||||
}
|
||||
remoteBuffer.ContextMenuList().SetTitleAlign(align)
|
||||
remoteBuffer.ContextMenuList().SetTitle(remoteEntriesShown[currentItem].Name)
|
||||
|
||||
remoteBuffer.ShowContextMenu(currentItem, -1, -1, func(primitive cview.Primitive) {
|
||||
app.SetFocus(primitive)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ev
|
||||
}
|
||||
|
||||
return ev
|
||||
|
||||
}
|
||||
|
||||
func previousField(_ *tcell.EventKey) *tcell.EventKey {
|
||||
if currentFocus > 0 {
|
||||
currentFocus--
|
||||
}
|
||||
|
||||
focusUpdated()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func nextField(_ *tcell.EventKey) *tcell.EventKey {
|
||||
if currentFocus < 2 {
|
||||
currentFocus++
|
||||
}
|
||||
|
||||
focusUpdated()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func exit(_ *tcell.EventKey) *tcell.EventKey {
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue