<commit_before>package completion

import (
	"context"
	"errors"
	"os"
	"regexp"
	"strings"

	"github.com/zetamatta/go-findfile"

	"github.com/zetamatta/nyagos/dos"
)

var ErrCtrlC = errors.New("C-c")

const (
	STD_SLASH = string(os.PathSeparator)
	OPT_SLASH = "/"
)

var IncludeHidden = false

var rxEnvPattern = regexp.MustCompile("%[^%]+%")

func replaceEnv(str string) string {
	str = rxEnvPattern.ReplaceAllStringFunc(str, func(p string) string {
		if len(p) == 2 {
			return "%"
		}
		name := p[1 : len(p)-1]
		for _, env := range PercentVariables {
			if value := env.Lookup(name); value != "" {
				return value
			}
		}
		return p
	})

	if len(str) >= 2 && str[0] == '~' && os.IsPathSeparator(str[1]) {
		if home := dos.GetHome(); home != "" {
			str = home + str[1:]
		}
	}
	return str
}

func listUpFiles(ctx context.Context, str string) ([]Element, error) {
	orgSlash := STD_SLASH[0]
	if UseSlash {
		orgSlash = OPT_SLASH[0]
	}
	if pos := strings.IndexAny(str, STD_SLASH+OPT_SLASH); pos >= 0 {
		orgSlash = str[pos]
	}
	str = strings.Replace(strings.Replace(str, OPT_SLASH, STD_SLASH, -1), `"`, "", -1)
	directory := DirName(str)
	wildcard := dos.Join(replaceEnv(directory), "*")

	// Drive letter
	cutprefix := 0
	if strings.HasPrefix(directory, STD_SLASH) {
		wd, _ := os.Getwd()<commit_msg>Completion support $ENV\name... by native (see also #228)<commit_after>package completion

import (
	"context"
	"errors"
	"os"
	"strings"

	"github.com/zetamatta/go-findfile"

	"github.com/zetamatta/nyagos/dos"
)

var ErrCtrlC = errors.New("C-c")

const (
	STD_SLASH = string(os.PathSeparator)
	OPT_SLASH = "/"
)

var IncludeHidden = false

func listUpFiles(ctx context.Context, str string) ([]Element, error) {
	orgSlash := STD_SLASH[0]
	if UseSlash {
		orgSlash = OPT_SLASH[0]
	}
	if pos := strings.IndexAny(str, STD_SLASH+OPT_SLASH); pos >= 0 {
		orgSlash = str[pos]
	}
	str = strings.Replace(strings.Replace(str, OPT_SLASH, STD_SLASH, -1), `"`, "", -1)
	directory := DirName(str)
	wildcard := dos.Join(findfile.ExpandEnv(directory), "*")

	// Drive letter
	cutprefix := 0
	if strings.HasPrefix(directory, STD_SLASH) {
		wd, _ := os.Getwd()
{'fraction_non_alphanumeric': 0.16945996275605213, 'fraction_numerical': 0.010242085661080074, 'mean_word_length': 4.0093240093240095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '3783554', 'n_tokens_mistral': 904, 'n_tokens_neox': 852, 'n_words': 170}