From cb338c76a8eb19a3ed1a73df330c3ddf638c7c39 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 20 Apr 2023 23:15:04 +0200 Subject: global-functions: introduce $FormatLine --- global-functions.rsc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'global-functions.rsc') diff --git a/global-functions.rsc b/global-functions.rsc index e682edc..501ea92 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -29,6 +29,7 @@ :global DownloadPackage; :global EitherOr; :global EscapeForRegEx; +:global FormatLine; :global GetMacVendor; :global GetRandom20CharAlNum; :global GetRandom20CharHex; @@ -329,6 +330,32 @@ :return $Return; } +# format a line for output +:set FormatLine do={ + :local Key [ :tostr $1 ]; + :local Values [ :toarray $2 ]; + :local Indent [ :tonum $3 ]; + :local Spaces " "; + :local Return ""; + + :global EitherOr; + :global FormatLine; + + :set Indent [ $EitherOr $Indent 16 ]; + + :if ([ :len $Key ] > 0) do={ :set Return ($Key . ":"); } + :if ([ :len $Key ] > ($Indent - 2)) do={ + :set Return ($Return . "\n" . [ :pick $Spaces 0 $Indent ] . ($Values->0)); + } else={ + :set Return ($Return . [ :pick $Spaces 0 ($Indent - [ :len $Return ]) ] . ($Values->0)); + } + :foreach Value in=[ :pick $Values 1 [ :len $Values ] ] do={ + :set Return ($Return . "\n" . [ $FormatLine "" ({$Value}) $Indent ]); + } + + :return $Return; +} + # get MAC vendor :set GetMacVendor do={ :local Mac [ :tostr $1 ]; -- cgit v1.2.3-54-g00ecf From fb12aabf3e8685aaa21185b145caf431ca44b0d4 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 20 Apr 2023 23:44:56 +0200 Subject: global-functions: $DeviceInfo: use $FormatLine --- global-functions.rsc | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'global-functions.rsc') diff --git a/global-functions.rsc b/global-functions.rsc index 501ea92..45fda9f 100644 --- a/global-functions.rsc +++ b/global-functions.rsc @@ -195,6 +195,7 @@ :global Identity; :global IfThenElse; + :global FormatLine; :local Resource [ /system/resource/get ]; :local RouterBoard; @@ -205,27 +206,27 @@ :local Update [ /system/package/update/get ]; :return ( \ - "Hostname: " . $Identity . \ - "\nBoard name: " . $Resource->"board-name" . \ - "\nArchitecture: " . $Resource->"architecture-name" . \ + [ $FormatLine "Hostname" $Identity ] . "\n" . \ + [ $FormatLine "Board name" ($Resource->"board-name") ] . "\n" . \ + [ $FormatLine "Architecture" ($Resource->"architecture-name") ] . "\n" . \ [ $IfThenElse ($RouterBoard->"routerboard" = true) \ - ("\nModel: " . $RouterBoard->"model" . \ - [ $IfThenElse ([ :len ($RouterBoard->"revision") ] > 0) \ - (" " . $RouterBoard->"revision") ] . \ - "\nSerial number: " . $RouterBoard->"serial-number") ] . \ + ([ $FormatLine "Model" ($RouterBoard->"model") ] . \ + [ $IfThenElse ([ :len ($RouterBoard->"revision") ] > 0) \ + (" " . $RouterBoard->"revision") ] . "\n" . \ + [ $FormatLine "Serial number" ($RouterBoard->"serial-number") ] . "\n") ] . \ [ $IfThenElse ([ :len ($License->"level") ] > 0) \ - ("\nLicense: " . $License->"level") ] . \ - "\nRouterOS:" . \ - "\n Channel: " . $Update->"channel" . \ - "\n Installed: " . $Update->"installed-version" . \ + ([ $FormatLine "License" ($License->"level") ] . "\n") ] . \ + "RouterOS:\n" . \ + [ $FormatLine " Channel" ($Update->"channel") ] . "\n" . \ + [ $FormatLine " Installed" ($Update->"installed-version") ] . "\n" . \ [ $IfThenElse ([ :typeof ($Update->"latest-version") ] != "nothing" && \ $Update->"installed-version" != $Update->"latest-version") \ - ("\n Available: " . $Update->"latest-version") ] . \ + ([ $FormatLine " Available" ($Update->"latest-version") ] . "\n") ] . \ [ $IfThenElse ($RouterBoard->"routerboard" = true && \ $RouterBoard->"current-firmware" != $RouterBoard->"upgrade-firmware") \ - ("\n Firmware: " . $RouterBoard->"current-firmware") ] . \ - "\nRouterOS-Scripts:" . \ - "\n Version: " . $ExpectedConfigVersion); + ([ $FormatLine " Firmware" ($RouterBoard->"current-firmware") ] . "\n") ] . \ + "RouterOS-Scripts:\n" . \ + [ $FormatLine " Version" $ExpectedConfigVersion ]); } # convert line endings, DOS -> UNIX -- cgit v1.2.3-54-g00ecf