View Single Post
Old 08-11-2016, 05:01 AM   #2
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,475
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by readin View Post
METHOD 3 - Single advanced rule for column icons - PARTIALLY WORKS

Advanced Rule: Set <composed icons w/no text> for column <Icons>:
Code:
program:
	list_union(
		'',
		strcat(
			contains(field('#own'), 'p', 'book.png,', ''),
			test(field('series'), 'series.png', ''),
			contains(approximate_formats(), 'LNK', 'folder.png,', ''),
		), ',')
This rule sort of works: an icon is displayed if only one of the tests is true, but if two or more tests are true, no icons are displayed.
There are two errors in the template, one because I made a mistake in my suggestions and the other just because.

1) The template must return a colon-separated list, not a comma-separated list.
2) 'series.png' does not contain a separator.

The following template, adapted from yours to use a column and icons I have available, works.
Code:
program:
	list_union(
		'',
		strcat(
			contains(field('#enum'), 'two', 'add_book.png:', ''),
			test(field('series'), 'arrow-up.png:', ''),
			contains(approximate_formats(), 'epub', 'debug.png:', ''),
		), ':')
========================================
Quote:
METHOD 4 - User-defined template functions

As chaley has suggested (e.g. here), a custom template function would probably be the most efficient method, but I haven't been able to get very far.

For example:

1) Color all columns red if the book contains no ebook formats:

Preferences > Look and Feel > Template Functions:

name: has_ebook
arg count: 1
doc: has_ebook(val) -- evaluate whether 'val' (passed as 'approximate_formats()') has any of the given ebook formats, and return "true" or "false"
program code:
Code:
def evaluate(self, formatter, kwargs, mi, locals, val):
	contains(val, 'EPUB|MOBI|PDF', 'true', 'false')
Custom template functions are written in python, not in the template language. Python for a template function that does what I think you are asking for is
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	import re
	fmt_data = mi._proxy_metadata.db_approx_formats
	fmt_data = ','.join(v.upper() for v in fmt_data)
	return '' if re.search('EPUB|MOBI|PDF', fmt_data) is None else 'Yes'
You can get the python equivalents for built-in formatter functions by looking at their source code in the template editor or on this page.

You would call it like this:
Code:
program:
	has_ebook()
Using the template tester to debug the function(s) is faster than changing the template in a custom column. I have added it (the tester) to the right-click context menu for the main library.
chaley is offline   Reply With Quote