Skip to content

Commit

Permalink
Embed shell extension in fzf binary (--bash & --zsh)
Browse files Browse the repository at this point in the history
This simplifies the distribution, and the users are less likely to have
problems caused by using incompatible scripts and binaries.

  # Set up bash shell extension
  source <(fzf --bash)

  # Set up zsh shell extension
  source <(fzf --zsh)
  • Loading branch information
junegunn committed Mar 12, 2024
1 parent c5b1970 commit c37b907
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ if [[ ! "\$PATH" == *$fzf_base_esc/bin* ]]; then
PATH="\${PATH:+\${PATH}:}$fzf_base/bin"
fi
EOF

if [[ $auto_completion -eq 1 ]] && [[ $key_bindings -eq 1 ]]; then
echo "source <(fzf --$shell)" >> "$src"
else
cat >> "$src" << EOF
# Auto-completion
# ---------------
$fzf_completion
Expand All @@ -270,6 +276,7 @@ $fzf_completion
# ------------
$fzf_key_bindings
EOF
fi
echo "OK"
done

Expand Down
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
package main

import (
_ "embed"
"fmt"

fzf "github.com/junegunn/fzf/src"
"github.com/junegunn/fzf/src/protector"
)

var version string = "0.47"
var revision string = "devel"

//go:embed shell/key-bindings.bash
var bashKeyBindings []byte

//go:embed shell/completion.bash
var bashCompletion []byte

//go:embed shell/key-bindings.zsh
var zshKeyBindings []byte

//go:embed shell/completion.zsh
var zshCompletion []byte

func main() {
protector.Protect()
fzf.Run(fzf.ParseOptions(), version, revision)
options := fzf.ParseOptions()
if options.Bash {
fmt.Println(string(bashKeyBindings))
fmt.Println(string(bashCompletion))
return
}
if options.Zsh {
fmt.Println(string(zshKeyBindings))
fmt.Println(string(zshCompletion))
return
}
fzf.Run(options, version, revision)
}
14 changes: 14 additions & 0 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,20 @@ Display version information and exit
.TP
Note that most options have the opposite versions with \fB--no-\fR prefix.

.SS Shell extension
.TP
.B "--bash"
Print script to set up Bash shell extension

e.g. \fBsource <(fzf --bash)\fR

.TP
.B "--zsh"
Print script to set up Zsh shell extension

e.g. \fBsource <(fzf --zsh)\fR


.SH ENVIRONMENT VARIABLES
.TP
.B FZF_DEFAULT_COMMAND
Expand Down
18 changes: 18 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const usage = `usage: fzf [options]
(To allow remote process execution, use --listen-unsafe)
--version Display version information and exit
Shell extension
--bash Print script to set up Bash shell extension
--zsh Print script to set up Zsh shell extension
Environment variables
FZF_DEFAULT_COMMAND Default command to use when input is tty
FZF_DEFAULT_OPTS Default options (e.g. '--layout=reverse --info=inline')
Expand Down Expand Up @@ -276,6 +280,8 @@ func firstLine(s string) string {

// Options stores the values of command-line options
type Options struct {
Bash bool
Zsh bool
Fuzzy bool
FuzzyAlgo algo.Algo
Scheme string
Expand Down Expand Up @@ -351,6 +357,8 @@ func defaultPreviewOpts(command string) previewOpts {

func defaultOptions() *Options {
return &Options{
Bash: false,
Zsh: false,
Fuzzy: true,
FuzzyAlgo: algo.FuzzyMatchV2,
Scheme: "default",
Expand Down Expand Up @@ -1602,6 +1610,16 @@ func parseOptions(opts *Options, allArgs []string) {
for i := 0; i < len(allArgs); i++ {
arg := allArgs[i]
switch arg {
case "--bash":
opts.Bash = true
if opts.Zsh {
errorExit("cannot specify both --bash and --zsh")
}
case "--zsh":
opts.Zsh = true
if opts.Bash {
errorExit("cannot specify both --bash and --zsh")
}
case "-h", "--help":
help(exitOk)
case "-x", "--extended":
Expand Down

0 comments on commit c37b907

Please sign in to comment.