Thread: Custom Coloring
View Single Post
Old 07-02-2011, 05:38 AM   #83
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,449
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by mezme View Post
Okay... I've pretty much got the coloring working the way I want it... with a few "exceptions" that I cannot figure out. Help?
Nice to see someone attempting complicated stuff.
Quote:
Basically this is what I'm wanting to happen:
Code:
If rating = 5 then rating is silver
     else if format is false then rating is red
          else if comments are false then rating is fuchsia
               else if cover is false then rating is fuchsia
                    else rating is black (format=true / cover=true / comments=true)
First: given your specification (a series of elses), you don't want to use switch. That function looks inside the content of a single field (in your case, rating) and compares that contents to a series of test regular expressions, returning the value of the first regular expression that matches. As written, your code is checking the result of the outermost test against the content of rating. EDIT: The last sentence is wrong. Looking more carefully, I note that the test is the default of the switch, so it stands alone and is definitely a correct usage.

Instead of switch, you should use the more general iteration function 'first_non_empty', which is designed to implement a series of else clauses like you have. This function evaluates the arguments in sequence, stopping when one of them has a value.

Second: unfortunately, for performance reasons you cannot test for the presence of a cover in templates. Adding that possibility would slow calibre to an acceptable level. If you really want to test for the presence/absence of a cover, then you will need to create your own yes/no column and populate it using search+bulk metadata edit, refreshing it from time to time.

So, removing the 'cover' else, the following code implements the sequence of elses.
Code:
program:
first_non_empty(
	cmp(field('rating'), 5, '', 'silver', ''),
	test(field('formats'), '', 'red'),
	test(field('comments'), '', 'fuchsia'),
	'black'
)
The first argument to first_non_empty, cmp, returns 'silver' if rating == 5, otherwise '' (the empty value). The second argument returns 'red' of formats is empty, otherwise ''. The third argument returns 'fuschia' of comments is empty, otherwise ''. The last argument always returns 'black'.

Note that first_non_empty does not need to find a non-empty value. It will happily return '' if no argument returns a value. For coloring, the value '' means 'apply no color', which is sometimes what one wants.

You could also do this with a series of tests, but that is much harder to read because of the required nesting. Sub-tests would go into the false or true arm of the enclosing test. Makes my brain hurt to think about it.

Last edited by chaley; 07-02-2011 at 08:03 AM. Reason: correction of statement about switch
chaley is offline   Reply With Quote