TrimText: Trim text e.g. to fit in a label

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This function will trim text up to a certain character and maximum length. This is helpful if we have random text of an unknown quantity that say needs to fit inside a fixed sized label on an InstallOptions page.

Using an input character of " " allows us to slice up to the next word and no further, so it is still readable. It also checks for multiple occurrences of that character so that we don't get some left on the end.

"..." is placed on the end of the output string if a trim was undertaken.

Usage

${TrimText} "string" "max_length" "up_to_char" "$out_var"

Example

${TrimText} "Hello my name is Stuart jajajajajaj haha!" 30 " " $R0
Output ($R0) is "Hello my name is Stuart..."

The Function

Function TrimText
 Exch $R0 ; char
 Exch
 Exch $R1 ; length
 Exch
 Exch 2
 Exch $R2 ; text
 Push $R3
 Push $R4
 
 StrLen $R3 $R2
 IntCmp $R3 $R1 Done Done
 
 StrCpy $R2 $R2 $R1
 
 StrCpy $R3 0
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 0 -3
 
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 -3
 
  IntOp $R3 $R3 - 1
  StrCpy $R2 $R2 -$R3
  StrCpy $R2 $R2...
 
 Done:
 StrCpy $R0 $R2
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Exch $R0 ; output
FunctionEnd
 
!macro TrimText Text Length Char Var
Push "${Text}"
Push "${Length}"
Push "${Char}"
 Call TrimText
Pop "${Var}"
!macroend
!define TrimText "!insertmacro TrimText"

-Stu