Skip to content

Commit

Permalink
Merge pull request #47 from knipferrc/feature/directory-preview
Browse files Browse the repository at this point in the history
Feature/directory preview
  • Loading branch information
mistakenelf authored Oct 15, 2021
2 parents 3c27489 + ea96fed commit 35fab37
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 26 deletions.
10 changes: 6 additions & 4 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CreateDirectory(name string) error {
}

// GetDirectoryListing returns a list of files and directories within a given directory.
func GetDirectoryListing(dir string, showHidden bool) ([]fs.DirEntry, error) {
func GetDirectoryListing(dir string, showHidden, changeWorkingDirectory bool) ([]fs.DirEntry, error) {
n := 0

// Read files from the directory.
Expand All @@ -50,9 +50,11 @@ func GetDirectoryListing(dir string, showHidden bool) ([]fs.DirEntry, error) {
}

// Update the apps directory to the directory currently being read.
err = os.Chdir(dir)
if err != nil {
return nil, err
if changeWorkingDirectory {
err = os.Chdir(dir)
if err != nil {
return nil, err
}
}

if !showHidden {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ require (
github.com/yuin/goldmark v1.4.1 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e // indirect
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 h1:qOfNqBm5gk93LjGZo1MJaKY6Bph39zOKz1Hz2ogHj1w=
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e h1:Xj+JO91noE97IN6F/7WZxzC5QE6yENAQPrwIYhW3bsA=
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -546,8 +546,8 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
Expand Down
17 changes: 15 additions & 2 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

type updateDirectoryListingMsg []fs.DirEntry
type previewDirectoryListingMsg []fs.DirEntry
type moveDirItemMsg []fs.DirEntry
type errorMsg string
type convertImageToStringMsg string
Expand All @@ -39,7 +40,7 @@ type readFileContentMsg struct {
// updateDirectoryListingCmd updates the directory listing based on the name of the directory provided.
func (m Model) updateDirectoryListingCmd(name string) tea.Cmd {
return func() tea.Msg {
files, err := dirfs.GetDirectoryListing(name, m.dirTree.ShowHidden)
files, err := dirfs.GetDirectoryListing(name, m.dirTree.ShowHidden, true)
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -48,6 +49,18 @@ func (m Model) updateDirectoryListingCmd(name string) tea.Cmd {
}
}

// previewDirectoryListingCmd updates the directory listing based on the name of the directory provided.
func (m Model) previewDirectoryListingCmd(name string) tea.Cmd {
return func() tea.Msg {
files, err := dirfs.GetDirectoryListing(name, m.dirTree.ShowHidden, false)
if err != nil {
return errorMsg(err.Error())
}

return previewDirectoryListingMsg(files)
}
}

// renameDirectoryItemCmd renames a file or directory based on the name and value provided.
func (m Model) renameDirectoryItemCmd(name, value string) tea.Cmd {
return func() tea.Msg {
Expand Down Expand Up @@ -79,7 +92,7 @@ func (m Model) moveDirectoryItemCmd(name string) tea.Cmd {
return errorMsg(err.Error())
}

files, err := dirfs.GetDirectoryListing(m.initialMoveDirectory, m.dirTree.ShowHidden)
files, err := dirfs.GetDirectoryListing(m.initialMoveDirectory, m.dirTree.ShowHidden, true)
if err != nil {
return errorMsg(err.Error())
}
Expand Down
7 changes: 7 additions & 0 deletions internal/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type keyMap struct {
CreateDirectory key.Binding
Rename key.Binding
EditFile key.Binding
PreviewDirectory key.Binding
}

// ShortHelp returns keybindings to be shown in the mini help view. It's part
Expand Down Expand Up @@ -56,6 +57,7 @@ func (k keyMap) ShortHelp() []key.Binding {
k.CreateDirectory,
k.Rename,
k.EditFile,
k.PreviewDirectory,
}
}

Expand Down Expand Up @@ -87,6 +89,7 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.CreateDirectory,
k.Rename,
k.EditFile,
k.PreviewDirectory,
},
}
}
Expand Down Expand Up @@ -186,5 +189,9 @@ func getDefaultKeyMap() keyMap {
key.WithKeys("E"),
key.WithHelp("E", "edit the currently selected file"),
),
PreviewDirectory: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "preview the currently selected directory"),
),
}
}
35 changes: 22 additions & 13 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Model struct {
secondaryPane pane.Model
loader spinner.Model
dirTree dirtree.Model
dirTreePreview dirtree.Model
statusBar statusbar.Model
colorimage colorimage.Model
markdown markdown.Model
Expand Down Expand Up @@ -75,6 +76,13 @@ func NewModel() Model {
theme.UnselectedTreeItemColor,
)

// Create a new dirtree for previews.
dirTreePreview := dirtree.NewModel(
cfg.Settings.ShowIcons,
theme.UnselectedTreeItemColor,
theme.UnselectedTreeItemColor,
)

// Initialize the primary pane as active and pass in some config values.
primaryPane := pane.NewModel(
true,
Expand Down Expand Up @@ -113,19 +121,20 @@ func NewModel() Model {
)

return Model{
keys: keys,
help: h,
primaryPane: primaryPane,
secondaryPane: secondaryPane,
loader: l,
dirTree: dirTree,
statusBar: statusBar,
colorimage: colorimage.Model{},
markdown: markdown.Model{},
sourcecode: sourcecode.Model{},
previousKey: tea.KeyMsg{},
itemToMove: nil,
appConfig: cfg,
keys: keys,
help: h,
primaryPane: primaryPane,
secondaryPane: secondaryPane,
loader: l,
dirTree: dirTree,
dirTreePreview: dirTreePreview,
statusBar: statusBar,
colorimage: colorimage.Model{},
markdown: markdown.Model{},
sourcecode: sourcecode.Model{},
previousKey: tea.KeyMsg{},
itemToMove: nil,
appConfig: cfg,
directoryItemSizeCtx: &directoryItemSizeCtx{
ctx: context.Background(),
},
Expand Down
42 changes: 41 additions & 1 deletion internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, m.getDirectoryItemSizeCmd(m.dirTree.GetSelectedFile().Name())

// previewDirectoryListingMsg shows a preview of a directory in the secondary pane.
case previewDirectoryListingMsg:
m.showCommandBar = false
m.inCreateFileMode = false
m.inCreateDirectoryMode = false
m.inRenameMode = false

m.markdown.SetContent("")
m.sourcecode.SetContent("")
m.colorimage.SetImage(nil)
m.dirTreePreview.GotoTop()
m.dirTreePreview.SetContent(msg)
m.secondaryPane.SetContent(m.dirTreePreview.View())
m.secondaryPane.GotoTop()
m.statusBar.BlurCommandBar()
m.statusBar.ResetCommandBar()

return m, nil

// A moveDirItemMsg is received any time a file or directory has been moved.
case moveDirItemMsg:
m.inMoveMode = false
Expand All @@ -99,18 +118,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.secondaryPane.GotoTop()
m.colorimage.SetImage(nil)
m.markdown.SetContent("")
m.dirTreePreview.SetContent(nil)
m.sourcecode.SetContent(msg.code)
m.secondaryPane.SetContent(m.sourcecode.View())
case msg.markdown != "":
m.secondaryPane.GotoTop()
m.colorimage.SetImage(nil)
m.sourcecode.SetContent("")
m.dirTreePreview.SetContent(nil)
m.markdown.SetContent(msg.markdown)
m.secondaryPane.SetContent(m.markdown.View())
case msg.image != nil:
m.secondaryPane.GotoTop()
m.markdown.SetContent("")
m.sourcecode.SetContent("")
m.dirTreePreview.SetContent(nil)
m.colorimage.SetImage(msg.image)
m.colorimage.SetContent(msg.imageString)
m.secondaryPane.SetContent(m.colorimage.View())
Expand Down Expand Up @@ -154,6 +176,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.primaryPane.SetSize(msg.Width/2, msg.Height-m.statusBar.GetHeight())
m.secondaryPane.SetSize(msg.Width/2, msg.Height-m.statusBar.GetHeight())
m.dirTree.SetSize(m.primaryPane.GetWidth())
m.dirTreePreview.SetSize(m.secondaryPane.GetWidth())
m.sourcecode.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.markdown.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.primaryPane.SetContent(m.dirTree.View())
Expand All @@ -169,7 +192,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.secondaryPane.SetContent(m.markdown.View())
case m.sourcecode.GetContent() != "":
m.secondaryPane.SetContent(m.sourcecode.View())
case m.sourcecode.GetContent() == "" && m.markdown.GetContent() == "" && m.colorimage.GetImage() == nil:
case m.dirTreePreview.GetTotalFiles() != 0:
m.secondaryPane.SetContent(m.dirTreePreview.View())
case m.sourcecode.GetContent() == "" && m.markdown.GetContent() == "" && m.colorimage.GetImage() == nil && m.dirTreePreview.GetTotalFiles() == 0:
m.secondaryPane.SetContent(lipgloss.NewStyle().
Width(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize()).
Render(m.help.View(m.keys)),
Expand Down Expand Up @@ -544,6 +569,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, m.updateDirectoryListingCmd(dirfs.CurrentDirectory)

case key.Matches(msg, m.keys.PreviewDirectory):
if !m.showCommandBar && m.primaryPane.GetIsActive() && m.dirTree.GetSelectedFile().IsDir() {
currentDir, err := dirfs.GetWorkingDirectory()
if err != nil {
return m, m.handleErrorCmd(err)
}

return m, m.previewDirectoryListingCmd(
fmt.Sprintf("%s/%s", currentDir, m.dirTree.GetSelectedFile().Name()),
)
}

return m, nil

// Reset FM to its initial state.
case key.Matches(msg, m.keys.Escape):
m.showCommandBar = false
Expand All @@ -567,6 +606,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.colorimage.SetImage(nil)
m.markdown.SetContent("")
m.sourcecode.SetContent("")
m.dirTreePreview.SetContent(nil)
m.updateStatusBarContent()
}

Expand Down

0 comments on commit 35fab37

Please sign in to comment.