Skip to content

Commit

Permalink
create new search command
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Oct 23, 2021
1 parent 5378942 commit e30facc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
12 changes: 7 additions & 5 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,20 +454,22 @@ func GetDirectoryItemSize(path string) (int64, error) {
return size, err
}

func FindFilesByName(name, dir string) ([]fs.DirEntry, error) {
var files []fs.DirEntry
func FindFilesByName(name, dir string) ([]string, []fs.DirEntry, error) {
var paths []string
var entries []fs.DirEntry

err := filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return filepath.SkipDir
}

if entry.Name() == name {
files = append(files, entry)
if strings.Contains(entry.Name(), name) {
paths = append(paths, path)
entries = append(entries, entry)
}

return err
})

return files, err
return paths, entries, err
}
6 changes: 6 additions & 0 deletions internal/dirtree/dirtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// Model is a struct to represent the properties of a dirtree.
type Model struct {
Files []fs.DirEntry
FilePaths []string
Width int
Cursor int
ShowIcons bool
Expand All @@ -39,6 +40,11 @@ func (m *Model) SetContent(files []fs.DirEntry) {
m.Files = files
}

// SetFilePaths sets an array of file paths.
func (m *Model) SetFilePaths(filePaths []string) {
m.FilePaths = filePaths
}

// SetSize updates the size of the dirtree, useful when resizing the terminal.
func (m *Model) SetSize(width int) {
m.Width = width
Expand Down
30 changes: 16 additions & 14 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type errorMsg string
type directoryItemSizeMsg string
type copyToClipboardMsg string
type convertImageToStringMsg string
type findFilesByNameMsg struct {
paths []string
entries []fs.DirEntry
}
type readFileContentMsg struct {
rawContent string
markdown string
Expand Down Expand Up @@ -149,15 +153,15 @@ func (m Model) deleteFileCmd(name string) tea.Cmd {
}

// readFileContentCmd reads the content of a file and returns it.
func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
func (m Model) readFileContentCmd(fileName string, width int) tea.Cmd {
return func() tea.Msg {
content, err := dirfs.ReadFileContent(file.Name())
content, err := dirfs.ReadFileContent(fileName)
if err != nil {
return errorMsg(err.Error())
}

switch {
case filepath.Ext(file.Name()) == ".md" && m.appConfig.Settings.PrettyMarkdown:
case filepath.Ext(fileName) == ".md" && m.appConfig.Settings.PrettyMarkdown:
markdownContent, err := renderer.RenderMarkdown(width, content)
if err != nil {
return errorMsg(err.Error())
Expand All @@ -171,8 +175,8 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
pdfContent: "",
image: nil,
}
case filepath.Ext(file.Name()) == ".png" || filepath.Ext(file.Name()) == ".jpg" || filepath.Ext(file.Name()) == ".jpeg":
imageContent, err := os.Open(file.Name())
case filepath.Ext(fileName) == ".png" || filepath.Ext(fileName) == ".jpg" || filepath.Ext(fileName) == ".jpeg":
imageContent, err := os.Open(fileName)
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -192,8 +196,8 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
pdfContent: "",
image: img,
}
case filepath.Ext(file.Name()) == ".pdf":
pdfContent, err := renderer.ReadPdf(file.Name())
case filepath.Ext(fileName) == ".pdf":
pdfContent, err := renderer.ReadPdf(fileName)
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -207,7 +211,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
image: nil,
}
default:
code, err := renderer.Highlight(content, filepath.Ext(file.Name()), m.appConfig.Settings.SyntaxTheme)
code, err := renderer.Highlight(content, filepath.Ext(fileName), m.appConfig.Settings.SyntaxTheme)
if err != nil {
return errorMsg(err.Error())
}
Expand Down Expand Up @@ -371,16 +375,14 @@ func (m Model) getDirectoryListingByType(listType string, showHidden bool) tea.C
// findFilesByNameCmd finds files based on name.
func (m Model) findFilesByNameCmd(name string) tea.Cmd {
return func() tea.Msg {
workingDir, err := dirfs.GetWorkingDirectory()
paths, entries, err := dirfs.FindFilesByName(name, dirfs.RootDirectory)
if err != nil {
return errorMsg(err.Error())
}

directories, err := dirfs.FindFilesByName(name, workingDir)
if err != nil {
return errorMsg(err.Error())
return findFilesByNameMsg{
paths: paths,
entries: entries,
}

return updateDirectoryListingMsg(directories)
}
}
32 changes: 30 additions & 2 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

return m, nil

// findFilesByNameMsg is received when a file listing is to be found by name.
case findFilesByNameMsg:
m.showCommandInput = false
m.createFileMode = false
m.createDirectoryMode = false
m.renameMode = false
m.findMode = false

m.dirTree.GotoTop()
m.dirTree.SetContent(msg.entries)
m.dirTree.SetFilePaths(msg.paths)
m.primaryPane.SetContent(m.dirTree.View())
m.primaryPane.GotoTop()
m.statusBar.BlurCommandBar()
m.statusBar.ResetCommandBar()
m.updateStatusBarContent()

// tea.WindowSizeMsg is received whenever the window size changes.
case tea.WindowSizeMsg:
m.primaryPane.SetSize(msg.Width/2, msg.Height-m.statusBar.GetHeight())
Expand Down Expand Up @@ -306,6 +323,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.handleErrorCmd(err)
}

if len(m.dirTree.FilePaths) > 0 {
return m, m.updateDirectoryListingCmd(m.dirTree.FilePaths[m.dirTree.GetCursor()])
}

return m, m.updateDirectoryListingCmd(filepath.Join(currentDir, m.dirTree.GetSelectedFile().Name()))
case m.dirTree.GetSelectedFile().Mode()&os.ModeSymlink == os.ModeSymlink:
m.statusBar.SetItemSize("")
Expand All @@ -324,12 +345,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

return m, m.readFileContentCmd(
fileInfo,
fileInfo.Name(),
m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(),
)
default:
if len(m.dirTree.FilePaths) > 0 {
return m, m.readFileContentCmd(
m.dirTree.FilePaths[m.dirTree.GetCursor()],
m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(),
)
}

return m, m.readFileContentCmd(
m.dirTree.GetSelectedFile(),
m.dirTree.GetSelectedFile().Name(),
m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(),
)
}
Expand Down

0 comments on commit e30facc

Please sign in to comment.