Skip to content

Commit

Permalink
Add flattening of config sequences and modify final output
Browse files Browse the repository at this point in the history
- Sequences in configuration are now flattened on parsing. You can now mix string and sequences in the configuration.
- The parsing functions `parseCommand` and ` parseOptions` now return `[]string` insteand of `string`.
- The 'raw' outputs now print each elements (arguments/options) on each line.

This allow easier management of arguments for shell scripts.

```sh
mapfile -t fzf_options <<< "$(fzg -r -o select_multi)"
fzf_options+=(--query="$1")  # extra options
fzf "${fzf_options[@]}"
```

When both command and fzf options are needed, the separator between command and options is `\x1E`.

```sh
config="$(fzg -r -p view_files)"
mapfile -t fzf_command <<< "${config%%$'\n\x1E'*}"
mapfile -t fzf_options <<< "${config##*$'\n\x1E'}"
"${fzf_command[@]}" pattern | fzf "${fzf_options[@]}" --prompt='Files: '
```

- Change environment variable name for the configuration file : `$FZF_CONFIG_FILE --> $FZF_CONF`
- Update error messages and rename variables in `main.go`
- Update the configuration
  • Loading branch information
krakozaure committed Feb 13, 2022
1 parent 2aae353 commit dcb773f
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 53 deletions.
36 changes: 24 additions & 12 deletions cmd/fzg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,40 @@ func main() {
os.Exit(1)
}

