util

package
v5.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package util provides miscellaneous utility functions that don't deserve their own package.

Index

Constants

View Source
const (
	DefaultFGColorDark      = "#aaaaaa"
	DefaultFGColorLight     = "#000000"
	DefaultBGColorDark      = "#232323"
	DefaultBGColorLight     = "#cccccc"
	DefaultCheckSelectDark  = "#ffffff"
	DefaultCheckSelectLight = "#000000"
	DefaultCheckMenuDark    = "#ffffff"
	DefaultCheckMenuLight   = "#000000"
	DefaultBrightFGDark     = "#ffffff"
	DefaultBrightFGLight    = "#000000"
	DefaultGridDark         = "#aaaaaa"
	DefaultGridLight        = "blue"
	DefaultGridMajorDark    = "#345f12"
	DefaultGridMajorLight   = "#345f12"
	DefaultGridMinorDark    = "#b00b03"
	DefaultGridMinorLight   = "#b00b03"
	DefaultHandColorLight   = "black"
	DefaultHandColorDark    = "#aaaaaa"
	DefaultTickColorLight   = "blue"
	DefaultTickColorDark    = "#aaaaaa"
	DefaultFListFGLight     = "black"
	DefaultFListFGDark      = "white"
	DefaultFListBGLight     = "white"
	DefaultFListBGDark      = "#232323"
	DefaultNextFGLight      = "white"
	DefaultNextFGDark       = "white"
	DefaultNextBGLight      = "black"
	DefaultNextBGDark       = "#cc0000"
	DefaultCurBGLight       = "#9cffb4"
	DefaultCurBGDark        = "#003300"
	DefaultReadyBGLight     = "#ff3333"
	DefaultReadyBGDark      = "#ff0000"
	DefaultHoldBGLight      = "#ffaaaa"
	DefaultHoldBGDark       = "#610400"
	DefaultZeroHPLight      = "#ff0000"
	DefaultZeroHPDark       = "#ff0000"
	DefaultNegativeHPLight  = "#000000"
	DefaultNegativeHPDark   = "#000000"
	DefaultSlotFGLight      = "#000000"
	DefaultSlotFGDark       = "#666666"
	DefaultSlotBGLight      = "#666666"
	DefaultSlotBGDark       = "#232323"
	DefaultFlatFootedLight  = "#3333ff"
	DefaultFlatFootedDark   = "#3333ff"
	DefaultPresetNameLight  = "blue"
	DefaultPresetNameDark   = "cyan"
)

Variables

This section is empty.

Functions

func FancyFileName

func FancyFileName(path string, extras map[byte]string) (string, error)

FancyFileName expands tokens found in the path string to allow the user to specify dynamically-named files at runtime. If there's a problem with the formatting, an error is returned along with the original path.

The tokens which may appear in the path include the following (note that all of these are modified as appropriate to the locale's national conventions and language):

%A   full weekday name
%a   abbreviated weekday name
%B   full month name
%b   abbreviated month name
%C   zero-padded two-digit year 00-99
%c   time and date
%d   day of month as number 01-31 (zero padded)
%e   day of month as number  1-31 (space padded)
%F   == %Y-%m-%d
%H   hour as number 00-23 (zero padded)
%h   abbreviated month name (same as %b)
%I   hour as number 01-12 (zero padded)
%j   day of year as number 001-366
%k   hour as number  0-23 (space padded)
%L   milliseconds as number 000-999
%l   hour as number  1-12 (space padded)
%M   minute as number 00-59
%m   month as number 01-12
%P   process ID
%p   AM or PM
%R   == %H:%M
%r   == %I:%M:%S %p
%S   second as number 00-60
%s   Unix timestamp as a number
%T   == %H:%M:%S
%U   week of the year as number 00-53 (Sunday as first day of week)
%u   weekday as number (1=Monday .. 7=Sunday)
%V   week of the year as number 00-53 (Monday as first day of week)
%v   == %e-%b-%Y
%W   week of the year as number 00-53 (Monday as first day of week)
%w   weekday as number (0=Sunday .. 6=Saturday)
%X   time
%x   date
%Y   full year
%y   two-digit year (00-99)
%Z   time zone name
%z   time zone offset from UTC
%µ   microseconds as number 000-999
%%   literal % character

The extras parameter maps token names to static string values, so more tokens specific to the task at hand can be added, such as these that the mapper client adds:

%G   "GM" if logged in as the GM, otherwise ""
%N   username
%n   module name

func Hexdump

func Hexdump(data []byte, opts ...func(*hdopt)) string

Hexdump takes an array of bytes and returns a multi-line string representing those bytes in a traditional hexdump format with an address field on the left, starting at address 0, showing 16 bytes per line, and a text bar along the right showing any printable ASCII characters found in the hexdump.

For example, calling

Hexdump([]byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG"))

will return the string

00000000:  00 81 02 03 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64  |....Hello, World|
00000010:  E2 84 A2 3C 3E 41 42 43 44 45 46 47              |...<>ABCDEFG    |

Options may be added after the data slice to control how the hex dump will be formatted: WithStartingAddress(addr), WithWidth(nbytes), WithWordSize(nbytes), and/or WithoutText.

func OverrideBool

func OverrideBool(key string, value bool) func(SimpleConfigurationData) error

if the supplied value is true, update the config data to be true.

func OverrideBoolWithNegation

func OverrideBoolWithNegation(key string, value bool, neg bool) func(SimpleConfigurationData) error

if the supplied value is true, update the config data to be true. if the supplied negation value is true, update the config data to be false.

func OverrideInt

func OverrideInt(key string, value int) func(SimpleConfigurationData) error

if the supplied value is nonzero, update the config data to match.

func OverrideString

func OverrideString(key string, value string) func(SimpleConfigurationData) error

if the supplied value is nonempty, update the config data to match.

func PluralizeCustom

func PluralizeCustom(base, singularSuffix, pluralSuffix string, qty int) string

PluralizeCustom emits a properly-pluralized version of a string, where that is more complicated than just adding an "s" to the end.

func PluralizeString

func PluralizeString(base string, qty int) string

PluralizeString emits a properly-pluralized version of a string, by adding an "s" for quantities other than one.

func SearchInPath added in v5.3.0

func SearchInPath(program string) (string, error)

SearchInPath looks for an executable program name by searching the user's execution path ($PATH environment variable)

func UpdateSimpleConfig

func UpdateSimpleConfig(inputFile io.Reader, data SimpleConfigurationData) error

UpdateSimpleConfig reads a configuration file as described for ParseSimpleConfig, but instead of creating a new set of config data, it updates an existing data set.

func VersionCompare

func VersionCompare(a, b string) (int, error)

VersionCompare compares version strings a and b. These strings must consist of integers separated with dots, such as "2" or "3.1". Any number of version levels are allowed, although generally only 2 or 3 are of practical use.

Returns <0 if a is a version before b, >0 if a is after b, or zero if they are the same.

As of version 5.0.0, this is simply a wrapper to the hashicorp go-version package.

func WithStartingAddress

func WithStartingAddress(a int) func(*hdopt)

WithStartingAddress may be added as an option to the Hexdump function to change the starting address of the data being shown.

Example:

data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG")
Hexdump(data, WithStartingAddress(0x4444))

will return the string

00004444:  00 81 02 03 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64  |....Hello, World|
00004454:  E2 84 A2 3C 3E 41 42 43 44 45 46 47              |...<>ABCDEFG    |

func WithWidth

func WithWidth(w int) func(*hdopt)

WithWidth may be added as an option to the Hexdump function to change the output width in bytes.

The behavior is undefined if the width is not a multiple of the word size.

Example:

data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG")
Hexdump(data, WithWidth(8), WithStartingAddress(0x4444))

will return the string

00004444:  00 81 02 03 48 65 6C 6C  |....Hell|
0000444C:  6F 2C 20 57 6F 72 6C 64  |o, World|
00004454:  E2 84 A2 3C 3E 41 42 43  |...<>ABC|
0000445C:  44 45 46 47              |DEFG    |

func WithWordSize

func WithWordSize(w int) func(*hdopt)

WithWordSize may be added as an option to the Hexdump function to change the output word size in bytes.

Example:

data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG")
Hexdump(data, WithWordSize(2))

will return the string

00000000:  0081 0203 4865 6C6C 6F2C 2057 6F72 6C64  |....Hello, World|
00000010:  E284 A23C 3E41 4243 4445 4647            |...<>ABCDEFG    |

func WithoutText

func WithoutText(o *hdopt)

WithoutText may be added as an option to the Hexdump function to suppress the text column from the generated display.

Example:

data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG")
Hexdump(data, WithWordSize(2), WithoutText)

will return the string

00000000:  0081 0203 4865 6C6C 6F2C 2057 6F72 6C64
00000010:  E284 A23C 3E41 4243 4445 4647

Types

type ButtonSize added in v5.3.0

type ButtonSize byte

ButtonSize represents the valid sizes for buttons to be.

const (
	SmallButtons ButtonSize = iota
	MediumButtons
	LargeButtons
)

func (*ButtonSize) MarshalJSON added in v5.3.0

func (bs *ButtonSize) MarshalJSON() ([]byte, error)

func (*ButtonSize) UnmarshalJSON added in v5.3.0

func (bs *ButtonSize) UnmarshalJSON(b []byte) error

type ClockStyles added in v5.3.0

type ClockStyles struct {
	HandColor       ColorSet `json:"hand_color,omitempty"`
	TickColor       ColorSet `json:"tick_color,omitempty"`
	FListFGColor    ColorSet `json:"flist_fg,omitempty"`
	FListBGColor    ColorSet `json:"flist_bg,omitempty"`
	NextFGColor     ColorSet `json:"next_fg,omitempty"`
	NextBGColor     ColorSet `json:"next_bg,omitempty"`
	CurrentBGColor  ColorSet `json:"cur_bg,omitempty"`
	ReadyBGColor    ColorSet `json:"ready_bg,omitempty"`
	HoldBGColor     ColorSet `json:"hold_bg,omitempty"`
	ZeroHPColor     ColorSet `json:"zero_hp,omitempty"`
	NegativeHPColor ColorSet `json:"negative_hp,omitempty"`
	SlotFGColor     ColorSet `json:"slot_fg,omitempty"`
	SlotBGColor     ColorSet `json:"slot_bg,omitempty"`
	FlatFootedColor ColorSet `json:"flat_footed,omitempty"`
	TimeDispFont    string   `json:"timedisp_font,omitempty"`
	TurnDispFont    string   `json:"turndisp_font,omitempty"`
	DefaultFont     string   `json:"default_font,omitempty"`
}

type ColorSet added in v5.3.0

type ColorSet struct {
	Dark  string `json:"dark,omitempty"`
	Light string `json:"light,omitempty"`
}

ColorSet encapsulates the colors to use in light and dark mode.

type DialogStyles added in v5.3.0

type DialogStyles struct {
	HeadingFG        ColorSet `json:"heading_fg,omitempty"`
	NormalFG         ColorSet `json:"normal_fg,omitempty"`
	NormalBG         ColorSet `json:"normal_bg,omitempty"`
	HighlightFG      ColorSet `json:"highlight_fg,omitempty"`
	OddRowBG         ColorSet `json:"odd_bg,omitempty"`
	EvenRowBG        ColorSet `json:"even_bg,omitempty"`
	CheckSelectColor ColorSet `json:"check_select,omitempty"`
	CheckMenuColor   ColorSet `json:"check_menu,omitempty"`
	BrightFG         ColorSet `json:"bright_fg,omitempty"`
	GridColor        ColorSet `json:"grid,omitempty"`
	MinorGridColor   ColorSet `json:"grid_minor,omitempty"`
	MajorGridColor   ColorSet `json:"grid_major,omitempty"`
	PresetNameColor  ColorSet `json:"preset_name,omitempty"`
}

type DieRollComponent added in v5.3.0

type DieRollComponent struct {
	FG             ColorSet `json:"fg,omitempty"`
	BG             ColorSet `json:"bg,omitempty"`
	FontName       string   `json:"font,omitempty"`
	Format         string   `json:"format,omitempty"`
	Overstrike     bool     `json:"overstrike,omitempty"`
	Underline      bool     `json:"underline,omitempty"`
	BaselineOffset int      `json:"offset,omitempty"`
}

DieRollComponent describes the settings for a specific chat or die-roll style component.

type DieRollStyles added in v5.3.0

type DieRollStyles struct {
	CompactRecents bool                        `json:"compact_recents,omitempty"`
	Components     map[string]DieRollComponent `json:"components,omitempty"`
}

type FontSlant added in v5.3.0

type FontSlant byte

FontSlant is the set of valid font slant values

const (
	Roman FontSlant = iota
	Italic
)

type FontWeight added in v5.3.0

type FontWeight byte

FontWeight is the set of valid font weight values

const (
	Regular FontWeight = iota
	Bold
)

type GridGuide added in v5.3.0

type GridGuide struct {
	Interval int         `json:"interval,omitempty"`
	Offsets  GridOffsets `json:"offsets,omitempty"`
}

GridGuide describes extra grid guidelines

type GridOffsets added in v5.3.0

type GridOffsets struct {
	X int `json:"x,omitempty"`
	Y int `json:"y,omitempty"`
}

GridOffsets provide x and y offsets for grid guides

type ImageType added in v5.3.0

type ImageType byte

ImageType represents the valid bitmap types supported by the mapper.

const (
	PNG ImageType = iota
	GIF
)

func (*ImageType) MarshalJSON added in v5.3.0

func (i *ImageType) MarshalJSON() ([]byte, error)

func (*ImageType) UnmarshalJSON added in v5.3.0

func (i *ImageType) UnmarshalJSON(b []byte) error

type ServerProfile added in v5.3.0

type ServerProfile struct {
	Name         string `json:"name"`
	Host         string `json:"host,omitempty"`
	Port         int    `json:"port,omitempty"`
	UserName     string `json:"username,omitempty"`
	Password     string `json:"password,omitempty"`
	CurlProxy    string `json:"curl_proxy,omitempty"`
	BlurAll      bool   `json:"blur_all,omitempty"`
	BlurPct      int    `json:"blur_pct,omitempty"`
	SuppressChat bool   `json:"suppress_chat,omitempty"`
	ChatLimit    int    `json:"chat_limit,omitempty"`
	ChatLog      string `json:"chat_log,omitempty"`
	CurlServer   string `json:"curl_server,omitempty"`
	UpdateURL    string `json:"update_url,omitempty"`
	ModuleID     string `json:"module_id,omitempty"`
	ServerMkdir  string `json:"server_mkdir,omitempty"`
	NcPath       string `json:"nc_path,omitempty"`
	ScpPath      string `json:"scp_path,omitempty"`
	ScpDest      string `json:"scp_dest,omitempty"`
	ScpServer    string `json:"scp_server,omitempty"`
	ScpProxy     string `json:"scp_proxy,omitempty"`
	SshPath      string `json:"ssh_path,omitempty"`
}

ServerProfile describes each set of preferences associated with a given server as opposed to global settings which apply regardless of server.

type SimpleConfigurationData

type SimpleConfigurationData map[string]string

func NewSimpleConfigurationData

func NewSimpleConfigurationData() SimpleConfigurationData

NewSimpleConfigurationData creates a ready-to-use SampleConfigurationData value which you can call Set, et al. directly without having read in a configuration from a file first.

func ParseSimpleConfig

func ParseSimpleConfig(inputFile io.Reader) (SimpleConfigurationData, error)

ParseSimpleConfig parses a minimal configuration file format used by the mapper that isn't a full INI file. Rather, it's a simple "key=value" collection with one entry per line in the file. The key must be alphanumeric (including underscores and hyphens), while the value may include any characters. Spaces before or after the key are ignored, as are spaces before or after the value.

A key alone on a line (without an = sign) indicates a boolean true value for that key.

Lines starting with a # sign (allowing for leading spaces before that) are ignored as comments.

func (SimpleConfigurationData) Get

func (c SimpleConfigurationData) Get(key string) (string, bool)

Get retrieves a string value from the configuration data. Returns the string value, or "" if the key does not exist, and a boolean indicating whether the value existed in the data.

func (SimpleConfigurationData) GetBool

func (c SimpleConfigurationData) GetBool(key string) (bool, error)

GetBool retrieves a boolean value from the configuration data. Returns an error if the value does not exist or could not be converted to a boolean.

This considers values "0", "false", "no", or "off" to be false, and non-zero integers, "true", "yes", or "on" to be true. Non-existent keys are considered to be false.

func (SimpleConfigurationData) GetBoolDefault

func (c SimpleConfigurationData) GetBoolDefault(key string, def bool) bool

func (SimpleConfigurationData) GetDefault

func (c SimpleConfigurationData) GetDefault(key, def string) (string, bool)

GetDefault retrieves a string value from the configuration data, or the supplied default value if no such key exists.

func (SimpleConfigurationData) GetInt

func (c SimpleConfigurationData) GetInt(key string) (int, error)

GetInt retrieves an integer value from the configuration data. Returns an error if the value does not exist or could not be converted to an integer.

func (SimpleConfigurationData) GetIntDefault

func (c SimpleConfigurationData) GetIntDefault(key string, def int) (int, error)

GetIntDefault retrieves an integer value from the configuration data. Returns an error if the value could not be converted to an integer, or the given default value if the key could not be found.

func (SimpleConfigurationData) Override

func (c SimpleConfigurationData) Override(opts ...func(SimpleConfigurationData) error) error

func (SimpleConfigurationData) Set

func (c SimpleConfigurationData) Set(key, value string)

Set adds a key/value pair to the SimpleConfigurationData receiver. If key already exists, it will be replaced with this new value.

func (SimpleConfigurationData) SetInt

func (c SimpleConfigurationData) SetInt(key string, value int)

type StyleDescription added in v5.3.0

type StyleDescription struct {
	Clocks   ClockStyles   `json:"clocks,omitempty"`
	Dialogs  DialogStyles  `json:"dialogs,omitempty"`
	DieRolls DieRollStyles `json:"dierolls,omitempty"`
}

StyleDescription describes the different kinds of style settings.

type UserFont added in v5.3.0

type UserFont struct {
	Family     string     `json:"family,omitempty"`
	Size       float64    `json:"size,omitempty"`
	Weight     FontWeight `json:"weight,omitempty"`
	Slant      FontSlant  `json:"slant,omitempty"`
	Overstrike bool       `json:"overstrike,omitempty"`
	Underline  string     `json:"underline,omitempty"`
}

UserFont describes a user-defined font.

type UserPreferences added in v5.3.0

type UserPreferences struct {
	GMAMapperPreferencesVersion int        `json:"GMA_Mapper_preferences_version"`
	Animate                     bool       `json:"animate,omitempty"`
	ButtonSize                  ButtonSize `json:"button_size,omitempty"`
	CurlPath                    string     `json:"curl_path,omitempty"`
	CurrentProfile              string     `json:"current_profile,omitempty"`
	DarkMode                    bool       `json:"dark,omitempty"`
	DebugLevel                  int        `json:"debug_level,omitempty"`
	DebugProtocol               bool       `json:"debug_proto,omitempty"`
	GuideLines                  struct {
		Major GridGuide `json:"major,omitempty"`
		Minor GridGuide `json:"minor,omitempty"`
	} `json:"guide_lines,omitempty"`
	ImageFormat   ImageType           `json:"image_format,omitempty"`
	KeepTools     bool                `json:"keep_tools,omitempty"`
	PreloadImages bool                `json:"preload,omitempty"`
	Profiles      []ServerProfile     `json:"profiles,omitempty"`
	Fonts         map[string]UserFont `json:"fonts,omitempty"`
	Styles        StyleDescription    `json:"styles,omitempty"`
}

UserPreferences represents the preferences settings for the GMA Mapper.

func DefaultPreferences added in v5.3.0

func DefaultPreferences() UserPreferences

DefaultPreferences returns a UserPreferences list with a reasonable set of default values.

func LoadPreferences added in v5.3.0

func LoadPreferences(stream io.Reader) (UserPreferences, error)

LoadPreferences reads a set of saved preferences from an io.Reader, returning a new UserPreferences value from that data. Any fields not specified in the input data will have zero values.

func LoadPreferencesWithDefaults added in v5.3.0

func LoadPreferencesWithDefaults(stream io.Reader) (UserPreferences, error)

LoadPreferencesWithDefaults reads a set of saved preferences from an open file or other io.Reader object. It provides default values for fields not specified in the input data.

func (*UserPreferences) Update added in v5.3.0

func (prefs *UserPreferences) Update(stream io.Reader) error

Update reads a set of saved preferences as LoadPreferences does, but rather than returning a new UserPreferences value, it updates the values of an existing UserPreferences value with the input data.

func (*UserPreferences) UpdateFromSimpleConfig added in v5.3.0

func (prefs *UserPreferences) UpdateFromSimpleConfig(profileName string, cfg SimpleConfigurationData) error

UpdateFromSimpleConfig updates the corresponding configuration values in a UserPreferences value from a set of key=value pairs read from a simple config file.

Jump to

Keyboard shortcuts

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