<commit_before>package action

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strings"

	shellquote "github.com/kballard/go-shellquote"
	"github.com/urfave/cli"
)

// Complete prints a list of all password names to os.Stdout
func (s *Action) Complete(*cli.Context) {
	list, err := s.Store.List()
	if err != nil {
		return
	}

	for _, v := range list {
		fmt.Println(v)
	}
}

// CompletionBash returns a bash script used for auto completion
func (s *Action) CompletionBash(c *cli.Context) error {
	out := `#!/bin/bash

PROG=gopass

_cli_bash_autocomplete() {
     local cur opts base
     COMPREPLY=()
     cur="${COMP_WORDS[COMP_CWORD]}"
     opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
     COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
     return 0
 }

complete -F _cli_bash_autocomplete $PROG
`
	fmt.Println(out)

	return nil
}

// CompletionZSH returns a script that uses bash's auto completion
func (s *Action) CompletionZSH(c *cli.Context) error {
	out := `autoload -U compinit && compinit
autoload -U bashcompinit && bashcompinit

source <(gopass completion bash)
`
	fmt.Println(out)

	return nil
}

// CompletionDMenu returns a script that starts dmenu
// Usage: eval "$(gopass completion dmenu)"
func (s *Action) CompletionDMenu(c *cli.Context) error {
	typeit := c.Bool("type")
	args := c.String("args")

	list, err := s.Store.List()
	if err != nil {
		return err
	}

	argsSplit, err := shellquote.Split(args)
	if err != nil {
		return err
	}

	name, err := dmenu(list, argsSplit...)
	if err != nil {
		return err
	}

	content, err := s.Store.First(name)
	if err != nil {
		return err
	}

	if typeit {
		return exec.Command("xdotool", "type", "--clearmodifiers", string(content)).Run()
	}

	return s.copyToClipboard(name, content)
}

// dmenu runs it with the provided strings and returns the selected string
func dmenu(list []string, args ...string) (string, error) {
	stdin := bytes.NewBuffer(nil)
	for _, v := range list {
		stdin.WriteString(v + "\n")
	}

	cmd := exec.Command("dmenu", args...)
	cmd.Stdin = stdin
	cmd.Stderr = os.Stderr
	out, err := cmd.Output()
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(out)), nil
}<commit_msg>specify end of flags for xdotool<commit_after>package action

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strings"

	shellquote "github.com/kballard/go-shellquote"
	"github.com/urfave/cli"
)

// Complete prints a list of all password names to os.Stdout
func (s *Action) Complete(*cli.Context) {
	list, err := s.Store.List()
	if err != nil {
		return
	}

	for _, v := range list {
		fmt.Println(v)
	}
}

// CompletionBash returns a bash script used for auto completion
func (s *Action) CompletionBash(c *cli.Context) error {
	out := `#!/bin/bash

PROG=gopass

_cli_bash_autocomplete() {
     local cur opts base
     COMPREPLY=()
     cur="${COMP_WORDS[COMP_CWORD]}"
     opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
     COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
     return 0
 }

complete -F _cli_bash_autocomplete $PROG
`
	fmt.Println(out)

	return nil
}

// CompletionZSH returns a script that uses bash's auto completion
func (s *Action) CompletionZSH(c *cli.Context) error {
	out := `autoload -U compinit && compinit
autoload -U bashcompinit && bashcompinit

source <(gopass completion bash)
`
	fmt.Println(out)

	return nil
}

// CompletionDMenu returns a script that starts dmenu
// Usage: eval "$(gopass completion dmenu)"
func (s *Action) CompletionDMenu(c *cli.Context) error {
	typeit := c.Bool("type")
	args := c.String("args")

	list, err := s.Store.List()
	if err != nil {
		return err
	}

	argsSplit, err := shellquote.Split(args)
	if err != nil {
		return err
	}

	name, err := dmenu(list, argsSplit...)
	if err != nil {
		return err
	}

	content, err := s.Store.First(name)
	if err != nil {
		return err
	}

	if typeit {
		return exec.Command("xdotool", "type", "--clearmodifiers", "--", string(content)).Run()
	}

	return s.copyToClipboard(name, content)
}

// dmenu runs it with the provided strings and returns the selected string
func dmenu(list []string, args ...string) (string, error) {
	stdin := bytes.NewBuffer(nil)
	for _, v := range list {
		stdin.WriteString(v + "\n")
	}

	cmd := exec.Command("dmenu", args...)
	cmd.Stdin = stdin
	cmd.Stderr = os.Stderr
	out, err := cmd.Output()
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(out)), nil
}
{'fraction_non_alphanumeric': 0.1504865354152523, 'fraction_numerical': 0.0009051821679112921, 'mean_word_length': 3.5803108808290154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5036348', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1597, 'n_words': 445}