shithub: hell

ref: a80d76edbe913b8f6d9aace23357ca4d41b1c93d
dir: /readline_plan9.go/

View raw version
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

//not relevant
const (
	CharCtrlJ     = 0
	CharInterrupt = 0
	CharEnter     = 0
)

type readline struct {
	config Config
}

type Config struct {
	Prompt string

	//These are unused on Plan 9
	FuncFilterInputRune func(rune) (rune, bool)
	Listener          func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
}

func (rl *readline) GetConfig() *Config {
	return &Config{Prompt: rl.config.Prompt}
}

func NewReadline(config *Config) (*readline, error) {
	return &readline{config: *config}, nil
}

func (rl *readline) ReadLineWithConfig(cfg *Config) (string, error) {
	fmt.Print(cfg.Prompt)
	scanner := bufio.NewScanner(os.Stdin)

	// Scan for the next token (by default, a line)
	scanner.Scan()

	// Get the text of the scanned line
	input := scanner.Text()
	return strings.TrimSpace(input), nil
}

func (rl *readline) Stdout() io.Writer {
	return os.Stdout
}

func (rl *readline) Close() {
	return
}

func (rl *readline) SetPrompt(prompt string) {
	rl.config.Prompt = prompt
}

func (rl *readline) SetDefault(string) {
}

func (rl *readline) Readline() (string, error) {
	return rl.ReadLineWithConfig(&rl.config)
}

func enablePipeHack(rl *readline) {
}