cmd

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface {
	// Do performs the action.
	// An error is returned if the action could not be performed.
	Do(modder world.Modder) error

	// Undo reverses its action to restore the environment to the previous state.
	// An error is returned if the action could not be successfully undone. The
	// environment may not be in the state as before in an error occurred.
	Undo(modder world.Modder) error
}

Command describes an action that can be performed, undone, and redone.

type Commander

type Commander interface {
	Queue(command Command)
}

Commander tries to execute the given command.

type List

type List []Command

List is a sequence of commands, which are executed as one step.

func (List) Do

func (list List) Do(modder world.Modder) error

Do performs the entries in the list in ascending order. If an entry returns an error, the iteration is aborted and that error is returned.

func (List) Undo

func (list List) Undo(modder world.Modder) error

Undo performs the entries in the list in descending order. If an entry returns an error, the iteration is aborted and that error is returned.

type Registry added in v1.6.0

type Registry interface {
	Commander
	Register(modifier ...TransactionModifier) error
}

Registry allows to build commands as transactions with modifier functions.

type Stack

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

Stack describes a list of commands. The stack allows to sequentially undo and redo stacked commands. It essentially stores two lists: a list of commands to undo, and another of commands to redo. Modifying stack functions will panic if they are called while already in use.

func (*Stack) CanRedo

func (stack *Stack) CanRedo() bool

CanRedo returns true if there is at least one more command that can be redone.

func (*Stack) CanUndo

func (stack *Stack) CanUndo() bool

CanUndo returns true if there is at least one more command that can be undone.

func (*Stack) Perform

func (stack *Stack) Perform(cmd Command, modder world.Modder) error

Perform executes the given command and puts it on the stack if the command was successful. This function also clears the list of commands to be redone.

func (*Stack) Redo

func (stack *Stack) Redo(modder world.Modder) error

Redo attempts to perform the next command on the redo list. If there is no further command to redo, nothing happens. An error is returned if the command failed. In this case, the stack is unchanged and a further attempt to redo will try the same command again.

func (*Stack) Undo

func (stack *Stack) Undo(modder world.Modder) error

Undo attempts to undo the previous command on the list. If there is no further command to undo, nothing happens. An error is returned if the command failed. In this case, the stack is unchanged and a further attempt to undo will try the same command again.

type Task added in v1.6.0

type Task func(modder world.Modder) error

Task represents a modification on given modder.

type Transaction added in v1.6.0

type Transaction struct {
	Name    string
	Forward []Task
	Reverse []Task
}

Transaction is a named list of forward and reverse tasks. Transaction provides execution of the tasks as a Command.

func (Transaction) Do added in v1.6.0

func (txn Transaction) Do(modder world.Modder) error

Do executes the forward tasks in series. The first task to return an error aborts the iteration. A TransactionError is returned in case of aborted iteration.

func (Transaction) Undo added in v1.6.0

func (txn Transaction) Undo(modder world.Modder) error

Undo executes the reverse tasks in series. The first task to return an error aborts the iteration. A TransactionError is returned in case of aborted iteration.

type TransactionBuilder added in v1.6.0

type TransactionBuilder struct {
	Commander Commander
	// contains filtered or unexported fields
}

TransactionBuilder provides a way to register a command at a commander with possibly nested actions.

func (*TransactionBuilder) Queue added in v1.6.0

func (builder *TransactionBuilder) Queue(command Command)

Queue implements the Commander interface to add given command to a currently built transaction. If there is no outer transaction being built, then the command is immediately forwarded to the commander. Avoid building a loop between TransactionBuilder instances, as this will result in a stack overflow.

func (*TransactionBuilder) Register added in v1.6.0

func (builder *TransactionBuilder) Register(modifier ...TransactionModifier) error

Register creates a new transaction based on given modifier.

type TransactionError added in v1.6.0

type TransactionError struct {
	// Name is that of the transaction, if it was set.
	Name string
	// Index identifies the list index of tasks.
	Index int
	// Nested is the error returned from the task. In case of nested commands, this may be another TransactionError.
	Nested error
}

TransactionError is returned from a transaction execution if a task failed.

func (TransactionError) Error added in v1.6.0

func (err TransactionError) Error() string

Error returns the path of the transaction task, combined with the nested error text.

func (TransactionError) Unwrap added in v1.6.0

func (err TransactionError) Unwrap() error

Unwrap returns the nested error, to support diving into nested errors.

type TransactionModifier added in v1.6.0

type TransactionModifier func(*Transaction) error

TransactionModifier is a function to change a transaction while it is being built.

func Forward added in v1.6.0

func Forward(task Task) TransactionModifier

Forward creates a modifier for a task that is called when doing a command. If a nested command is added to the transaction after the forward function, then the nested task is performed after the given task. If a nested command is added to the transaction before the forward function, then the nested task is performed before the given task.

func Named added in v1.6.0

func Named(name string) TransactionModifier

Named allows to name the current level of transaction.

func Nested added in v1.6.0

func Nested(nested func() error) TransactionModifier

Nested adds a further level to the currently registered transaction. Depending on the order of the modifier in the register, the corresponding tasks will be called in sequence. See other modifier.

func Reverse added in v1.6.0

func Reverse(task Task) TransactionModifier

Reverse creates a modifier for a task that is called when undoing a command. If a nested command is added to the transaction after the reverse function, then the nested task is performed before the given task. If a nested command is added to the transaction before the reverse function, then the nested task is performed after the given task.

Jump to

Keyboard shortcuts

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