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 ➜ Plugins ➜ ANSI logging plugin

ANSI logging plugin

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


Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Fri 25 Jul 2003 01:20 AM (UTC)

Amended on Sat 26 Jul 2003 10:59 PM (UTC) by Nick Gammon

Message
The plugin below attempts to log in ANSI colours to the log file. It reconstructs each line (using GetLineInfo and GetStyleInfo), tries to work out the ANSI colour for each style, and writes the lot to the log file.

You can copy it from below, or download it from:


http://www.mushclient.com/plugins/ANSI_Log.xml


The plugin is a useful illustration of how to use GetLineInfo and GetStyleInfo to work out the colours in lines in the output buffer.

Customising

You can change the entities (first few lines) from vbTrue to vbFalse to alter the way it works slightly.

See the comments below for more details.




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!-- Saved on Friday, July 25, 2003, 10:50 AM -->
<!-- MuClient version 3.42 -->

<!-- Plugin "ANSI_Log" generated by Plugin Wizard -->

<!--
You can change the entities at the top to control the way it works.
-->

<muclient>
<plugin
   name="ANSI_Log"
   author="Nick Gammon"
   id="08025212f70f54d3bb25dd02"
   language="VBscript"
   purpose="Logs commands and MUD output using ANSI colours"
   date_written="2003-07-25 10:45:39"
   requires="3.35"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Once installed, this plugin will log player commands, and output from the MUD, to the current log file using ANSI colour sequences.

As far as possible, the plugin will try to match the colours on the output lines to existing ANSI codes (this may fail if you are using MXP and the MUD sends non-ANSI colours).

It then generates the appropriate ANSI code and writes it to the log file.

The trigger and alias in the plugin set the "omit from log" flags so the lines are not logged twice.

They are set to "keep evaluating" so that other triggers and aliases are still processed.
]]>
</description>

</plugin>


<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^.*$"
   name="ansi_log"
   omit_from_log="y"
   regexp="y"
   script="LogOutput"
   sequence="5"
    >
  </trigger>
</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   name="ansi_log"
   script="LogCommand"
   match="^.*$"
   enabled="y"
   omit_from_log="y"
   regexp="y"
   keep_evaluating="y"
   sequence="5"
  >
  <send>%0</send>
  </alias>
</aliases>

<!--  Script  -->


<script>
<![CDATA[
dim PreserveLineBreaks
  PreserveLineBreaks = vbTrue

'
'  Helper function to output code for bold, underline etc.
'

Function DoExtra (is_set, was_set, on_code, off_code)
  
  DoExtra = ""

  If is_set And (not was_set) Then
    DoExtra = ANSI (on_code)  ' send setting code
  ElseIf (Not is_set) And (was_set) Then
    DoExtra = ANSI (off_code) ' send resetting code
  End If  ' need to un-set it

End Function

'
'  log in ANSI colours (call from trigger/alias that matches everything)
'
sub LogOutput (name, matchingline, wildcards)

dim firstline, lastline, line, style, styles
dim logline, colour, target, bold, column
dim normal (8)
dim hilite (8)
dim lastfore, lastback, lastbold
dim lastul, lastblink, lastinverse
dim ul, blink, inverse
dim i

  '
  ' Not necessary if logging not active
  ' 

  If Not IsLogOpen Then
    Exit Sub
  End If

  ' find which line this is
  lastline = GetLinesInBufferCount

  ' now find the first line in this paragraph

  firstline = lastline - 1

  do while firstline > 0 

    if GetLineInfo (firstline, 3) then 
      exit do
    end if

    firstline = firstline - 1

  loop ' end while loop - finding start of paragraph

  '
  ' paragraph starts with first line *past* the previous newline
  '

  firstline = firstline + 1

  '
  ' paragraph can't end past where it started
  '

  if firstline > lastline then firstline = lastline

  '
  '  discard world.notes and user input
  '

  if GetLineInfo (firstline, 4) then exit sub  ' note line
  if GetLineInfo (firstline, 5) then exit sub  ' input line

  '
  ' Empty lines are a special and simple case
  '

  if firstline = lastline and GetLineInfo (firstline, 2) = 0 then 
     WriteLog ""  ' just write the blank line
     Exit Sub
  end if

  '
  ' find what the ANSI colours are for this world
  '

  for i = 1 to 8 
    normal (i) = NormalColour (i)  ' normal
    hilite (i) = BoldColour (i)  ' highlight
  next

  '
  ' process entire paragraph
  '

  logline = logline & ANSI (0)  ' reset all styles
  lastfore = 37   ' reset is white
  lastback = 40       '  on black
  lastbold = vbFalse
  lastul = vbFalse
  lastblink = vbFalse
  lastinverse = vbFalse

  for line = firstline to lastline

    styles = GetLineInfo (line, 11)   ' how many styles

    '
    '  process all styles runs in this line
    ' 

    for style = 1 to styles
  
    '
    '  find text style (ANSI code)
    '
    
    target = GetStyleInfo (line, style, 14) ' RGB colour of style text
    colour = -1
    bold = vbFalse
 
    '
    '  work out which ANSI colour it must have been by a scan
    '
    for i = 1 to 8
      ' normal colour
      if target = normal (i) then
         colour = 29 + i
         exit for
      end if
      ' bold colour
      if target = hilite (i) then
         colour = 29 + i
         bold = vbTrue
         exit for
      end if
    next  ' colour

    '
    '  if found, output that colour
    '      

    if colour <> -1 then
      '
      '  only output if colour change
      '
      if colour <> lastfore then
        logline = logline & ANSI (colour)
        lastfore = colour 
      end if

    end if  ' colour found

  logline = logline & DoExtra (bold, lastbold, 1, 22)
    lastbold = bold

    target = GetStyleInfo (line, style, 15) ' RGB colour of style background
    colour = -1
 
    '
    '  work out which ANSI colour it must have been by a scan
    '
    for i = 1 to 8
      ' normal colour
      if target = normal (i) then
         colour = 39 + i
         exit for
      end if  
     next ' colour

    '
    '  if found, output that colour
    '      

    if colour <> -1 then
      '
      '  only output if colour change
      '
      if colour <> lastback then
        logline = logline & ANSI (colour)
        lastback = colour 
      end if

    end if  ' colour found

    '
    ' do other styles (underline, blink, inverse)
    '

    bold = GetStyleInfo (line, style,  8) ' bold flag
  logline = logline & DoExtra (bold, lastbold, 1, 22)
    lastbold = bold

    ul = GetStyleInfo (line, style, 9) ' underline flag
  logline = logline & DoExtra (ul, lastul, 4, 24)
    lastul = ul

    blink = GetStyleInfo (line, style, 10) ' blink flag
  logline = logline & DoExtra (blink, lastblink, 3, 23)
    lastblink = blink
    
    inverse = GetStyleInfo (line, style, 11) ' inverse flag
  logline = logline & DoExtra (inverse, lastinverse, 7, 27)
    lastinverse = inverse

    '
    '  now output the text
    '

    logline = logline & GetStyleInfo (line, style,  1)
    
    next ' process the next style

  '
  '  If wanted, keep the original (soft) line breaks
  '
   
  If PreserveLineBreaks And line <> lastline Then
    logline = logline & vbCrLf
  End If

  next ' end for loop - processing each line in the paragraph

  WriteLog logline

end sub  ' end of LogOutput 



sub LogCommand (name, matchingline, wildcards)
  '
  ' Not necessary if logging not active
  ' 

  If Not IsLogOpen Then
    Exit Sub
  End If

  '
  ' simply log in default ANSI colours
  '

  WriteLog ANSI (0) & matchingline

end sub

]]>
</script>

</muclient>


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


7,250 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.