diff --git a/.gitignore b/.gitignore index c74464e..1f661b6 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,9 @@ pkg vendor/ pet dist/ + +# Tools and nix +flake.nix +flake.lock +.direnv +.envrc diff --git a/README.md b/README.md index 3599d0a..5b0afa7 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,8 @@ Run `pet configure` backend = "gist" # specify backend service to sync snippets (gist, ghe or gitlab, default: gist) sortby = "description" # specify how snippets get sorted (recency (default), -recency, description, -description, command, -command, output, -output) cmd = ["sh", "-c"] # specify the command to execute the snippet with + color = false # enables output coloring with fzf, same as '--color' flag + format = "[$descripion]: $command $tags" controls the format of the output when searching [Gist] file_name = "pet-snippet.toml" # specify gist file name diff --git a/cmd/util.go b/cmd/util.go index aa013db..8c21e82 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -46,18 +46,27 @@ func filter(options []string, tag string) (commands []string, err error) { if strings.ContainsAny(command, "\n") { command = strings.Replace(command, "\n", "\\n", -1) } - t := fmt.Sprintf("[%s]: %s", s.Description, command) tags := "" for _, tag := range s.Tag { - tags += fmt.Sprintf(" #%s", tag) + tags += fmt.Sprintf("#%s ", tag) } - t += tags + + format := "[$description]: $command $tags" + if config.Conf.General.Format != "" { + format = config.Conf.General.Format + } + + t := strings.Replace(format, "$command", command, 1) + t = strings.Replace(t, "$description", s.Description, 1) + t = strings.Replace(t, "$tags", tags, 1) snippetTexts[t] = s if config.Flag.Color || config.Conf.General.Color { - t = fmt.Sprintf("[%s]: %s%s", - color.HiRedString(s.Description), command, color.HiCyanString(tags)) + t = config.Conf.General.Format + t = strings.Replace(format, "$command", command, 1) + t = strings.Replace(t, "$description", color.HiRedString(s.Description), 1) + t = strings.Replace(t, "$tags", color.HiCyanString(tags), 1) } text += t + "\n" } diff --git a/config/config.go b/config/config.go index 0ef553f..96ce032 100644 --- a/config/config.go +++ b/config/config.go @@ -31,6 +31,7 @@ type GeneralConfig struct { Backend string SortBy string Color bool + Format string Cmd []string } @@ -125,6 +126,7 @@ func (cfg *Config) Load(file string) error { cfg.General.SelectCmd = "fzf --ansi --layout=reverse --border --height=90% --pointer=* --cycle --prompt=Snippets:" cfg.General.Backend = "gist" cfg.General.Color = false + cfg.General.Format = "[$description]: $command $tags" cfg.Gist.FileName = "pet-snippet.toml" diff --git a/dialog/view.go b/dialog/view.go index ff75f11..25c44a4 100644 --- a/dialog/view.go +++ b/dialog/view.go @@ -18,58 +18,58 @@ var ( // createView sets up a new view with the given parameters. func createView(g *gocui.Gui, name string, coords []int, editable bool) (*gocui.View, error) { - if StringInSlice(name, views) { - return nil, nil - } + if StringInSlice(name, views) { + return nil, nil + } - v, err := g.SetView(name, coords[0], coords[1], coords[2], coords[3], 0) - if err != nil && err != gocui.ErrUnknownView { - return nil, err - } + v, err := g.SetView(name, coords[0], coords[1], coords[2], coords[3], 0) + if err != nil && err != gocui.ErrUnknownView { + return nil, err + } - v.Title = name - v.Wrap = true - v.Autoscroll = true - v.Editable = editable + v.Title = name + v.Wrap = true + v.Autoscroll = true + v.Editable = editable - views = append(views, name) + views = append(views, name) - return v, nil + return v, nil } func generateSingleParameterView(g *gocui.Gui, name string, defaultParam string, coords []int, editable bool) error { view, err := createView(g, name, coords, editable) - if err != nil { - return err - } + if err != nil { + return err + } g.SetKeybinding(view.Name(), gocui.KeyCtrlK, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { v.Clear() return nil }) - fmt.Fprint(view, defaultParam) + fmt.Fprint(view, defaultParam) return nil } func generateMultipleParameterView(g *gocui.Gui, name string, defaultParams []string, coords []int, editable bool) error { view, err := createView(g, name, coords, editable) - if err != nil { - return err - } + if err != nil { + return err + } currentOpt := 0 maxOpt := len(defaultParams) fmt.Fprint(view, defaultParams[currentOpt]) - + viewTitle := name - // Adjust view title to hint the user about the available + // Adjust view title to hint the user about the available // options if there are more than one if maxOpt > 1 { viewTitle = name + " (UP/DOWN => Select default value)" } - + view.Title = viewTitle g.SetKeybinding(view.Name(), gocui.KeyArrowDown, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {