simprompt

package module
v0.0.0-...-2633b88 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 7 Imported by: 0

README

simprompt - A Simple Prompt

simprompt is a minimum of minimum prompt implement for me, written in Go.

Usage

go get github.com/TylorShine/simprompt

See cmd/example.go:

package main

import (
	"fmt"
	"strings"

	"github.com/TylorShine/simprompt"
)

func main() {
	// make the simprompt
	sp := simprompt.NewSimPrompt()

	// append commands
	sp.AppendCmd([]string{"t", "test"}, "Test Command", func(s []string) bool {
		fmt.Println("Argn:", len(s), ",Argv:", s, "\n...was given.")
		return true
	})

	sp.AppendCmd([]string{"p", "prompt"}, "Set Prompt", func(s []string) bool {
		if len(s) > 0 {
			sp.Prompt = strings.Join(s, "/") + "> "
			// or, simprompt.SetPrompt()
			// sp.SetPrompt(strings.Join(s, "/") + "> ")
		}
		return true
	})

	sp.AppendCmd([]string{"q", "quit", "exit"}, "Quit Program", func(s []string) bool {
		fmt.Println("Quit, bye!")
		return false
	})

	// nil given, read from os.Stdin
	done := sp.Run(nil)
	<-done
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SimPrompt

type SimPrompt struct {
	Prompt          string
	Cmds            map[string]SimPromptCmd
	DefaultCallback func([]string) bool
	CmdNum          int
}

func NewSimPrompt

func NewSimPrompt() *SimPrompt

func (*SimPrompt) AppendCmd

func (sp *SimPrompt) AppendCmd(cmd []string, help string, cb func([]string) bool) error

func (*SimPrompt) GetHelp

func (sp *SimPrompt) GetHelp(cmd string) string

func (*SimPrompt) GetHelpAll

func (sp *SimPrompt) GetHelpAll() (ret []string)

func (*SimPrompt) Run

func (sp *SimPrompt) Run(scan *os.File) chan bool

func (*SimPrompt) SetCmds

func (sp *SimPrompt) SetCmds(cmds map[string]SimPromptCmd)

func (*SimPrompt) SetDefaultCallback

func (sp *SimPrompt) SetDefaultCallback(cb func([]string) bool)

func (*SimPrompt) SetHelp

func (sp *SimPrompt) SetHelp(cmd, help string) error

func (*SimPrompt) SetPrompt

func (sp *SimPrompt) SetPrompt(s string)

type SimPromptCmd

type SimPromptCmd struct {
	Command  []string
	Help     string
	Callback func([]string) bool // Callback(args) => isExit
	CmdIndex int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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