<commit_before>package cliedit

import (
	"bufio"
	"io"
	"os"
	"strings"
	"sync"

	"github.com/elves/elvish/cli"
	"github.com/elves/elvish/cli/addons/instant"
	"github.com/elves/elvish/cli/el"
	"github.com/elves/elvish/eval"
	"github.com/elves/elvish/eval/vals"
	"github.com/elves/elvish/parse"
)

func initInstant(app cli.App, ev *eval.Evaler, ns eval.Ns) {
	bindingVar := newBindingVar(EmptyBindingMap)
	binding := newMapBinding(app, ev, bindingVar)
	ns.AddNs("-instant",
		eval.Ns{
			"binding": bindingVar,
		}.AddGoFns("<edit:-instant>", map[string]interface{}{
			"start": func() { instantStart(app, ev, binding) },
		}))
}

func instantStart(app cli.App, ev *eval.Evaler, binding el.Handler) {
	execute := func(code string) ([]string, error) {
		src := eval.NewInteractiveSource(code)
		n, err := parse.AsChunk("[instant]", code)
		if err != nil {
			return nil, err
		}
		op, err := ev.Compile(n, src)
		if err != nil {
			return nil, err
		}
		fm := eval.NewTopFrame(ev, src, []*eval.Port{
			{File: eval.DevNull},
			{}, // Will be replaced in CaptureOutput
			{File: eval.DevNull},
		})
		var output []string
		var outputMutex sync.Mutex
		addLine := func(line string) {
			outputMutex.Lock()
			defer outputMutex.Unlock()
			output = append(output, line)
		}
		valuesCb := func(ch <-chan interface{}) {
			for v := range ch {
				addLine("▶ " + vals.ToString(v))
			}
		}
		bytesCb := func(r *os.File) {
			bufr := bufio.NewReader(r)
			for {
				line, err := bufr.ReadString('\n')
				if err != nil {
					if err != io.EOF {
						addLine("i/o error: " + err.Error())
					}
					break
				}
				addLine(strings.TrimSuffix(line, "\n"))
			}
		}
		err = fm.ExecWithOutputCallback(op, valuesCb, bytesCb)
		return output, err
	}
	instant.Start(app, instant.Config{Binding: binding, Execute: execute})
}
<commit_msg>cliedit: Fix name of the edit:instant namespace.<commit_after>package cliedit

import (
	"bufio"
	"io"
	"os"
	"strings"
	"sync"

	"github.com/elves/elvish/cli"
	"github.com/elves/elvish/cli/addons/instant"
	"github.com/elves/elvish/cli/el"
	"github.com/elves/elvish/eval"
	"github.com/elves/elvish/eval/vals"
	"github.com/elves/elvish/parse"
)

func initInstant(app cli.App, ev *eval.Evaler, ns eval.Ns) {
	bindingVar := newBindingVar(EmptyBindingMap)
	binding := newMapBinding(app, ev, bindingVar)
	ns.AddNs("-instant",
		eval.Ns{
			"binding": bindingVar,
		}.AddGoFns("<edit:-instant>:", map[string]interface{}{
			"start": func() { instantStart(app, ev, binding) },
		}))
}

func instantStart(app cli.App, ev *eval.Evaler, binding el.Handler) {
	execute := func(code string) ([]string, error) {
		src := eval.NewInteractiveSource(code)
		n, err := parse.AsChunk("[instant]", code)
		if err != nil {
			return nil, err
		}
		op, err := ev.Compile(n, src)
		if err != nil {
			return nil, err
		}
		fm := eval.NewTopFrame(ev, src, []*eval.Port{
			{File: eval.DevNull},
			{}, // Will be replaced in CaptureOutput
			{File: eval.DevNull},
		})
		var output []string
		var outputMutex sync.Mutex
		addLine := func(line string) {
			outputMutex.Lock()
			defer outputMutex.Unlock()
			output = append(output, line)
		}
		valuesCb := func(ch <-chan interface{}) {
			for v := range ch {
				addLine("▶ " + vals.ToString(v))
			}
		}
		bytesCb := func(r *os.File) {
			bufr := bufio.NewReader(r)
			for {
				line, err := bufr.ReadString('\n')
				if err != nil {
					if err != io.EOF {
						addLine("i/o error: " + err.Error())
					}
					break
				}
				addLine(strings.TrimSuffix(line, "\n"))
			}
		}
		err = fm.ExecWithOutputCallback(op, valuesCb, bytesCb)
		return output, err
	}
	instant.Start(app, instant.Config{Binding: binding, Execute: execute})
}

{'fraction_non_alphanumeric': 0.16928920238741182, 'fraction_numerical': 0.0, 'mean_word_length': 3.410287081339713, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, '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': '5729150', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1406, 'n_words': 292}