From b872615e897c1cf0a0a4c6d0ccf9dd155d86ec54 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 9 Dec 2021 16:13:00 +0100 Subject: mod/inspectvar: introduce $InspectVarReturn --- mod/inspectvar | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mod/inspectvar b/mod/inspectvar index 4e10fd6..27a47fa 100644 --- a/mod/inspectvar +++ b/mod/inspectvar @@ -4,15 +4,24 @@ # https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md :global InspectVar; +:global InspectVarReturn; -# inspect variable +# inspect variable and print on terminal :set InspectVar do={ + :global CharacterReplace; + :global InspectVarReturn; + + :put [ $CharacterReplace [ $InspectVarReturn $1 ] ("\n") ("\n\r") ]; +} + +# inspect variable and return formatted string +:set InspectVarReturn do={ :local Input $1; :local Level (0 + [ :tonum $2 ]); - :global InspectVar; + :global InspectVarReturn; - :local PutIndent do={ + :local IndentReturn do={ :local Prefix [ :tostr $1 ]; :local Value [ :tostr $2 ]; :local Level [ :tonum $3 ]; @@ -21,20 +30,23 @@ :for I from=1 to=$Level step=1 do={ :set Indent ($Indent . " "); } - :put ($Indent . "-" . $Prefix . "-> " . $Value); + :return ($Indent . "-" . $Prefix . "-> " . $Value); } :local TypeOf [ :typeof $Input ]; - $PutIndent "type" $TypeOf $Level; + :local Return [ $IndentReturn "type" $TypeOf $Level ]; :if ($TypeOf = "array") do={ :foreach Key,Value in=$Input do={ - $PutIndent "key" $Key ($Level + 1); - $InspectVar $Value ($Level + 2); + :set $Return ($Return . "\n" . \ + [ $IndentReturn "key" $Key ($Level + 1) ] . "\n" . \ + [ $InspectVarReturn $Value ($Level + 2) ]); } } else={ :if ($TypeOf != "nothing") do={ - $PutIndent "value" $Input $Level; + :set $Return ($Return . "\n" . \ + [ $IndentReturn "value" $Input $Level ]); } } + :return $Return; } -- cgit v1.2.3-54-g00ecf