prefs

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: GPL-3.0, GPL-3.0 Imports: 8 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

This section is empty.

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() interface{}

Get returns the raw pref value

func (*Bool) Set

func (p *Bool) Set(v interface{}) 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) Load

func (dsk *Disk) Load() error

Load preference values from disk

func (*Disk) Save

func (dsk *Disk) Save() error

Save current preference values to disk

func (Disk) String

func (dsk Disk) 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.

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() interface{}

Get triggers the get value procedure for the generic type.

func (*Generic) Set

func (p *Generic) Set(v interface{}) 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() interface{}

Get returns the raw pref value

func (*Int) Set

func (p *Int) Set(v interface{}) 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() interface{}

Get returns the raw pref value

func (*String) Set

func (p *String) Set(v interface{}) error

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

func (String) String

func (p String) String() string

Jump to

Keyboard shortcuts

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