Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube] Print macro spacing

edited October 2015 in Help! with 2.0
This question is somewhat related to this one I already posted, but I'll make another thread because it's not really the same thing.

Does the <<print>> macro add a space at the end of its output? I'm doing the same thing as the other question, displaying the time, but it's displaying text similar to "11 :00 AM", when I want "11:00 AM". Note the space between the "11" and the ":" in the first example.

I'm using the nobr passage tag, and when I tried <<silently>> it output nothing at all. Here's my code, if it's helpful:
<<widget "displayTime">>
<<if ndef $args[0]>>
	<<set $workingTime = $time>>
<<else>>
	<<set $workingTime = $args[0]>>
<</if>>
<<if $workingTime % 12 == 0>>
	<<print "12">>
<<else>>
	<<print $workingTime % 12>>
<</if>>
<<print ":00 ">>
<<if $workingTime >= 12>>
	<<print "PM">>
<<else>>
	<<print "AM">>
<</if>>
<</widget>>

The only solution I see is to add ":00 " at the end of the time printing macros, but that seems like an odd workaround to what I assumed would be a simple thing.

Comments

  • The <<print>> macro does not add anything to the beginning or end of its output.

    The reason you're seeing a space between the hours and minutes is because you have whitespace between the hours and minutes <<print>> macros.

    Your best bet to resolve that would be to either build the string you want to print, then print it, or stop putting all the whitespace in there. I'd hope the latter is self-explanatory. As an example of the former: (also using some line continuations)
    <<widget "displayTime">>\
    <<silently>>
    <<if ndef $args[0]>>
    	<<set $workingTime = $time>>
    <<else>>
    	<<set $workingTime = $args[0]>>
    <</if>>
    <<if $workingTime % 12 == 0>>
    	<<set $formattedTime = 12>>
    <<else>>
    	<<set $formattedTime = $workingTime % 12>>
    <</if>>
    <<set $formattedTime += ":00">>
    <<if $workingTime >= 12>>
    	<<set $formattedTime += " PM">>
    <<else>>
    	<<set $formattedTime += " AM">>
    <</if>>
    <</silently>>\
    $formattedTime\
    <<unset $formattedTime>>\
    <</widget>>
    
  • I see. I thought the nobr tag in my passage would have taken care of that, but I guess it literally is just no line breaks. Thanks for the help!
  • numberQ wrote: »
    ... I thought the nobr tag in my passage would have taken care of that, but I guess it literally is just no line breaks.
    The documentation for the nobr tag states that it is:
    Equivalent to a full-passage <<nobr>>.
    ... and the documentation for the <<nobr>> macro states this it:
    Executes its contents and outputs the result, after removing all line breaks.
    ... so it is very clear on what that macro does.

    SugarCube has very good documentation and reading that documentation can help to better understand what each feature does. lol
Sign In or Register to comment.