prefs

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package prefs facilitates the storage of preferential values in the Gopher2600 system. It is a key/value system and is quite flexible. The most significant feature is the ability to only specify the preferences you are interested in for a given task. Loading and saving a preference will ignore and preserve values that haven't been added to the "Disk" type.

The Disk type is the key resource in the package. First, create new a Disk instance with NewDisk(), specifying the location of the preferences file (using resources.ResourcePath() as necessary).

fn, _ := resources.ResourcePath(prefs.DefaultPrefsFile)
prf := prefs.NewDisk(fn)

Preference value can then be added with the Add() function, specifying the "key" value. For clarity the key value should be similar to the name of the variable as used in the calling code, although this isn't required (note the restrictions on the key value in the documtation for the Add() function).

prf.Add("auto", &auto)

Values that are added to the Disk type must use one of the preference types defined in this package. Currently, there are Bool, String and Int types and also a Generic type (unreleated to Go "generics").

Once added to the disk object, preference values can be changed in the program in the normal way. Changes can be committed to disk with the Disk.Save() function and restored with Disk.Load().

Prefs valus and Multiple Disk Instances

A prefs values can be added to more than one disk intance at once. Moreover, more than disk instance can point to the same file on disk.

This is useful when only wanting to save a subset of prefs values. The values that are to be saved can be allocated to a limited disk object and be saved. Values that exist on the physical disk but which are missing from the limited disk object will be preserved.

Concurrency

Generally, it is safe to access a prefs value from any goroutine. However, Set() should be used with care *if* SetHookPost() or SetHookPre() has been set for that value.

Command Line Support

The *CommandLine*() functions are designed to help with the overriding of disk values with a value given on the command line. These values are added as a group with AddCommandLineGroup(). For example

AddCommandLineGroup("foo::bar; baz::qux")

(see below for more detail about the format of the prefs string)

These values are then looked up when preferences are first loaded by an instance of the Disk type. The command line value will be used instead of the saved value for the first load only. If Load() is never called then the command line value will not be used -- this shouldn't be an issue because it is good practice for Load() to always be called after the a set of prefs values have been added with the Add() function

Commandline groups can be nested. Subsequent calls to AddCommandLineGroup() will hide the previous group until EndCommandLineGroup() is called.

The prefs string used with AddCommandLineGroup() mirror the format of the preferences file except that entries are separated by semi-colons instead of newlines.

For example, if the preferences file has the following entries:

a.b.c :: 100
d.e.f.g :: false
h.i :: wibble

A valid string to use with AddCommandLineGroup() might be:

a.b.c::100; h.i::wibble

Leading and trailing spaces around the key and value are stripped.

Note

While saved preference files are stored in UTF-8 it is not a good idea for the files to be edited by hand. As such, manual editing is discorouaged with the warning at the top of the file:

*** do not edit this file by hand ***

This also serves as the means of identification for the Load() function. ie. if this warning is not present then the Load() operation will fail.

Index

Constants

View Source
const DefaultPrefsFile = "preferences"

DefaultPrefsFile is the default filename of the global preferences file.

View Source
const GenericGetValueUndefined = "GenericGetValueUndefined"

GenericGetValueUndefined is a special return value for the get() function (see NewGeneric()). It indicates that the value is currently unavailable and the most recent previous value should be used.

View Source
const KeySep = " :: "

the string the separators the pref key from the value.

View Source
const WarningBoilerPlate = "*** do not edit this file by hand ***"

WarningBoilerPlate is inserted at the beginning of a preferences file.

Variables

View Source
var DisableSaving = false

DisableSaving is useful for testing when a blanket prohibition on saving to disk is required.

Functions

func PopCommandLineStack added in v0.16.0

func PopCommandLineStack() string

PopCommandLineStack forgets the most recent group added by AddCommandLineGroup().

Returns the "unused" preferences of the stack entry.

func PushCommandLineStack added in v0.16.0

func PushCommandLineStack(prefs string)

PushCommandLineStack parses a command line and adds it as a new group.

func SizeCommandLineStack added in v0.16.0

func SizeCommandLineStack() int

SizeCommandLineStack returns the number of groups that have been added with AddCommanLineGroup().

Types

type Bool

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

Bool implements a boolean type in the prefs system.

func (*Bool) Get

func (p *Bool) Get() Value

Get returns the raw pref value.

func (*Bool) Reset added in v0.7.1

func (p *Bool) Reset() error

Reset sets the boolean value to false.

func (*Bool) Set

func (p *Bool) Set(v Value) error

Set new value to Bool type. New value must be of type bool or string. A string value of anything other than "true" (case insensitive) will set the value to false.

func (*Bool) SetHookPost added in v0.15.0

func (p *Bool) SetHookPost(f func(value Value) error)

SetHookPost sets the callback function to be called just after the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Bool) SetHookPre added in v0.15.0

func (p *Bool) SetHookPre(f func(value Value) error)

SetHookPre sets the callback function to be called just before the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Bool) String