conf, text := "", ""
err_text, out_text := "", ""
if fzg.CommandFlag != "" && fzg.OptionsFlag != "" {
conf = fmt.Sprintf("command '%s' or options '%s'", fzg.CommandFlag, fzg.OptionsFlag)
text = fzg.ParseConfig(
err_text = fmt.Sprintf(
"Invalid or missing configuration for '%s' or '%s'",
fzg.CommandFlag, fzg.OptionsFlag,
)
out_text = fzg.ParseConfig(
config.Commands[fzg.CommandFlag],
config.Options[fzg.OptionsFlag],
)
} else if fzg.CommandFlag != "" {
conf = fmt.Sprintf("command '%s'", fzg.CommandFlag)
text = fzg.ParseConfig(
err_text = fmt.Sprintf(
"Invalid or missing command configuration for '%s'",
fzg.CommandFlag,
)
out_text = fzg.ParseConfig(
config.Commands[fzg.CommandFlag],
nil,
)
} else if fzg.OptionsFlag != "" {
conf = fmt.Sprintf("options '%s'", fzg.OptionsFlag)
text = fzg.ParseConfig(
err_text = fmt.Sprintf(
"Invalid or missing options configuration for '%s'",
fzg.OptionsFlag,
)
out_text = fzg.ParseConfig(
nil,
config.Options[fzg.OptionsFlag],
)
} else if fzg.ProfileFlag != "" {
conf = fmt.Sprintf("profile '%s'", fzg.ProfileFlag)
text = fzg.ParseConfig(
err_text = fmt.Sprintf(
"Invalid or missing profile configuration for '%s'",
fzg.ProfileFlag,
)
out_text = fzg.ParseConfig(
config.Profiles[fzg.ProfileFlag].Command,
config.Profiles[fzg.ProfileFlag].Options,
)
Expand All @@ -49,11 +61,11 @@ func main() {
os.Exit(1)
}

if text == "" {
if out_text == "" {
if !fzg.QuietFlag {
fmt.Fprintf(os.Stderr, "Missing or invalid configuration %s\n", conf)
fmt.Fprintf(os.Stderr, "%s\n", err_text)
}
os.Exit(1)
}
fmt.Print(text)
fmt.Print(out_text)
}
143 changes: 129 additions & 14 deletions configs/fzg.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,147 @@
bindings:
jump: &binds_jump
alt-g: jump
alt-j: jump-accept

move: &binds_move
",": down
";": up
"alt-,": last
"alt-;": first

scroll: &binds_scroll
alt-d: preview-half-page-down
alt-u: preview-half-page-up
alt-e: preview-down
alt-y: preview-up

select: &binds_select
alt-a: select-all
alt-A: deselect-all

toggle: &binds_toggle
ctrl-alt-a: toggle-all
ctrl-alt-p: toggle-preview
ctrl-alt-s: toggle-sort

all: &binds_all
<<: *binds_jump
<<: *binds_move
<<: *binds_scroll
<<: *binds_select
<<: *binds_toggle

# -------------------------------------------------------

commands:
invalid_command: null
fd: &cmd_fd [fd, --color=always, --hidden]
fd_dirs: &cmd_fd_dirs [*cmd_fd, --type=d]
fd_files: &cmd_fd_files [*cmd_fd, --type=f]

find_files: &cmd_find_files >-
find . -mindepth 1
-not \( -path './.git/*' -or -path './node_modules/*' \)
-and -type f
rg:
&cmd_rg [
rg,
--color=always,
--column,
--hidden,
--line-number,
--no-heading,
--no-ignore,
--smart-case,
--glob=!.git,
--glob=!node_modules,
]

options:
invalid_options: null
previews:
bat: &pview_bat bat --color=always --style=changes,header,numbers,rule,snip
tree: &pview_tree tree -aCFL 1

# -------------------------------------------------------

options:
default: &opts_default
ansi: true
bind:
<<: *binds_all
border: none
color:
- 16,bg+:-1,fg+:cyan,border:black,info:bright-black
- hl:bright-yellow,hl+:bright-yellow
- header:bright-blue,prompt:bright-blue,spinner:bright-magenta
- marker:bright-green,pointer:bright-cyan
exact: false
extended: true
header-first: true
marker: ""
multi: false
pointer: ""
prompt: "fzg: "
reverse: true
select-1: true

preview: &opts_preview
select_one: &opts_select_one
<<: *opts_default
exact: true
multi: false

select_multi: &opts_select_multi
<<: *opts_default
exact: false
multi: true
preview-window: 60%,right,wrap

preview: &opts_preview
<<: *opts_select_multi
preview-window: 66%,right,border-left
preview:
- 'cat -n {1} 2>/dev/null'
- '|| tree -aCFL 1 {1} 2>/dev/null'
prompt: 'view: '
[
"(",
*pview_bat,
"-- {} ||",
*pview_tree,
"-- {} ||",
"file -- {}",
") 2> /dev/null",
]

grep: &opts_grep
<<: *opts_select_multi
preview-window: up,66%,border-bottom,+{2}+3/3,~1
preview: [*pview_bat, "--highlight-line {2} -- {1}"]
sort: false

vimgrep: &opts_vimgrep
<<: *opts_grep
delimiter: ":"
nth: -1

# -------------------------------------------------------

profiles:
invalid_profile: null
select_paths:
command: [*cmd_fd, "--max-depth=1"]
options:
<<: *opts_select_multi

select_paths_recursive:
command: *cmd_fd
options:
<<: *opts_select_multi

view_files:
command: *cmd_find_files
command: *cmd_fd_files
options: *opts_preview

filter_lines_regex:
command: *cmd_rg
options:
<<: *opts_vimgrep
bind:
<<: *binds_all
change: reload( sleep 0.05; $FZF_DEFAULT_COMMAND {q} || true )
disabled: true

filter_lines_fuzzy:
command: *cmd_rg
options:
<<: *opts_vimgrep
exact: true
24 changes: 15 additions & 9 deletions internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package fzg
import (
"fmt"
"reflect"
"strings"
)

type Command interface{}

func parseCommand(commandConf Command) string {
func parseCommand(commandConf Command) []string {
if commandConf == nil {
return ""
return []string{}
}
command := ""
command := []string{}
switch commandConf.(type) {
case []interface{}:
return commandFromSequence(commandConf)
Expand All @@ -23,15 +22,22 @@ func parseCommand(commandConf Command) string {
}
}

func commandFromSequence(ivalue interface{}) string {
func commandFromSequence(ivalue interface{}) []string {
strSlice := []string{}
v := reflect.ValueOf(ivalue)
for i := 0; i < v.Len(); i++ {
strSlice = append(strSlice, fmt.Sprint(v.Index(i)))
switch ivalue := v.Index(i).Interface().(type) {
case []interface{}:
for _, element := range ivalue {
strSlice = append(strSlice, element.(string))
}
case string:
strSlice = append(strSlice, fmt.Sprint(v.Index(i)))
}
}
return strings.Join(strSlice, " ")
return strSlice
}

func commandFromString(ivalue interface{}) string {
return fmt.Sprint(ivalue)
func commandFromString(ivalue interface{}) []string {
return []string{fmt.Sprint(ivalue)}
}
26 changes: 17 additions & 9 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -46,18 +47,25 @@ func ParseConfig(commandConf Command, optionsConf Options) string {
options := parseOptions(optionsConf)
output := ""
if !RawFlag {
if command != "" {
output += fmt.Sprintf("export %s=%q\n", "FZF_DEFAULT_COMMAND", command)
if len(command) > 0 {
output += fmt.Sprintf(
"export %s=%q\n", "FZF_DEFAULT_COMMAND", strings.Join(command, " "),
)
}
if options != "" {
output += fmt.Sprintf("export %s=%q\n", "FZF_DEFAULT_OPTS", options)
if len(options) > 0 {
output += fmt.Sprintf(
"export %s=%q\n", "FZF_DEFAULT_OPTS", strings.Join(options, " "),
)
}
} else {
if command != "" {
output += fmt.Sprintf("%s\n", command)
if len(command) > 0 {
output += fmt.Sprintf("%s\n", strings.Join(command, "\n"))
}
if options != "" {
output += fmt.Sprintf("%s\n", options)
if len(options) > 0 {
if len(command) > 0 {
output += "\x1E"
}
output += fmt.Sprintf("%s\n", strings.Join(options, "\n"))
}
}
return output
Expand All @@ -66,7 +74,7 @@ func ParseConfig(commandConf Command, optionsConf Options) string {
func configFile() (string, error) {
var confFile string

envConfigFile := os.Getenv("FZG_CONFIG_FILE")
envConfigFile := os.Getenv("FZG_CONF")
xdgConfigFile := os.ExpandEnv(CONFIG_FILE_PATH)

if envConfigFile != "" && isFile(envConfigFile) {
Expand Down
19 changes: 13 additions & 6 deletions internal/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

type Options map[string]interface{}

func parseOptions(optionsConf Options) string {
func parseOptions(optionsConf Options) []string {
if optionsConf == nil {
return ""
return []string{}
}
options := []string{}
for name, ivalue := range optionsConf {
Expand All @@ -29,7 +29,7 @@ func parseOptions(optionsConf Options) string {
}
}
sort.Strings(options)
return strings.Join(options, " ")
return options
}

func optionFromMap(name string, ivalue interface{}) string {
Expand All @@ -45,7 +45,14 @@ func optionFromSlice(name string, ivalue interface{}) string {
strSlice := []string{}
v := reflect.ValueOf(ivalue)
for i := 0; i < v.Len(); i++ {
strSlice = append(strSlice, fmt.Sprint(v.Index(i)))
switch ivalue := v.Index(i).Interface().(type) {
case []interface{}:
for _, element := range ivalue {
strSlice = append(strSlice, element.(string))
}
case string:
strSlice = append(strSlice, fmt.Sprint(v.Index(i)))
}
}

if name == "preview" {
Expand All @@ -71,8 +78,8 @@ func optionFromString(name string, ivalue interface{}) string {
}

func quotedOption(key string, ivalue interface{}) string {
if strings.Contains(ivalue.(string), " ") {
return fmt.Sprintf("--%s=%q", key, ivalue)
if !RawFlag && (key == "bind" || strings.Contains(ivalue.(string), " ")) {
return fmt.Sprintf("--%s='%s'", key, ivalue)
} else {
return fmt.Sprintf("--%s=%s", key, ivalue)
}
Expand Down
4 changes: 1 addition & 3 deletions scripts/install
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ set -e

go mod tidy
go build -ldflags='-s -w' -o "./dist/fzg" -- "./cmd/fzg/main.go"

mkdir -pv -- "$HOME/.local/bin"
cp -av -- "./dist/fzg" "$HOME/.local/bin/fzg"
[ -d "$HOME/.local/bin" ] && mv -v -- "./dist/fzg" "$HOME/.local/bin/fzg"

mkdir -pv -- "$HOME/.config/fzg"
cp -av -- "./configs"/* "$HOME/.config/fzg"

0 comments on commit dcb773f

Please sign in to comment.