Skip to content

Commit

Permalink
Merge pull request #64 from knipferrc/feature/search
Browse files Browse the repository at this point in the history
Feature/search
  • Loading branch information
mistakenelf authored Nov 7, 2021
2 parents 88f674b + 3c77e0f commit 26ddb96
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 51 deletions.
72 changes: 37 additions & 35 deletions README.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,26 @@ func GetDirectoryItemSize(path string) (int64, error) {
return size, err
}

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 strings.Contains(entry.Name(), name) {
paths = append(paths, path)
entries = append(entries, entry)
}

return err
})

return paths, entries, err
}

// WriteToFile writes content to a file, overwriting content if it exists.
func WriteToFile(path, content string) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
golang.org/x/exp v0.0.0-20211105205138-14c72366447f // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/mobile v0.0.0-20211103151657-e68c98865fb2 // indirect
golang.org/x/net v0.0.0-20211104170005-ce137452f963 // indirect
golang.org/x/net v0.0.0-20211105192438-b53810dc28af // indirect
golang.org/x/sys v0.0.0-20211106132015-ebca88c72f68 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211104170005-ce137452f963 h1:8gJUadZl+kWvZBqG/LautX0X6qe5qTC2VI/3V3NBRAY=
golang.org/x/net v0.0.0-20211104170005-ce137452f963/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211105192438-b53810dc28af h1:SMeNJG/vclJ5wyBBd4xupMsSJIHTd1coW9g7q6KOjmY=
golang.org/x/net v0.0.0-20211105192438-b53810dc28af/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
11 changes: 11 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,16 @@ 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
}

// GetFilePaths returns an array of file paths.
func (m Model) GetFilePaths() []string {
return m.FilePaths
}

// SetSize updates the size of the dirtree, useful when resizing the terminal.
func (m *Model) SetSize(width int) {
m.Width = width
Expand Down
38 changes: 37 additions & 1 deletion internal/pane/pane.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package pane

import (
"fmt"

"github.com/knipferrc/fm/strfmt"

"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

Expand All @@ -14,17 +18,23 @@ type Model struct {
IsActive bool
Borderless bool
AlternateBorder bool
ShowLoading bool
ActiveBorderColor lipgloss.AdaptiveColor
InactiveBorderColor lipgloss.AdaptiveColor
Spinner spinner.Model
}

// NewModel creates an instance of a pane.
func NewModel(isActive, borderless bool, activeBorderColor, inactiveBorderColor lipgloss.AdaptiveColor) Model {
s := spinner.NewModel()
s.Spinner = spinner.Dot

return Model{
IsActive: isActive,
Borderless: borderless,
ActiveBorderColor: activeBorderColor,
InactiveBorderColor: inactiveBorderColor,
Spinner: s,
}
}

Expand Down Expand Up @@ -107,6 +117,25 @@ func (m Model) GetYOffset() int {
return m.Viewport.YOffset
}

// ShowSpinner determines wether to show the spinner or not.
func (m *Model) ShowSpinner(show bool) {
m.ShowLoading = show
}

// Update updates the pane.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd

switch msg := msg.(type) {
case spinner.TickMsg:
m.Spinner, cmd = m.Spinner.Update(msg)
cmds = append(cmds, cmd)
}

return m, tea.Batch(cmds...)
}

// View returns a string representation of the pane.
func (m Model) View() string {
borderColor := m.InactiveBorderColor
Expand Down Expand Up @@ -135,11 +164,18 @@ func (m Model) View() string {
borderColor = m.ActiveBorderColor
}

content := m.Viewport.View()

if m.ShowLoading {
content = fmt.Sprintf("%s%s", m.Spinner.View(), "loading...")
}

return m.Style.Copy().
BorderForeground(borderColor).
PaddingLeft(padding).
PaddingRight(padding).
Border(border).
Width(m.Viewport.Width).
Render(strfmt.ConvertTabsToSpaces(m.Viewport.View()))
Height(m.Viewport.Height).
Render(strfmt.ConvertTabsToSpaces(content))
}
8 changes: 7 additions & 1 deletion internal/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Model struct {
ShowCommandBar bool
InMoveMode bool
ItemSize string
FilePaths []string
SelectedFile os.FileInfo
ItemToMove os.FileInfo
FirstColumnColors Color
Expand Down Expand Up @@ -101,13 +102,14 @@ func (m *Model) FocusCommandBar() {
}

// SetContent sets the content of the statusbar.
func (m *Model) SetContent(totalFiles, cursor int, showCommandBar, inMoveMode bool, selectedFile, itemToMove os.FileInfo) {
func (m *Model) SetContent(totalFiles, cursor int, showCommandBar, inMoveMode bool, selectedFile, itemToMove os.FileInfo, filePaths []string) {
m.TotalFiles = totalFiles
m.Cursor = cursor
m.ShowCommandBar = showCommandBar
m.InMoveMode = inMoveMode
m.SelectedFile = selectedFile
m.ItemToMove = itemToMove
m.FilePaths = filePaths
}

// SetItemSize sets the size of the currently selected directory item as a formatted size string.
Expand Down Expand Up @@ -162,6 +164,10 @@ func (m Model) View() string {
fileSize = m.ItemSize
}

if len(m.FilePaths) > 0 {
currentPath = m.FilePaths[m.Cursor]
}

// Display some information about the currently seleted file including
// its size, the mode and the current path.
status = fmt.Sprintf("%s %s %s",
Expand Down
40 changes: 32 additions & 8 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -146,15 +150,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 @@ -168,8 +172,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 @@ -189,8 +193,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 @@ -204,7 +208,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 @@ -365,6 +369,26 @@ func (m Model) getDirectoryListingByTypeCmd(listType string, showHidden bool) te
}
}

// findFilesByNameCmd finds files based on name.
func (m Model) findFilesByNameCmd(name string) tea.Cmd {
return func() tea.Msg {
workingDir, err := dirfs.GetWorkingDirectory()
if err != nil {
return errorMsg(err.Error())
}

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

return findFilesByNameMsg{
paths: paths,
entries: entries,
}
}
}

// writeSelectionPathCmd writes content to the file specified.
func (m Model) writeSelectionPathCmd(selectionPath, filePath string) tea.Cmd {
return func() tea.Msg {
Expand Down
7 changes: 7 additions & 0 deletions internal/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type keyMap struct {
CopyToClipboard key.Binding
ShowOnlyDirectories key.Binding
ShowOnlyFiles key.Binding
Find key.Binding
}

// ShortHelp returns keybindings to be shown in the mini help view. It's part
Expand Down Expand Up @@ -66,6 +67,7 @@ func (k keyMap) ShortHelp() []key.Binding {
k.CopyToClipboard,
k.ShowOnlyDirectories,
k.ShowOnlyFiles,
k.Find,
}
}

Expand Down Expand Up @@ -102,6 +104,7 @@ func (k keyMap) FullHelp() [][]key.Binding {
k.CopyToClipboard,
k.ShowOnlyDirectories,
k.ShowOnlyFiles,
k.Find,
},
}
}
Expand Down Expand Up @@ -221,5 +224,9 @@ func getDefaultKeyMap() keyMap {
key.WithKeys("s"),
key.WithHelp("s", "only show files in the directory tree"),
),
Find: key.NewBinding(
key.WithKeys("ctrl+f"),
key.WithHelp("ctrl+f", "find files by name"),
),
}
}
2 changes: 2 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Model struct {
createFileMode bool
createDirectoryMode bool
renameMode bool
findMode bool
showHidden bool
showDirectoriesOnly bool
showFilesOnly bool
Expand Down Expand Up @@ -140,6 +141,7 @@ func NewModel() Model {
createFileMode: false,
createDirectoryMode: false,
renameMode: false,
findMode: false,
showHidden: true,
showDirectoriesOnly: false,
showFilesOnly: false,
Expand Down
Loading

0 comments on commit 26ddb96

Please sign in to comment.