Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete using net.Conn #236

Open
vl-axentia opened this issue Jun 25, 2024 · 0 comments
Open

Autocomplete using net.Conn #236

vl-axentia opened this issue Jun 25, 2024 · 0 comments

Comments

@vl-axentia
Copy link

Im trying to use this to get autocomplete through a socket connection, something like this:

var completer = readline.NewPrefixCompleter(
	readline.PcItem("version"),
)

func (c *Console) consoleHandler(conn net.Conn) {
	defer conn.Close()

	rl, err := readline.NewEx(&readline.Config{
		Prompt:         "> ",
		AutoComplete:   completer,
		Stdin:          conn,
		Stdout:         conn,
		Stderr:         conn,
		UniqueEditLine: true,
	})
	if err != nil {
		log.Printf("ERROR: Failed to setup readline: %+v", err)
		return
	}
	defer rl.Close()

	for {
		line, err := rl.Readline()
		if err != nil {
			break
		}

		c.executeCommand(strings.TrimSpace(line), rl)
	}
}

func (c *Console) executeCommand(cmd string, r io.Writer) {
	switch cmd {
	case "version":
		fmt.Fprintf(r, "%s\n", config.Version)
	default:
		fmt.Fprintf(r, "unknown command\n")
	}
}

If I remove the three lines redirecting stdin/stdout/stderr to the conn object, things work like I would expect when pressing TAB. However, when using conn, it instead seems to autocomplete when pressing TAB without displaying it and instead simply adds a TAB character. The second time I wrote version, I pressed TAB before pressing enter.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant