Skip to content

Commit

Permalink
Embed man page in the binary and show it on 'fzf --man'
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed May 20, 2024
1 parent 7b0c9e0 commit 5241df2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
_ "embed"
"errors"
"fmt"
"os"
"os/exec"
"strings"

fzf "github.com/junegunn/fzf/src"
Expand All @@ -28,6 +30,9 @@ var zshCompletion []byte
//go:embed shell/key-bindings.fish
var fishKeyBindings []byte

//go:embed man/man1/fzf.1
var manPage []byte

func printScript(label string, content []byte) {
fmt.Println("### " + label + " ###")
fmt.Println(strings.TrimSpace(string(content)))
Expand Down Expand Up @@ -76,6 +81,18 @@ func main() {
}
return
}
if options.Man {
file := fzf.WriteTemporaryFile([]string{string(manPage)}, "\n")
if len(file) == 0 {
exit(fzf.ExitError, errors.New("failed to display man page"))
return
}
defer os.Remove(file)
cmd := exec.Command("man", file)
cmd.Stdout = os.Stdout
cmd.Run()
return
}

code, err := fzf.Run(options)
exit(code, err)
Expand Down
2 changes: 1 addition & 1 deletion src/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"unsafe"
)

func writeTemporaryFile(data []string, printSep string) string {
func WriteTemporaryFile(data []string, printSep string) string {
f, err := os.CreateTemp("", "fzf-temp-*")
if err != nil {
// Unable to create temporary file
Expand Down
6 changes: 6 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ type Options struct {
Bash bool
Zsh bool
Fish bool
Man bool
Fuzzy bool
FuzzyAlgo algo.Algo
Scheme string
Expand Down Expand Up @@ -493,6 +494,7 @@ func defaultOptions() *Options {
Bash: false,
Zsh: false,
Fish: false,
Man: false,
Fuzzy: true,
FuzzyAlgo: algo.FuzzyMatchV2,
Scheme: "default",
Expand Down Expand Up @@ -1865,10 +1867,14 @@ func parseOptions(opts *Options, allArgs []string) error {
opts.Fish = false
opts.Help = false
opts.Version = false
opts.Man = false
}
for i := 0; i < len(allArgs); i++ {
arg := allArgs[i]
switch arg {
case "--man":
clearExitingOpts()
opts.Man = true
case "--bash":
clearExitingOpts()
opts.Bash = true
Expand Down
2 changes: 1 addition & 1 deletion src/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runProxy(commandPrefix string, cmdBuilder func(temp string) *exec.Cmd, opts
exports[idx] = fmt.Sprintf("export %s=%s", pair[0], escapeSingleQuote(pair[1]))
}
}
temp := writeTemporaryFile(append(exports, command), "\n")
temp := WriteTemporaryFile(append(exports, command), "\n")
defer os.Remove(temp)

cmd := cmdBuilder(temp)
Expand Down
2 changes: 1 addition & 1 deletion src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2840,7 +2840,7 @@ func replacePlaceholder(params replacePlaceholderParams) (string, []string) {
}

if flags.file {
file := writeTemporaryFile(replacements, params.printsep)
file := WriteTemporaryFile(replacements, params.printsep)
tempFiles = append(tempFiles, file)
return file
}
Expand Down

0 comments on commit 5241df2

Please sign in to comment.