console

package
v0.0.0-...-b9edb71 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package console implements console command parsing for Nox.

Index

Constants

View Source
const (
	ColorBlack       = Color(1)
	ColorDarkGrey    = Color(2)
	ColorLightGrey   = Color(3)
	ColorWhite       = Color(4)
	ColorDarkRed     = Color(5)
	ColorRed         = Color(6)
	ColorLightRed    = Color(7)
	ColorDarkGreen   = Color(8)
	ColorGreen       = Color(9)
	ColorLightGreen  = Color(10)
	ColorDarkBlue    = Color(11)
	ColorBlue        = Color(12)
	ColorLightBlue   = Color(13)
	ColorDarkYellow  = Color(14)
	ColorYellow      = Color(15)
	ColorLightYellow = Color(16)
)
View Source
const (
	// Server flag allows using this command on the server.
	Server = Flags(1 << 0)
	// Client flag allows using this command on the client.
	Client = Flags(1 << 2)
	// NoHelp hides the command from the commands list printed by help.
	NoHelp  = Flags(1 << 3)
	Flag0x8 = Flags(1 << 4)
	// Cheat marks a command as a cheat, thus requiring enabling cheats first.
	Cheat         = Flags(1 << 5)
	FlagDedicated = Flags(1 << 6)
	// Secret flag is used when the command token should be encrypted.
	Secret = Flags(1 << 7)
)
View Source
const (
	// ClientServer flag allows using this command on both the server and the client.
	ClientServer = Server | Client
)

Variables

This section is empty.

Functions

func AsClient

func AsClient(ctx context.Context) context.Context

AsClient marks the commands as executed client-side.

func AsDedicated

func AsDedicated(ctx context.Context) context.Context

AsDedicated marks the commands as executed in a dedicated server context.

func AsServer

func AsServer(ctx context.Context) context.Context

AsServer marks the commands as executed server-side.

func CurCommand

func CurCommand(ctx context.Context) string

CurCommand gets current command string that is being executed.

func EncodeSecret

func EncodeSecret(s string) string

EncodeSecret encodes a command token so it's kept secret. See Secret.

func IsCheats

func IsCheats(ctx context.Context) bool

IsCheats checks if this context allows cheat execution.

func IsClient

func IsClient(ctx context.Context) bool

IsClient checks if it's a client-side command context.

func IsDedicated

func IsDedicated(ctx context.Context) bool

IsDedicated checks if it's a dedicated server command context.

func IsServer

func IsServer(ctx context.Context) bool

IsServer checks if it's a server-side command context.

func WithCheats

func WithCheats(ctx context.Context) context.Context

WithCheats forces a cheats-enabled flag for this context.

Types

type Color

type Color int

Color of console text messages.

type Command

type Command struct {
	// Token is the first token (keyword) of the command.
	Token string
	Alias string
	// HelpID is a string ID used to fetch the help text.
	HelpID strman.ID
	// Help is a text string used when a help text cannot be found by HelpID.
	Help string
	// Flags for this command.
	Flags Flags
	// Sub is a list of sub-commands.
	Sub []*Command
	// Func is a function that will be executed.
	Func CommandFunc
	// LegacyFunc is a legacy function that will be executed.
	//
	// Deprecated: use Func instead.
	LegacyFunc CommandLegacyFunc
	// Raw disables parsing of the rest of the tokens.
	// Command will receive a single token containing the rest of the line.
	Raw bool
}

Command describes a console command.

func (*Command) Register

func (c *Command) Register(sub *Command)

Register a sub command handler.

type CommandFunc

type CommandFunc func(ctx context.Context, c *Console, tokens []string) bool

CommandFunc accepts a set of string arguments (tokens) and executes some action. The function should return true if the parsing was successful and false otherwise.

type CommandLegacyFunc

type CommandLegacyFunc func(ctx context.Context, c *Console, tokInd int, tokens []string) bool

CommandLegacyFunc is the same as CommandFunc, but accepts the full array of tokens and a index of the first token that is the first argument for the command. This functions is a Nox legacy and isn't designed well, e.g. changing parent of the sub-command requires changes to the code, since the number of tokens may change. The function should return true if the parsing was successful and false otherwise.

