Socket
Book a DemoInstallSign in
Socket

notabug.org/_percival/goinput

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

notabug.org/_percival/goinput

Go Modules
Version
v0.0.0-20250606064325-b95263f50662
Version published
Created
Source

goinput

Table Of Contents

  • Intro
  • Learn
  • Start

Intro

Go library for building an interface and entering user data.

License

The goinput is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3.

Learn

Module

For more information about the module, use the link.

Use

Processing user input and executing commands:

func testInput() (err error) {
	var (
		data = goinput.Data{
			Slice: make([]byte, 32),
		}
		input = &goinput.Input{
			Fd: syscall.Stdin,
		}
		sigInt = make(chan os.Signal, 1)
		keys   = goinput.Keys{
			"a": {
				EventFunc: func(key *goinput.Key) (err error) {
					fmt.Println("a")
					return
				},
			},
			"c": {
				EventFunc: func(key *goinput.Key) (err error) {
					key.Data[0].(chan os.Signal) <- syscall.SIGINT
					return
				},
				Data: append(make([]any, 0, 0), sigInt),
			},
			"1 2 3": {
				EventFunc: func(key *goinput.Key) (err error) {
					fmt.Println("1 2 3")
					return
				},
			},
		}
	)

	defer func() {
		close(sigInt)
	}()

	if err = keys.Compile(); err != nil {
		return
	}

	signal.Notify(sigInt, syscall.SIGINT)

	for {
		var (
			pressedKeys = keys.PressedKeys()
			pairNum     = 0
		)

		if !input.IsRaw() {
			if err = input.SetRaw(); err != nil {
				return
			}
		}

	_loop:
		if err = input.Read(&data); err != nil {
			return
		}

		for _, name := range *pressedKeys.Update(keys, data, pairNum) {
			switch key := keys[name]; {
			case pairNum == key.Len()-1:
				if err = input.ResetRaw(); err != nil {
					return
				}

				key.Event()

				select {
				case <-sigInt:
					return
				default:
				}
			case pairNum < key.Len()-1:
				pairNum++

				goto _loop
			}
		}
	}
}

Start

Get

To get, execute the command:

~ $ go get -v notabug.org/_percival/goinput@latest

Update

To update, execute the command:

~ $ go get -u -v notabug.org/_percival/goinput@latest

FAQs

Package last updated on 06 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts