View Single Post
Old 01-08-2023, 06:41 AM   #462
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,522
Karma: 8065528
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Thanks.

I converted one of my templates to get a better idea of how the switch_if works. Is this a good place to use one?

Code:
program:
	switch_if(
		'Cleanup' in virtual_libraries(), 'polish.png',
		'Fanfiction' in $#booktype, list_split(icons_fanfic(), ':', 'ff'); 
			ff_0,
		'Documentations & Manuals' in $#booktype, 'helpbook.png',
		'Loans' in virtual_libraries(), 'overdrive.png',
		'Physical Books' in virtual_libraries(), 'paperbook.png',
		approximate_formats()=='PDF', 'pdfbook.png',
		'omnibus' inlist $#admintags, 'bookshelf.png',
		'book_open.png',
	)
Yes, this is exactly the situation I envisioned for using switch_if().

Comment: you often use 'in' instead of 'inlist'. Using 'in' can lead to errors because it can find substrings. For example, if you have virtual libraries named "Loans" and "Temp Loans", the "in" will find them both, while inlist will find only the one as long as you use regular expression anchors on the search value.

Quote:
Originally Posted by ownedbycats View Post
Another example:

Code:
program:	
	if
		is_marked()
	then
		m = is_marked();
		switch_if(
			'reading_list_send_to_device' in m, 'sync.png',
			'reading_list_to_be_read' in m, 'list.png',
			'fff' in m, 'download-metadata.png',
			'marked.png'
		)
	fi
IIRC a book can have at most one mark. In this case you should use == instead of 'in', because == is much faster. You could also rewrite the if/then to get rid of it.

Example:
Code:
program:	
	m = is_marked();
	switch_if(
		'reading_list_send_to_device' == m, 'sync.png',
		'reading_list_to_be_read' == m, 'list.png',
		'fff' == m, 'download-metadata.png',
		m, 'marked.png',
		''
	)
chaley is offline   Reply With Quote