type Console

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

Console handles console commands.

func New

func New(p Printer) *Console

New creates a new console handler. A custom exec function can be provided. The function is used for macros only. in case it is nil, Exec will be used.

func (*Console) BindKey

func (cn *Console) BindKey(k keybind.Key, cmd string) bool

BindKey binds command to a given key.

func (*Console) BindKeyByName

func (cn *Console) BindKeyByName(name string, cmd string) bool

BindKeyByName binds command to a key with a given alias.

func (*Console) Cheats

func (cn *Console) Cheats() bool

Cheats indicates if cheats are enabled on this console.

func (*Console) Commands

func (cn *Console) Commands() []*Command

Commands lists already registered console commands.

func (*Console) Exec

func (cn *Console) Exec(ctx context.Context, cmd string) bool

Exec a given console command.

func (*Console) ExecMacros

func (cn *Console) ExecMacros(ctx context.Context, k keybind.Key) bool

ExecMacros runs a macros associated with a given key. It returns false if the key is not bound to anything.

func (*Console) HelpString

func (cn *Console) HelpString(cmd *Command) string

func (*Console) Localize

func (cn *Console) Localize(sm *strman.StringManager, tokens ...string)

Localize all command tokens. Additional tokens can be passed as well.

func (*Console) Macros

func (cn *Console) Macros() bool

Macros indicates if macros are enabled on this console.

func (*Console) ParseToken

func (cn *Console) ParseToken(ctx context.Context, tokInd int, tokens []string, cmds []*Command) bool

ParseToken matches the first token against a list of commands.

func (*Console) Print

func (cn *Console) Print(cl Color, str string)

Print exposes underlying Printer.

func (*Console) PrintOrError

func (cn *Console) PrintOrError(cl Color, str string)

PrintOrError prints given text or prints a generic error if the text is empty.

func (*Console) Printf

func (cn *Console) Printf(cl Color, format string, args ...interface{})

Printf exposes underlying Printer.

func (*Console) Register

func (cn *Console) Register(c *Command)

Register a command handler for this console.

func (*Console) SetCheats

func (cn *Console) SetCheats(enabled bool)

SetCheats enables or disables cheats on this console.

func (*Console) SetExec

func (cn *Console) SetExec(exec ExecFunc)

SetExec sets a custom exec function for macros.

func (*Console) SetMacros

func (cn *Console) SetMacros(enabled bool)

SetMacros enables or disables macros on this console.

func (*Console) Strings

func (cn *Console) Strings() *strman.StringManager

Strings exposes the underlying string manager.

func (*Console) UnBindKey

func (cn *Console) UnBindKey(k keybind.Key) bool

UnBindKey unbinds command from a key.

func (*Console) UnBindKeyByName

func (cn *Console) UnBindKeyByName(name string) bool

UnBindKeyByName unbinds command from a key with a given alias.

type ExecFunc

type ExecFunc func(ctx context.Context, cmd string) bool

type Flags

type Flags uint

Flags represents console command flags.

func (Flags) Has

func (f Flags) Has(v Flags) bool

type LocalizedPrinter

type LocalizedPrinter interface {
	Strings() *strman.StringManager
	Printer
}

LocalizedPrinter is an interface used for command output.

type MultiPrinter

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

func NewMultiPrinter

func NewMultiPrinter(list ...Printer) *MultiPrinter

NewMultiPrinter prints into multiple printers at once.

func (*MultiPrinter) Add

func (p *MultiPrinter) Add(p2 Printer)

func (*MultiPrinter) Print

func (p *MultiPrinter) Print(cl Color, str string)

func (*MultiPrinter) Printf

func (p *MultiPrinter) Printf(cl Color, format string, args ...interface{})

type Printer

type Printer interface {
	Print(cl Color, str string)
	Printf(cl Color, format string, args ...interface{})
}

Printer is an interface used for command output.

func LogPrinter

func LogPrinter(l *log.Logger) Printer

LogPrinter returns a printer that writes to a given logger.

Jump to

Keyboard shortcuts

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