shithub: hell

ref: 34984e1d7dfcb3e013e9834e3bb19e522e75a0d7
dir: /readline.go/

View raw version
//go:build !plan9

package main

import (
	native "github.com/ergochat/readline"
	"io"
	"os"
)

type readline struct {
	*native.Instance
}

var (
	ErrInterrupt = native.ErrInterrupt
)

type Config = native.Config

const (
	CharCtrlJ     = native.CharCtrlJ
	CharInterrupt = native.CharInterrupt
	CharEnter     = native.CharEnter
)

func NewReadline(config *Config) (*readline, error) {
	instance, err := native.NewEx(config)
	if err != nil {
		return nil, err
	}
	return &readline{instance}, nil
}

func enablePipeHack(rl *readline) {
	//Horrible io pipe hack
	//Replaces system stdout with the readline one
	r, w, _ := os.Pipe()
	os.Stdout = w

	go func() {
		io.Copy(rl.Stdout(), r)
	}()
}