0
$\begingroup$

First, let me illustrate what I'm trying to do. FormatOptions is a programming utility that will Echo a given Symbol-'s Options in a Column or a multicolumn Grid table (to be added later as an Option) so that they are Sort-ed (also to be added later) in some particular fashion. The goal is creating a tool that doesn't just spit out a raw list that is difficult to read. The only problem with it is that long Rule-s can misshapen the output. Therefore, it would be expedient to automate the Iconize step with a ReplaceAll (or one like it) Rule.

The code below works. As you can see I have one long string pre-Iconized. This works, but I would like to create an expression that will test every RHS Rule for example, StringLength[expr] >= 35, and Iconize them.

(* This works, but it's not automated *)

ClearSystemCache[];
ClearAll[FormatOptions, stage, rules];

Options[FormatOptions] = {"rawlist" -> True, "iconize" -> True};
FormatOptions[sym_Symbol, OptionsPattern[]] := Module[{
    nam = 
     Style[("Options[" <> (ToString[Evaluate[sym]]) <> "]"), Bold],
    rul = Evaluate[Options[sym]],
    lab, raw, ops
    },
   lab := 
    "raw list: " <> (ToString[Evaluate[Length[rul]]]) <> " " <> 
     "items";
   raw := If[OptionValue["iconize"], Iconize[rul, lab], rul];
   ops = If[OptionValue["rawlist"], AppendTo[rul, Evaluate[raw]], rul];
   Echo[Row[{nam, Style[" \[Rule] ", Orange], "{   ", Column[ops], 
      "  }"}]]
   ];


Options[stage] = {"backuprate" -> 2, 
   "source" -> Iconize[NotebookFileName[], NotebookFileName], 
   "makefolder" -> Automatic , "fileinfix" -> Automatic, 
   "fileappend" -> Automatic, "echoalerts" -> Automatic, 
   "soundalerts" -> Automatic, "volume" -> .25, "limitalerts" -> 3, 
   OverwriteTarget -> True, Options[CreateDirectory]};

FormatOptions[stage];

Below is my attempt at automating the Iconize step with a Replace-ment Rule. There are three strings in the rules that should have been Iconize-ed. Instead it drops the entire list, only returning one icon from the first long string it encounters. Also, please allow me to stress that the OptionValues in this example are not the same as the prior example.

Please help. Thank you.

(* almost slightly works *)

ClearSystemCache[];
ClearAll[stage, rules];

Options[stage] = {"backuprate" -> 2, "source" -> NotebookFileName[], 
   "makefolder" -> "this-folder-name-is-38-charscters-long", 
   "fileinfix" -> 
    "this-string-that-will-be-infixed-to-a-filename-is-55-charscters-\
long", "fileappend" -> "too-short-to-iconize-33-chracters", 
   "echoalerts" -> Automatic, "soundalerts" -> Automatic, 
   "volume" -> .25, "limitalerts" -> 3, OverwriteTarget -> True, 
   Options[CreateDirectory]};

FormatOptions[stage];
rules = Options[stage]
rules /. 
 KeyValuePattern[x_ -> y_String?(StringLength[#] >= 35 &)] -> 
  x -> Iconize[y, "String"]
$\endgroup$

1 Answer 1

1
$\begingroup$

You might start by figuring out the iconization step in isolation. You could try something like this:

IconizeLong[length_][Rule[key_, str_]] := 
  If[
    length <= StringLength[ToString@str], 
    key -> Iconize[str, key], 
    key -> str]

IconizeLong[5]["a" -> "abcde"](* should result in a iconized object*)
IconizeLong[5]["a" -> "abcd"](* should result in no change *)

Now let's assume you have options for stage:

IconizeLong[5] /@ Options[stage]

will give you a list with long items iconized. For formatting this nicely, it looks like you want a column:

Column[IconizeLong[5] /@ Options[stage]]

Now you want to add the other elements of the formatted options:

FormatOptions2[sym_Symbol, OptionsPattern[]] :=
  Row[
    {Style[("Options[" <> (SymbolName[sym]) <> "]"), Bold], 
     Style[" \[Rule] ", Orange], 
     Column[IconizeLong[5] /@ Options[sym]]}]

I haven't tested this with anything other than the stage options you provided, but it seems to work in that case at least:

FormatOptions2[stage]

Obviously you'd choose whatever length threshold you want, maybe even make that an argument to the formatting function. Also, I don't know if you want the key used as the icon label, but you'll need to come up with some way of setting that.

$\endgroup$
4
  • $\begingroup$ Don't get me wrong I really appreciate this, and I may end up using it, but I think I could have derived a very similar solution on my own. You see, this kind of problem feels a lot like lt should be a simple one-liner using a Replace-ment Rule. That's what I am looking for. $\endgroup$ Commented Nov 19, 2022 at 10:26
  • 1
    $\begingroup$ Okay, well then replace KeyValuePattern with HoldPattern. $\endgroup$
    – lericr
    Commented Nov 19, 2022 at 16:28
  • $\begingroup$ IT WORKED!!! THANK YOU SO MUCH :) here it is and its a one-liner: Options[stage] /. HoldPattern[x_ -> y_String?(StringLength[#] >= 35 &)] -> x -> Iconize[y, String] $\endgroup$ Commented Nov 20, 2022 at 10:06
  • $\begingroup$ i just figured out a shorter solution but i prefer your HoldPattern suggesting because it looks safer, here is the slightly shorter one: Options[stage] /. (x_ -> y_String /; StringLength[y] >= 35) -> (x ->Iconize[y, String]) $\endgroup$ Commented Nov 20, 2022 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.