The top function manages several sub-functions which set visual properties over a list of Options.
Inside stylerules the function deconstruct Extract-s parts of a Rule and calls string or symbol for applying appropriate Style-s then reconstructs them.
A whole set of Rule-s is processed by Map-ping deconstruct onto set. Style properties are generated or selected based on user-input for stylerules[set_List, keystyle_, valstyle_, strstyle_] (option rules, keys style, values style, strings style ). For any style argument, user may input Automatic vs True vs any custom Style properties in a List vs None or False (last two default to generic Style-s)
I'm having difficulties with the output. Style-s are not evaluating, which also prevents the deconstructed Rules from reconstructing.
Don't worry, the code below looks a lot worse than it is. I promise that it is very easy to follow. I believe the problem areas to focus on are the inner functions string and symbol which may be scoped wrongly. I tried other scopes which did not help, including dropping initialized variables and constants into the main body. Please help. Thank you.
To reiterate:
Why are Style-s not evaluating and how to remedy this.
I just noticed that some strange out-of-place
Null-s are also appearing in the resulting rules. I don't know why.
(* PrettyOptions 0.7 Prototype 1 Runs - 2 *)
ClearSystemCache[];
ClearAll[set, stylerules];
(* the Echo's are temporary *)
stylerules[set_List, keystyle_, valstyle_, strstyle_] := With[{
rules = {x_ /;
MatchQ[x, Automatic | True | False | None | Null]} ->
Nothing,
keys =
DeleteDuplicates[
Flatten[Join[{Bold,
FontFamily -> "Courier New"}, {keystyle}]] //. rules],
vals =
DeleteDuplicates[
Flatten[Join[{Plain, FontFamily -> "Arial"}, {valstyle}]] //.
rules],
strs =
DeleteDuplicates[
Flatten[Join[{Bold, FontFamily -> "Arial"}, {strstyle}]] //.
rules]
},
Echo[keys, "keys [Rule]"];
Echo[vals, "vals [Rule]"];
Echo[strs, "strs [Rule]"];
deconstruct[rule_, keysty_, valsty_, strsty_] := With[{
nbcol = Options[EvaluationNotebook[], FontColor],
comcol = RGBColor[.75, 1, .75],
altcol = Lighter[LightBlue],
a = Extract[rule, {1}],
b = Extract[rule, {2}],
fn = Head[rule]
},
x =
If[StringQ[a], string[a, strs, strsty], symbol[a, keys, keysty]];
y =
If[StringQ[b], string[b, strs, strsty], symbol[b, vals, valsty]];
Return[fn[x, y]];
];
symbol[sym_, ops_, style_] :=
If[(ListQ[style] || ColorQ[style]), Style[sym, ops],
Which[
MatchQ[style, Automatic], Evaluate[Style[sym, comcol, ops]],
TrueQ[style], Style[sym, altcol, ops],
True, Style[sym, ops]
]
];
string[str_, ops_, style_] :=
If[(ListQ[style] || ColorQ[style]), Style[str, ops],
Which[
MatchQ[style, Automatic],
Evaluate[Style[InputForm[str], Gray, ops]],
TrueQ[style], Style[str, comcol, ops],
True, Style[str, ops]
];
];
Echo[Map[deconstruct[#, keystyle, valstyle, strstyle] &, set]]
];
stylerules[set_] := set;
(* arbritary data *)
set = Options[TestCases] = {TestCase1 :> 111111111,
TestCase2 :> "string222", "TestCase3" -> Symbol333,
TestCase4 :> Symbol444, "TestCase5" -> "string555"};
(* a few typical use cases, none worked *)
stylerules[set, Automatic, Automatic, Automatic];
stylerules[set, True, True, True];
stylerules[set, None, None, None];
stylerules[set, Automatic, True, Automatic];
(* this test case works *)
stylerules[
set, {Blue, FontFamily -> "Times New Roman"}, {Magenta,
FontFamily -> "Times New Roman"}, {Orange, Plain, Italic}];
With[{a=1,b=2a},{a,b}]doesn't do what you think it does, see e.g. this question for some context – Lukas Lang Dec 15 '22 at 19:30keys = ...line in yourstylerulesdefinition will not work as is, sincerulesis not replaced with the value from the line above. You'll have to use nestedWiths or similar, as discussed in the linked question. (SoWith[{rules = ...}, With[{keys = ...}, ... ]]– Lukas Lang Dec 15 '22 at 21:01