shortcut

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 12 Imported by: 0

README

shortcut

The shortcut package provides a Go library for defining and executing shortcuts.

Usage

First, create a new Map with shortcut.NewMap(). Then, use the Store method to define new shortcuts, and the Load method to retrieve them.

Example

package main

import (
	"fmt"

	"github.com/sunshineplan/shortcut"
)

func main() {
	m := shortcut.NewMap()
	m.Store("g", shortcut.Command("git", "%s"))
	m.Store("gi", shortcut.Command("git", "init"))
	m.Store("gs", shortcut.Command("git", "status"))

	if cmd, ok := m.Load("g"); ok {
		if err := cmd.Run("help"); err != nil {
			fmt.Println(err)
		}
	}

	if cmd, ok := m.Load("gs"); ok {
		if err := cmd.Run(); err != nil {
			fmt.Println(err)
		}
	}
}

This example creates a new shortcut.Map, adds three shortcuts to it, and executes two of them.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

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

Cmd is a shortcut command consisting of a name and arguments.

func Command

func Command(name string, n int, arg ...string) *Cmd

Command returns a new Cmd with the specified name and arguments. If the command fails to initialize, panic with the error.

func (Cmd) Args added in v1.0.1

func (c Cmd) Args() int

func (*Cmd) Env added in v1.0.1

func (c *Cmd) Env(env ...string)

Env sets the environment variables for the command. The environment variables are specified as a slice of strings, with each string being in the form of "key=value". If multiple values are specified for the same key, the last one takes precedence. If env is empty, the environment variables of the parent process are used.

func (Cmd) MarshalJSON

func (c Cmd) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation of a Cmd.

func (Cmd) Run

func (c Cmd) Run(a ...any) error

Run runs the command with the given arguments.

func (Cmd) RunContext

func (c Cmd) RunContext(ctx context.Context, a ...any) error

RunContext runs the command with the given context and arguments.

func (Cmd) String

func (c Cmd) String() string

String returns the command as a string.

func (*Cmd) UnmarshalJSON

func (c *Cmd) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the JSON representation of a Cmd. If the command fails to initialize, return an error.

type Cmds

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

Cmds is a list of commands.

func Commands

func Commands(cmd ...*Cmd) Cmds

Commands returns a new Cmds with the specified commands.

func (Cmds) Args added in v1.0.1

func (c Cmds) Args() (n int)

func (Cmds) MarshalJSON

func (c Cmds) MarshalJSON() ([]byte, error)

MarshalJSON marshals the list of commands to JSON. If the list has only one command, it marshals that command directly. Otherwise, it marshals the list of commands.

func (Cmds) Run

func (c Cmds) Run(a ...any) error

Run executes the list of commands with the given arguments.

func (Cmds) RunContext

func (c Cmds) RunContext(ctx context.Context, a ...any) error

RunContext executes the list of commands with the given context and arguments.

func (Cmds) String

func (c Cmds) String() string

String returns the string representation of the list of commands.

func (*Cmds) UnmarshalJSON

func (c *Cmds) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the JSON representation of a Cmds. If any command fails to initialize, return an error.

type Key

type Key string

type Map

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

Map is a shortcut map that associates Key with one or multiple Cmds.

func NewMap

func NewMap() *Map

NewMap creates and returns a new instance of Map.

func (*Map) Choose added in v1.0.2

func (m *Map) Choose() (bool, Key, Shortcut, error)

func (*Map) Count added in v1.0.2

func (m *Map) Count() int

func (*Map) Delete

func (m *Map) Delete(key Key)

Delete removes the Shortcut with the given key from the Map.

func (*Map) FromFile

func (m *Map) FromFile(file string) error

FromFile reads the content of the JSON file and populates the Map with the key-value pairs.

func (*Map) FromJSON

func (m *Map) FromJSON(b []byte) error

FromJSON unmarshals a JSON byte array and populates the Map with the key-value pairs.

func (*Map) Index added in v1.0.2

func (m *Map) Index(index int) (Key, Shortcut, error)

func (*Map) Load

func (m *Map) Load(key Key) (Shortcut, bool)

Load retrieves a Shortcut with the given key. It returns the Shortcut and true if the key exists. If the key does not exist, it returns nil and false.

func (*Map) Menu added in v1.0.2

func (m *Map) Menu(showQuit bool) string

func (*Map) Range

func (m *Map) Range(f func(Key, Shortcut) bool)

Range calls the given function for each key-value pair in the Map until the function returns false.

func (*Map) Store

func (m *Map) Store(key Key, cmd ...*Cmd)

Store stores a Key with one or multiple Cmds in the Map. If no command is provided, it panics. If only one command is provided, it is stored directly. If multiple commands are provided, they are wrapped as a Cmds object and stored.

type Shortcut

type Shortcut interface {
	Run(...any) error
	RunContext(context.Context, ...any) error
	Args() int
	String() string
}

Shortcut is an interface for defining a shortcut command.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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