inputconsole

package module
v0.0.0-...-910cf12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 3 Imported by: 0

README

InputConsole

GoDoc Go Report Card

InputConsole is a console for Go that will keep all output above the input line without interrupting the input line.

Looking for the Python version?

Install

$ go get github.com/eiannone/keyboard
$ go get github.com/nathan-fiscaletti/inputconsole

Usage

import(
    "time"

    "github.com/nathan-fiscaletti/inputconsole-go"
)

func main() {
    console := inputconsole.NewInputConsole()

    // Register a command.
    // Runtime exceptions caused by commands are automatically caught
    // and an error message will be written to the inputconsole.
    console.RegisterCommand("help", func(params []string) {
        console.Writef("I don't want to help you %s", params[0])
    })

    // Start listening for input on a new thread
    // Input line will always stay at the bottom
    console.ListenForInput("> ")

    // Set unknown command handler, Return 'true' for command handled
    // or 'false' for command not handled.
    console.SetUnknownCommandHandler(func(command string) bool {
        console.Writef("Unknown command: %s\n", command)
        return true
    })

    // Generate random output to keep the output thread active.
    go func() {
        var cnt int = 0
        for {
            console.Writef("This is a test message: %d\n", cnt)
            time.Sleep(time.Second)
            cnt = cnt + 1
        }
    }()

    // Keep the process alive
    for true {
        time.Sleep(time.Second)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InputConsole

type InputConsole struct {
	// contains filtered or unexported fields
}

InputConsole represents a console in which the input text is kept separate from the output text. You will be able to type on the input line without the output lines interfering with it.

func NewInputConsole

func NewInputConsole() *InputConsole

NewInputConsole creates a new instance of an InputConsole.

func (*InputConsole) ListenForInput

func (ic *InputConsole) ListenForInput(prompt string)

ListenForInput will listen for input on a new thread using the specified prompt.

func (*InputConsole) RegisterCommand

func (ic *InputConsole) RegisterCommand(
	name string, action func([]string),
)

RegisterCommand will register a command with the specified name and action. The action callback should take an array of strings that will represent the arguments passed to the command.

func (*InputConsole) SetUnknownCommandHandler

func (ic *InputConsole) SetUnknownCommandHandler(
	handler func(string) bool,
)

SetUnknownCommandHandler will set the handler to use for unknown commands. The handler will be passed the full command string and should return a boolean indicating whether or not the command was handled by the handler.

func (*InputConsole) Writef

func (ic *InputConsole) Writef(format string, vargs ...interface{})

Writef will write a message to the InputConsole using the specified format and arguments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL