Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ how to draw styled text in miniwindow

how to draw styled text in miniwindow

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Zhenzh   China  (68 posts)  Bio
Date Sun 04 Mar 2018 04:56 PM (UTC)
Message
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?
Top

Posted by Fiendish   USA  (2,558 posts)  Bio   Global Moderator
Date Reply #1 on Sun 04 Mar 2018 05:49 PM (UTC)

Amended on Sun 04 Mar 2018 05:58 PM (UTC) by Fiendish

Message
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

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 04 Mar 2018 08:38 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Zhenzh   China  (68 posts)  Bio
Date Reply #3 on Mon 05 Mar 2018 05:54 PM (UTC)

Amended on Mon 05 Mar 2018 05:58 PM (UTC) by Zhenzh

Message
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

Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 05 Mar 2018 08:54 PM (UTC)

Amended on Mon 05 Mar 2018 08:59 PM (UTC) by Nick Gammon

Message
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.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,434 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.