User settings example

User settings system loads and saves registered consumer state transparently to application. Consumers are menu items, widgets and windows, and Ano script variables. Settings database is located in user home directory, either .config or .local subdirectory. If none of those two directories exists, application user home directory is used.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh user_settings

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; -------------------------------------------------------------------------------
; user_settings.ano
;
; To compile:
;
; $ ./build/ano ./examples/user_settings/user_settings.ano > \
;     engine/dsl_ano.h
; -------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		user_settings
@ANO_SCRIPT_VERSION		0.0.1
@ANO_SCRIPT_DESCRIPTION	Simple settings/variable state saver
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; User settings system loads and saves registered consumer state transparently
; to application. Consumers are menu items, widgets and windows, and Ano script
; variables. Settings database is located in user home directory, either
; .config or .local subdirectory. If none of those two directories exists,
; application user home directory is used.
;

main [exit: 0] {
	; Run this program multiple times to see variable increasing its
	; value in each run. You may want to delete user_settings.(debug).rc
	; settings file from either ˜/.config, ˜/.local or your home directory
	; afterwards. After each run, variable value can also be seen by using
	; --settings command line switch.
	;

	; Register settings for my_var variable to load and save it
	; automatically.
	;
	settings_register (\
		consumer_name: "my_var", \
		consumer_desc: "my_var value storage", \
		consumer_class: SETTINGS_CUSTOM)

	; Initialize variable, its initial value is fetched from settings.
	;
	var [number] my_var

	; Print it out and increase by one, its value and type is saved
	; automatically back to settings when program terminates.
	;
	print "Saved variable value is: " . my_var . ".\nStart program again" \
		" to see it increasing.\n"

	inc my_var
}
; ------------------------------------------------------------------------------
; user_settings.w2c
;
; To compile:
;
; $ ./build/widget ./examples/user_settings/user_settings.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; user_settings.m2c
;
; To compile:
;
; $ ./build/menu ./examples/user_settings/user_settings.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.