Skip to content
Myr

main.go

This file

Path

main.go

115 lines · 2.3 KiB · Go

Last changed by

0.74.0Junegunn Choibefore adoption6765f46

History of this file

Take it

1package main23import (4	_ "embed"5	"fmt"6	"os"7	"os/exec"8	"strings"910	fzf "github.com/junegunn/fzf/src"11	"github.com/junegunn/fzf/src/protector"12)1314var version = "0.74"15var revision = "devel"1617//go:embed shell/key-bindings.bash18var bashKeyBindings []byte1920//go:embed shell/completion.bash21var bashCompletion []byte2223//go:embed shell/key-bindings.zsh24var zshKeyBindings []byte2526//go:embed shell/completion.zsh27var zshCompletion []byte2829//go:embed shell/key-bindings.fish30var fishKeyBindings []byte3132//go:embed shell/key-bindings.nu33var nushellKeyBindings []byte3435//go:embed shell/completion.nu36var nushellCompletion []byte3738//go:embed shell/completion.fish39var fishCompletion []byte4041//go:embed man/man1/fzf.142var manPage []byte4344func printScript(label string, content []byte) {45	fmt.Println("### " + label + " ###")46	fmt.Println(strings.TrimSpace(string(content)))47	fmt.Println("### end: " + label + " ###")48}4950func exit(code int, err error) {51	if code == fzf.ExitError && err != nil {52		fmt.Fprintln(os.Stderr, err.Error())53	}54	os.Exit(code)55}5657func main() {58	protector.Protect()5960	options, err := fzf.ParseOptions(true, os.Args[1:])61	if err != nil {62		exit(fzf.ExitError, err)63		return64	}65	if options.Bash {66		printScript("key-bindings.bash", bashKeyBindings)67		printScript("completion.bash", bashCompletion)68		return69	}70	if options.Zsh {71		printScript("key-bindings.zsh", zshKeyBindings)72		printScript("completion.zsh", zshCompletion)73		return74	}75	if options.Fish {76		printScript("key-bindings.fish", fishKeyBindings)77		printScript("completion.fish", fishCompletion)78		return79	}80	if options.Nushell {81		printScript("key-bindings.nu", nushellKeyBindings)82		printScript("completion.nu", nushellCompletion)83		return84	}85	if options.Help {86		fmt.Print(fzf.Usage)87		return88	}89	if options.Version {90		if len(revision) > 0 {91			fmt.Printf("%s (%s)\n", version, revision)92		} else {93			fmt.Println(version)94		}95		return96	}97	if options.Man {98		file := fzf.WriteTemporaryFile([]string{string(manPage)}, "\n")99		if len(file) == 0 {100			fmt.Print(string(manPage))101			return102		}103		defer os.Remove(file)104		cmd := exec.Command("man", file)105		cmd.Stdin = os.Stdin106		cmd.Stdout = os.Stdout107		if err := cmd.Run(); err != nil {108			fmt.Print(string(manPage))109		}110		return111	}112113	code, err := fzf.Run(options)114	exit(code, err)115}