From 6f2ccdd3959f8a96c3d24575f8be1929a444854b Mon Sep 17 00:00:00 2001 From: Stuart Buchanan Date: Tue, 28 Mar 2017 21:38:34 +0100 Subject: [PATCH] Add support for sprintf() formatting to tutorial tags can now contain sprintf() format strings (%d, %.2f etc), with /some/prop used as the substitute value. Could be extended in the future with perhaps Nasal evaluation? --- Docs/README.tutorials | 6 ++++-- Nasal/tutorial/tutorial.nas | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Docs/README.tutorials b/Docs/README.tutorials index 18722bfad..8ee684c8c 100644 --- a/Docs/README.tutorials +++ b/Docs/README.tutorials @@ -84,6 +84,10 @@ in detail below: is entered; allowed multiple times, in which case one is chosen at random + optional; allowed up to 4 times. + ... property to substitute into the string + using sprintf() formatting. E.g. %d, %.2f + optional; file name of *.wav sample to be played; may be used multiple times (random) @@ -473,5 +477,3 @@ terminal, for example: This just needs to be copied to the tutorial XML file. - - diff --git a/Nasal/tutorial/tutorial.nas b/Nasal/tutorial/tutorial.nas index f44c985e4..9a7a2b928 100644 --- a/Nasal/tutorial/tutorial.nas +++ b/Nasal/tutorial/tutorial.nas @@ -414,6 +414,20 @@ var say_message = func(node, default = nil) { } if (msg != nil) { + var params = []; + + foreach (var p; node.getChildren("message-param")) { + if (p.getNode("property") != nil) { + append(params, getprop(p.getNode("property").getValue())); + } + } + + # Ugly + if (size(params) == 1) { msg = sprintf(msg, params[0]); } + if (size(params) == 2) { msg = sprintf(msg, params[0], params[1]); } + if (size(params) == 3) { msg = sprintf(msg, params[0], params[1], params[2]); } + if (size(params) == 4) { msg = sprintf(msg, params[0], params[1], params[2], params[3]); } + display.write(msg, 1, 1, 1); if (audio == nil) { # Link to text-to-speech