$wrap2 - Preserves control codes

By rockcavera on Feb 10, 2017

This snippet preserves the control codes (bold, italic, underline, reverse, color and plain) of the text wrapped, something the $wrap does not do. Its use is identical to the identifier $wrap.

Syntax: $wrap2(text, font, size, width, [word,] N)

alias wrap2 {
  if (!$isid) return
  if ($3 !isnum) || ($4 !isnum) || ($5 !isnum) || (($6) && ($6 !isnum)) {
    echo -esc info * Invalid parameters: $!wrap2
    halt
  }
  set -l %n $5
  set -l %wrap $!wrap($1,$2,$3,$4,%n)
  if ($6 != $null) {
    %n = $6
    %wrap = $!wrap($1,$2,$3,$4,$5,%n)
  }
  if (%n < 1) return [ [ %wrap ] ]
  set -l %t %n
  %n = 1
  while (%n < %t) {
    set -l %m $gettok(%r $+ [ [ %wrap ] ],-1,15)
    set -l %r
    if (2 \\ $count(%m,$chr(2))) %r = $chr(2)
    if (2 \\ $count(%m,$chr(31))) %r = $+(%r,$chr(31))
    if (2 \\ $count(%m,$chr(29))) %r = $+(%r,$chr(29))
    if (2 \\ $count(%m,$chr(22))) %r = $+(%r,$chr(22))
    set -l %x $count(%m,$chr(3))
    if (!%x) goto b
    if (!$regex(k,$mid(%m,$pos(%m,$chr(3),%x),3),/^\x03(\d\d?)/)) goto b
    %r = $+(%r,$chr(3),$base($regml(k,1),10,10,2))
    :a
    if ($regex(k,$mid(%m,$pos(%m,$chr(3),%x),6),/^\x03(\d\d?)(?:\x2c(\d\d?))?/)) && ($regml(k,2) != $null) %r = $+(%r,$chr(44),$base($v1,10,10,2))
    elseif ($regml(k,0) == 1) && (%x > 1) {
      dec %x
      goto a
    }
    :b
    inc %n
  }
  if ([ [ %wrap ] ] != $null) return %r $+ $v1
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.