how to draw styled text in miniwindow

Posted by Zhenzh on Sun 04 Mar 2018 04:56 PM — 5 posts, 21,516 views.

China #0
I have had a plugin capturing specified text from MUD and redirect them to a miniwindow.

In some cases, the captured text has special color/fonts.
I want these captured text being draw in miniwindow with its original styles.

I find there's a parameter 'styles' which includes all styles of the whole line and I'm able to loop this style table to draw the whole matched line with style.

But I can not draw the only captured text(recorded by wildcards parameter) as I can not get the exact start/end column of each text in wildcards table.

Is there a formal way to draw text in wildcards table with original style?
USA Global Moderator #1
Quote:
Is there a formal way to draw text in wildcards table with original style?


No, but it is possible to get the result you want with some extra work.

Call rex.new with the trigger pattern and then use that with :exec on the entire (without styles) line matched by your trigger, and then you'll have your columns.

https://www.gammon.com.au/scripts/doc.php?lua=re:exec
Amended on Sun 04 Mar 2018 05:58 PM by Fiendish
Australia Forum Administrator #2
If you have a simple pattern, Lua's string.find can be used to find the position of a match. For example:


^Exits: ()(.*)$


The returns two captures, the first being the position of the second capture.

Not all PCRE regular expressions can be expressed as Lua patterns, however for simple ones you may be able to do that.

Thus, you could use the Lua version in your trigger-matching code to establish the columns of the wildcards.

Alternatively, what Fiendish suggested would work, as you can find the positions of each capture.

Having done that, see: http://www.gammon.com.au/modules

In particular getstyle.lua will tell you the style for a particular column.
China #3
Thank you for your answer. I finally chose rex.new method in my plugin and successfully drew styled captures.

Here's my code:

<trigger
  enabled = "y"
  match = "^[>\s]*Title:【\s+\S+\s*\S*\s+】.*(?:」| )\S+\(\w+\)$"
  regexp = "y"
  keep_evaluating = "y"
  script = "DrawInfo"
  sequence = "1"
>
</trigger>

function DrawInfo (_, line, wildcards, styles)
  re = rex.new ("^[>\s]*Title:(【\s+\S+\s*\S*\s+】.*(?:」| ))\S+\(\w+\)$")
  local _,_,column = re:exec (line)
  local column_start,column_end = column[1],column[2]
  DrawText (context, line, text_width, text_color, styles, column_start, column_end)
end

function DrawText (context, text, text_width, text_color, styles, column_start, column_end)
  local text_x = 1
  local text_y = 1
  WindowRectOp (context.win, 2, text_x, text_y, text_x + text_width, text_y + text_height, background_colors)  -- clear text row
  if styles ~= nil then
    local column = 0
    for _,v in ipairs(styles) do
      column = column + v.length
      if column_start <= column then
        text_color = v.textcolour
        local column_text = string.sub (text, column_start, math.min(column, column_end))
        WindowText (context.win, context.font.id, column_text, text_x, text_y, 0, 0, text_color)
        if column_end > column then
          text_x = text_x + WindowTextWidth(context.win, context.font.id, column_text)
          column_start = column + 1
        else
          break
        end
      end
    end
  end
end

Amended on Mon 05 Mar 2018 05:58 PM by Zhenzh
Australia Forum Administrator #4
Very good. You don't need to duplicate the regular expression. Give the trigger a label (name) and then extract the regexp from it. That way, if you change it, you don't have to change it in two places.

eg.


<trigger
  enabled = "y"
  match = "^[>\s]*Title:?\s+\S+\s*\S*\s+?.*(?:?| )\S+\(\w+\)$"
  regexp = "y"
  keep_evaluating = "y"
  script = "DrawInfo"
  name="mytrigger"
  sequence = "1"
>
</trigger>

function DrawInfo (name, line, wildcards, styles)
  re = rex.new (GetTriggerInfo (name, 1)) -- get regexp
...





In fact, you don't even have to name the trigger, because if you don't they get an internally-generated name, and then the GetTriggerInfo call will still work.

Template:function=GetTriggerInfo
GetTriggerInfo

The documentation for the GetTriggerInfo script function is available online. It is also in the MUSHclient help file.