mIRC Homepage
Posted By: argv0 $colorat() or generalized $controlat() - 30/05/12 06:16 PM
It would be useful in some situations to be able to have mIRC calculate the "effective colour" (or control codes) at a given position in a line. Consider the text (control codes replaced with ^K, ^B, ^U, etc.):

^K1,4hello ^K12 world ^K

If someone wanted to programmatically copy part of the line using /clipboard, say, the word "world", it would be non-trivial to copy the text along with its colours in the buffer. Note the effective colour at the "w" is ^K12,4.

It would be nice to have an identifier that returned the effective control code(s) to reproduce text at a given point in a line, for example:

Code:
//echo -a $colorat(^K1,4hello ^K12 world ^K, 15)


Would return ^K12,4.

The generalized version of this would be a $controlat identifier, perhaps better suited, where you could specify all codes you want mIRC to calculate:

Code:
//echo -a $controlat(^K1,4hel^Ulo ^B^K12^Bworld ^K, 17, bku)


Would return ^U^K12,4.

Perhaps a way to grab a range of text (like $mid but with control codes copied) would also be a good solution here. In this case, mIRC could perform proper wrapping of the codes:

Code:
//echo -a $midc(^K1,4hel^Ulo ^B^K12^Bworld ^K, 17, 2, bku)


Would return ^U^K12,4wo^K^U, so that it could be safely(*) used in a line. The downside of this is that occasionally the scripter only wants the control code data and not the text, but perhaps N=0 could return just the control codes.

(*) not entirely "safe", since mIRC would need to know the destination line to properly end a colour code, but at least you would not end up with leaking codes. Doing this manually with something like the initially suggested $controlat would require the scripter to check for each code and manually add the end-code, though there may be some obscure tricks to do this easily.

Although this is scriptable, doing the effective calculation is non-trivial, and, if mIRC ever adds colours or control codes, each script would need updating. Having a builtin identifier for this gives out of the box support and makes sure the parsing rules stay in sync with how mIRC actually interprets data on screen.
Posted By: Riamus2 Re: $colorat() or generalized $controlat() - 30/05/12 11:31 PM
I like this idea. I've had to calculate the codes before and it's definitely challenging. And even though I've managed it in most cases, there are still cases where I haven't been able to correctly determine the color at a given point in the line. And even where I could, it required a lot of work. It would be great to have something built in that would do it easily.
Posted By: pball Re: $colorat() or generalized $controlat() - 31/05/12 01:51 AM
I'll third this. Made a script which replaces stuff in a line and it wasn't fun to get the color codes so the rest of the line appeared as it was.
Posted By: qwerty Re: $colorat() or generalized $controlat() - 01/06/12 03:24 PM
+1, something like this would be very useful.
Posted By: Wims Re: $colorat() or generalized $controlat() - 03/06/12 11:23 PM
Good idea. Here is a scripted version for the meantime:
Code:
alias colorat {
  if ($prop == set) {
    if ($1 != $chr(15)) {
      var %i $iif($2,$2,$gettok(%colorat,2,44))
      %colorat = $1 $+ $iif($1,$iif(%i,$chr(44) $+ %i))
    }
    else %colorat =
  }
  else {
    set -u %colorat
    var %r /(?:\x03(?:(\d+)(?:,(\d+))?)?|(\x0F))/g,%r $regsubex($left($1,$2),%r,$colorat(\1,\2).set)
    %r = %colorat
    unset %colorat
    return $iif(%r != $null,$chr(3) $+ %r)
  }
}

alias controlat {
  if ($prop == set) {
    if ($1 != $chr(15)) {
      if ($1 isin %controlat) %controlat = $remove(%controlat,$1)
      else %controlat = %controlat $+ $1
    }
    else %controlat =
  }
  else {
    set -u %controlat
    var %c $iif($3,$3,kubir),%r $+(/,$chr(40),$left($regsubex($remove(%c,k),/(.)/g,\x $+ $base($replace(\1,u,31,b,2,i,29,r,22),10,16,2) $+ |),-1),|\x0F,$chr(41),/g),%r $regsubex($left($1,$2),%r,$controlat(\1).set)
    %r = %controlat
    unset %controlat
    return %r $+ $iif(k isin %c,$colorat($1,$2))
  }
}
alias midc returnex $controlat($1,$2,$4) $+ $mid($1,$2,$3)

Posted By: Riamus2 Re: $colorat() or generalized $controlat() - 03/06/12 11:57 PM
Unfortunately, this doesn't handle Ctrl-O in the text when determining the color and it counts the codes when determining where you are in the line (that second part may or may not be an issue depending on exactly what you're using this for). But it's a good start.
Posted By: Wims Re: $colorat() or generalized $controlat() - 04/06/12 12:01 AM
I completely forgot about ctrl+o!! I'll fix it eventually frown

Edit: should be ok now
© mIRC Discussion Forums