<commit_before><commit_msg>Workings of a very simple actual REPL<commit_after>package main

import (
	"fmt"
	"io"

	"github.com/ashb/jq-repl/jq"

	"gopkg.in/chzyer/readline.v1"
)

func main() {
	prompt := "\033[0;36m»\033[0m "
	l, err := readline.New(prompt)

	if err != nil {
		// TODO: don't use panic
		panic(err)
	}

	libjq, err := jq.New()
	if err != nil {
		// TODO: don't use panic
		panic(err)
	}
	defer libjq.Close()

	input, err := jq.JvFromJSONString(`
		{ "simple": 123,
			"nested": {
				"a": [1,2,"a"],
				"b": true,
				"c": null
			}
		}`)
	if err != nil {
		// TODO: don't use panic
		panic(err)
	}

	defer input.Free()

	for {
		line, err := l.Readline()
		if err == io.EOF {
			break
		} else if err == readline.ErrInterrupt {
			// Stop the streaming.
			continue
		} else if err != nil {
			panic(fmt.Errorf("%#v", err))
		}

		chanIn, chanOut, chanErr := libjq.Start(line)
		inCopy := input.Copy()

		// Run until the channels are closed
		for chanErr != nil && chanOut != nil {
			select {
			case e, ok := <-chanErr:
				if !ok {
					chanErr = nil
				} else {
					fmt.Printf("\033[0;31m%s\033[0m\n", e)
				}
			case o, ok := <-chanOut:
				if !ok {
					chanOut = nil
				} else {
					fmt.Printf("// Read from output %v %#v\n", o.Kind(), ok)
					fmt.Println(o.ToGoVal())
				}
			case chanIn <- inCopy:
				// We've sent our input, close the channel to tell Jq we're done
				close(chanIn)
				chanIn = nil
			}
		}
	}

	libjq.Close()
}
{'fraction_non_alphanumeric': 0.1737057220708447, 'fraction_numerical': 0.017711171662125342, 'mean_word_length': 2.3615560640732265, 'pattern_counts': {'":': 5, '<': 6, '<?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': '5223939', 'n_tokens_mistral': 729, 'n_tokens_neox': 590, 'n_words': 153}