tui

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: GPL-2.0 Imports: 6 Imported by: 0

README

plane-tui v1

This branch contains the original, minimal terminal user interface loop used by Plane. It provides a lightweight command dispatch system with readline-powered input, contextual navigation, and auto-generated help.

Features

  • Simple Command interface (Name, Help, Exec) for rapid command implementation
  • Context-aware prompt with nested command groups
  • Built-in help (help, ls, ?) and exit (exit, quit, q) commands
  • Readline history and tab completion driven by registered contexts/commands
  • Configurable prompt prefix and help header text

Installation

go get github.com/network-plane/plane-tui@v1

Quick Start

package main

import (
    "fmt"
    "strings"

    "github.com/chzyer/readline"
    tui "github.com/network-plane/plane-tui"
)

type echoCommand struct{}

func (c *echoCommand) Name() string { return "echo" }
func (c *echoCommand) Help() string { return "echo back any arguments" }
func (c *echoCommand) Exec(args []string) {
    fmt.Println(strings.Join(args, " "))
}

func main() {
    rl, err := readline.NewEx(&readline.Config{Prompt: "> "})
    if err != nil {
        panic(err)
    }
    defer rl.Close()

    // Register a global command (root context uses an empty string).
    tui.RegisterCommand("", &echoCommand{})

    if err := tui.Run(rl); err != nil {
        panic(err)
    }
}

Working with Contexts

Commands can be grouped under named contexts to create a directory-like experience:

type listServers struct{}

func (*listServers) Name() string { return "list" }
func (*listServers) Help() string { return "list known servers" }
func (*listServers) Exec(args []string) { fmt.Println("server-1\nserver-2") }

func initCommands() {
    tui.RegisterContext("servers", "Server management commands")
    tui.RegisterCommand("servers", &listServers{})
}

With the above registration you can type servers to enter the context, then run list. Use / or ctx .. to return to the root context.

Customising the Prompt and Help Header

tui.SetPrompt("plane> ")
tui.SetHelpHeader("Available contexts and commands:")

Built-in Commands

  • help, ?, h, ls – show help for the current context
  • clear – clear the screen (root context only)
  • / or ctx .. – return to the root context
  • exit, quit, q – terminate the session

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCommand

func RegisterCommand(ctxName string, cmd Command)

RegisterCommand registers a command under the specified context.

func RegisterContext

func RegisterContext(name, description string)

RegisterContext registers a new context with an optional description.

func ResetState

func ResetState()

ResetState returns the TUI to the root context, clearing any session-specific state.

func Run

func Run(rl *readline.Instance)

Run starts the main input loop using the provided readline instance.

func SetExitHandler

func SetExitHandler(handler func()) func()

SetExitHandler overrides the behaviour when an exit command is issued, returning the previous handler.

func SetHelpHeader

func SetHelpHeader(h string)

SetHelpHeader allows customising the help text header.

func SetPrompt

func SetPrompt(p string)

SetPrompt sets the base prompt prefix.

Types

type Command

type Command interface {
	Name() string
	Help() string
	Exec(args []string)
}

Command represents an executable command in a context.

Jump to

Keyboard shortcuts

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