func (p *Bool) String() string

type Disk

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

Disk represents preference values as stored on disk.

func NewDisk

func NewDisk(path string) (*Disk, error)

NewDisk is the preferred method of initialisation for the Disk type.

func (*Disk) Add

func (dsk *Disk) Add(key string, p pref) error

Add preference value to list of values to store/load from Disk. The key value is used to identify the preference value on disk and should only consist of alphabetic characters or the period character. The program will panic if these constraints are not met.

func (*Disk) DoesNotHaveEntry added in v0.17.0

func (dsk *Disk) DoesNotHaveEntry(entry string) (bool, error)

DoesNotHaveEntry returns true if entry is missing from the written disk.

func (*Disk) Load

func (dsk *Disk) Load(saveOnFirstUse bool) error

Load preference values from disk. The saveonFirstUse argument is useful when loading preferences on initialisation. It makes sure default preferences are saved to disk if they are not present in the preferences file.

func (*Disk) Reset added in v0.7.1

func (dsk *Disk) Reset() error

Reset all preferences to the default values. In other words, the value they would have on first use before being set.

func (*Disk) Save

func (dsk *Disk) Save() (rerr error)

Save current preference values to disk.

func (Disk) String

func (dsk Disk) String() string

type Float added in v0.7.1

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

Int implements a string type in the prefs system.

func (*Float) Get added in v0.7.1

func (p *Float) Get() Value

Get returns the raw pref value.

func (*Float) Reset added in v0.7.1

func (p *Float) Reset() error

Reset sets the int value to zero.

func (*Float) Set added in v0.7.1

func (p *Float) Set(v Value) error

Set new value to Int type. New value can be an int or string.

func (*Float) SetHookPost added in v0.15.0

func (p *Float) SetHookPost(f func(value Value) error)

SetHookPost sets the callback function to be called just after the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Float) SetHookPre added in v0.15.0

func (p *Float) SetHookPre(f func(value Value) error)

SetHookPre sets the callback function to be called just before the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Float) String added in v0.7.1

func (p *Float) String() string

type Generic

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

Generic is a general purpose prefererences type, useful for values that cannot be represented by a single live value. You must use the NewGeneric() function to initialise a new instance of Generic.

The Generic prefs type does not have a way of registering a callback function. It is also slower than other prefs types because it must protect potential critical sections with a mutex (other types can use an atomic value).

func NewGeneric

func NewGeneric(set func(Value) error, get func() Value) *Generic

NewGeneric is the preferred method of initialisation for the Generic type.

func (*Generic) Get

func (p *Generic) Get() Value

Get triggers the get value procedure for the generic type.

func (*Generic) Reset added in v0.7.1

func (p *Generic) Reset() error

Reset sets the generic value to the empty string.

func (*Generic) Set

func (p *Generic) Set(v Value) error

Set triggers the set value procedure for the generic type.

func (*Generic) String

func (p *Generic) String() string

type Int

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

Int implements a string type in the prefs system.

func (*Int) Get

func (p *Int) Get() Value

Get returns the raw pref value.

func (*Int) Reset added in v0.7.1

func (p *Int) Reset() error

Reset sets the int value to zero.

func (*Int) Set

func (p *Int) Set(v Value) error

Set new value to Int type. New value can be an int or string.

func (*Int) SetHookPost added in v0.15.0

func (p *Int) SetHookPost(f func(value Value) error)

SetHookPost sets the callback function to be called just after the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Int) SetHookPre added in v0.15.0

func (p *Int) SetHookPre(f func(value Value) error)

SetHookPre sets the callback function to be called just before the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*Int) String

func (p *Int) String() string

type String

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

String implements a string type in the prefs system.

func (*String) Get

func (p *String) Get() Value

Get returns the raw pref value.

func (*String) Reset added in v0.7.1

func (p *String) Reset() error

Reset sets the string value to the empty string.

func (*String) Set

func (p *String) Set(v Value) error

Set new value to String type. New value must be of type string.

func (*String) SetHookPost added in v0.15.0

func (p *String) SetHookPost(f func(value Value) error)

SetHookPost sets the callback function to be called just after the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*String) SetHookPre added in v0.15.0

func (p *String) SetHookPre(f func(value Value) error)

SetHookPre sets the callback function to be called just before the prefs value is updated. Note that even if the value hasn't changed, the callback will be executed.

Not required but is useful in some contexts.

func (*String) SetMaxLen added in v0.7.1

func (p *String) SetMaxLen(max int)

SetMaxLen sets the maximum length for a string when it is set. To set no limit use a value less than or equal to zero. Note that the existing string will be cropped if necessary - cropped string information will be lost.

func (*String) String

func (p *String) String() string

type Value added in v0.7.1

type Value interface{}

Value represents the actual Go preference value.

func GetCommandLinePref added in v0.16.0

func GetCommandLinePref(key string) (bool, Value)

GetCommandLinePref value from current group. The value is deleted when it is returned.

Jump to

Keyboard shortcuts

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