prefs

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2021 License: GPL-3.0, 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 Disk instance with NewDisk(), specifying the location of the preferences file (using paths.ResourcePath() as necessary).

fn, _ := paths.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. There is also a Generic type, that allows the Set() and Get() functions to be defined.

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

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 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

This section is empty.

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) RegisterCallback added in v0.3.1

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

RegisterCallback sets the callback function to be called when the value has changed. Not required but is useful in some contexts.

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) 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 id 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) HasEntry added in v0.7.1

func (dsk *Disk) HasEntry(key string) (bool, error)

HasEntry returns false if there is no matching entry on disk and true if there is.

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) RegisterCallback added in v0.7.1

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

RegisterCallback sets the callback function to be called when the value has changed. Not required but is useful in some contexts.

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) 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.

func NewGeneric

func NewGeneric(set func(string) error, get func() string) *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) RegisterCallback added in v0.3.1

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

RegisterCallback sets the callback function to be called when the value has changed. Not required but is useful in some contexts.

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) 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) RegisterCallback added in v0.3.1

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

RegisterCallback sets the callback function to be called when the value has changed. Not required but is useful in some contexts.

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) 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.

Jump to

Keyboard shortcuts

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