lingling2012 / go-terminput

Package terminput provides terminal keyboard input for interactive command-line tools.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Terminput

Package terminput provides terminal keyboard input for interactive command-line tools.

Example

package main

import (
	"fmt"
	"log"

	"github.com/pkg/term"
	"github.com/tj/go-terminput"
)

func main() {
	t, err := term.Open("/dev/tty")
	if err != nil {
		log.Fatalf("error: %s\n", err)
	}

	t.SetRaw()
	defer t.Restore()

	fmt.Printf("Type something, use 'q' to exit.\r\n")

	for {
		e, err := terminput.Read(t)
		if err != nil {
			log.Fatalf("error: %s\n", err)
		}

		if e.Key() == terminput.KeyEscape || e.Rune() == 'q' {
			break
		}

		fmt.Printf("%s — shift=%v ctrl=%v alt=%v meta=%v\r\n", e.String(), e.Shift(), e.Ctrl(), e.Alt(), e.Meta())
	}
}

GoDoc

Sponsors

This project is sponsored by CTO.ai, making it easy for development teams to create and share workflow automations without leaving the command line.

About

Package terminput provides terminal keyboard input for interactive command-line tools.

License:MIT License


Languages

Language:Go 100.0%