Skip to content

Commit

Permalink
Merge pull request #45 from koki-develop/preview-window
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Apr 13, 2023
2 parents 33bb954 + f390da7 commit 5171c26
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 70 deletions.
35 changes: 35 additions & 0 deletions docs/library/README.ja.md
Expand Up @@ -157,6 +157,7 @@ if err != nil {
- [インプットの位置](#インプットの位置)
- [インプットのプレースホルダ](#インプットのプレースホルダ)
- [カウントビュー](#カウントビュー)
- [プレビューウィンドウ](#プレビューウィンドウ)
- [スタイル](#スタイル)

#### プロンプト
Expand Down Expand Up @@ -251,6 +252,40 @@ if err != nil {

[Example](/examples/countview/)

#### プレビューウィンドウ

`Find()` メソッドに `fzf.PreviewWindow()` 渡すとプレビューウィンドウをレンダリングする関数を設定することができます。
関数の引数にはカーソル行のアイテムのインデックスとプレビューウィンドウのサイズが渡されます。

```go
files, err := os.ReadDir(".")
if err != nil {
// ...
}

f, err := fzf.New()
if err != nil {
// ...
}

idxs, err := f.Find(
files,
func(i int) string { return files[i].Name() },
fzf.WithPreviewWindow(func(i, width, height int) string {
info, _ := files[i].Info()
return fmt.Sprintf(
"Name: %s\nModTime: %s\nSize: %d bytes",
info.Name(), info.ModTime(), info.Size(),
)
}),
)
if err != nil {
// ...
}
```

[Example](/examples/preview-window/)

#### スタイル

`fzf.WithStyles()` を使用すると各コンポーネントのスタイルを設定することができます。
Expand Down
35 changes: 35 additions & 0 deletions docs/library/README.md
Expand Up @@ -158,6 +158,7 @@ if err != nil {
- [Position of input](#position-of-input)
- [Placeholder for input](#placeholder-for-input)
- [Count View](#count-view)
- [Preview Window](#preview-window)
- [Styles](#styles)

#### Prompt
Expand Down Expand Up @@ -253,6 +254,40 @@ if err != nil {

[Example](/examples/countview/)

#### Preview Window

You can set a function to render a preview window by passing `fzf.PreviewWindow()` to the `Find()` method.
The function argument is passed the index of the item at the cursor line and the size of the preview window.

```go
files, err := os.ReadDir(".")
if err != nil {
// ...
}

f, err := fzf.New()
if err != nil {
// ...
}

idxs, err := f.Find(
files,
func(i int) string { return files[i].Name() },
fzf.WithPreviewWindow(func(i, width, height int) string {
info, _ := files[i].Info()
return fmt.Sprintf(
"Name: %s\nModTime: %s\nSize: %d bytes",
info.Name(), info.ModTime(), info.Size(),
)
}),
)
if err != nil {
// ...
}
```

[Example](/examples/preview-window/)

#### Styles

`WithStyles()` can be used to set the style of each component.
Expand Down
1 change: 1 addition & 0 deletions examples/preview-window/README.md
@@ -0,0 +1 @@
![](./demo.gif)
40 changes: 40 additions & 0 deletions examples/preview-window/main.go
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"log"
"os"

"github.com/koki-develop/go-fzf"
)

func main() {
files, err := os.ReadDir(".")
if err != nil {
log.Fatal(err)
}

f, err := fzf.New()
if err != nil {
log.Fatal(err)
}

idxs, err := f.Find(
files,
func(i int) string { return files[i].Name() },
fzf.WithPreviewWindow(func(i, width, height int) string {
info, _ := files[i].Info()
return fmt.Sprintf(
"Name: %s\nModTime: %s\nSize: %d bytes",
info.Name(), info.ModTime(), info.Size(),
)
}),
)
if err != nil {
log.Fatal(err)
}

for _, i := range idxs {
fmt.Println(files[i].Name())
}
}
13 changes: 11 additions & 2 deletions fzf.go
Expand Up @@ -9,7 +9,8 @@ import (
)

var defaultFindOption = findOption{
itemPrefixFunc: nil,
itemPrefixFunc: nil,
previewWindowFunc: nil,
}

// Fuzzy Finder.
Expand Down Expand Up @@ -103,7 +104,8 @@ func (fzf *FZF) Abort() {
type FindOption func(o *findOption)

type findOption struct {
itemPrefixFunc func(i int) string
itemPrefixFunc func(i int) string
previewWindowFunc func(i, width, height int) string
}

// WithItemPrefix sets the prefix function of the item.
Expand All @@ -112,3 +114,10 @@ func WithItemPrefix(f func(i int) string) FindOption {
o.itemPrefixFunc = f
}
}

// WithPreviewWindow sets the preview window function of the item.
func WithPreviewWindow(f func(i, width, height int) string) FindOption {
return func(o *findOption) {
o.previewWindowFunc = f
}
}

0 comments on commit 5171c26

Please sign in to comment.