[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Colour copying
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Colour copying

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


Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Sun 19 Aug 2007 09:24 AM (UTC)
Message
I couldn't leave well enough alone... I took the function made in this thread: http://www.gammon.com.au/forum/bbshowpost.php?id=8052 and tried to make it copy the mud's colour codes. It was fairly easy except for one major issue I can't track down. The first line is always repeated if the last character is included in the selection. Even if I copy 20 lines, just that first line has itself repeated in the clipboard.

Here's the script:

Accelerator ("Ctrl+D", GetAlphaOption("script_prefix").."Copy3()")
colourcodes = { "","@r", "@g", "@y", "@b", "@m", "@c", "@w",
                "","@R", "@G", "@Y", "@B", "@M", "@C", "@W" }
function Copy3()
  local line1, line2 = GetSelectionStartLine (), GetSelectionEndLine ()
  local col1, col2 = GetSelectionStartColumn (), GetSelectionEndColumn ()
  local colourtable = {}
  for i = 1,8 do
    colourtable[GetNormalColour(i)] = i
    colourtable[GetBoldColour(i)] = i+8
  end
  if line1 == 0 then
    DoCommand( "copy" )
  else
    local copystring,addtext = "",""
    local left,right = 1,1
    for j = line1,line2 do
      if j == line1 then left = col1 else left = 1 end
      if j == line2 then right = col2 else right = GetLineInfo(j).length end
      for _,i in pairs( GetStyleInfo( j ) ) do
	if i.column + i.length >= left and i.column <= right then
          addtext = string.sub( i.text, math.max( left, i.column ) - i.column + 1,
		        	math.min( right, i.column+i.length) -i.column + 1 )
          if addtext ~= "" then
            copystring = copystring..colourcodes[ colourtable[ i.textcolour or 8 ] ]..addtext
	  end -- check blank start
	end -- check for copying
      end -- loop through styles
      if GetLineInfo(j, 3) == true and j ~= line2 then
        copystring = copystring.."\r\n"
      end -- add newline if needed
    end -- loop through lines
    if string.sub( copystring, 1, 2 ) == "@w" then
      copystring = string.sub( copystring, 3 )
    end

    SetClipboard( copystring )
  end
end -- function Copy3

It hasn't exactly been cleaned up yet, as I was trying to get it to work first. I thought I did get it working for a bit while I just grabbed the middle of lines. If anyone does have an easier way to grab the colour of a bit of text in the output buffer, I'm all ears.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #1 on Sun 19 Aug 2007 09:34 AM (UTC)

Amended on Sun 19 Aug 2007 09:35 AM (UTC) by Shaun Biggs

Message
And for an example of the problem

original (and again as html to show colours):

<Pinned to chest1>    (K)(G)(H) V2 Order Of The Second Tier
<Pinned to chest2>    (K)(G)(H) V2 Aardwolf Supporters Pin

<!-- Produced by MUSHclient v 4.18 - www.mushclient.com -->
<table border=0 cellpadding=5 bgcolor="#000000">
<tr><td>
<pre><code><font size=2 face="Courier New, FixedSys, Lucida Console, Courier New, Courier"><font color="#0">
</font><font color="#00FFFF">&lt;Pinned to chest1&gt;    </font><font color="#FF5A5A">(K)</font><font color="#FFFFFF">(G)</font><font color="#5AFFFF">(H) </font><font color="#FFFFFF">V2</font><font color="#E1E1E1"> </font><font color="#FF5A5A">O</font><font color="#FF0000">rder </font><font color="#98FB98">O</font><font color="#2CCE2C">f </font><font color="#5AFFFF">T</font><font color="#00FFFF">he </font><font color="#FF5A5A">S</font><font color="#FF0000">econd </font><font color="#98FB98">T</font><font color="#2CCE2C">ier
</font><font color="#00FFFF">&lt;Pinned to chest2&gt;    </font><font color="#FF5A5A">(K)</font><font color="#FFFFFF">(G)</font><font color="#5AFFFF">(H) </font><font color="#FFFFFF">V2 </font><font color="#FF5A5A">A</font><font color="#FF0000">ardwolf </font><font color="#98FB98">S</font><font color="#2CCE2C">upporters </font><font color="#5AFFFF">P</font><font color="#00FFFF">in
</font></font></code></pre>
</td></tr></table>

And what is put into the clipboard:

@c<Pinned to chest1>    @R(K)@W(G)@C(H) @WV2@w @RO@rrder @GO@gf @CT@che @RS@recond @GT@gier@c<Pinned to chest1>    @R(K)@W(G)@C(H) @WV2@w @RO@rrder @GO@gf @CT@che @RS@recond @GT@gier
@c<Pinned to chest2>    @R(K)@W(G)@C(H) @WV2 @RA@rardwolf @GS@gupporters @CP@cin

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sun 19 Aug 2007 09:56 PM (UTC)

Amended on Sun 19 Aug 2007 10:04 PM (UTC) by Nick Gammon

Message
Looking at your code so far I have found 2 problems, however I have not yet reproduced your problem of the repeated line.

First problem, which I can't reproduce right now, is getting this:


[string "Immediate"]:20: bad argument #1 to 'pairs' (table expected, got no value)
stack traceback:
	[C]: in function 'pairs'
	[string "Immediate"]:20: in function 'Copy3'
	[string "Command line"]:1: in main chunk


That is referring to this line:


for _,i in pairs( GetStyleInfo( j ) ) do


So far I can't work out what caused that, as it won't do it again.

Second problem is this:


[string "Immediate"]:27: attempt to concatenate field '?' (a nil value)
stack traceback:
        [string "Immediate"]:27: in function 'Copy3'
        [string "Command line"]:1: in main chunk


This was caused by copying a world.Note, which had a colour that wasn't in your table, for this line:


copystring = copystring..colourcodes[ colourtable[ i.textcolour or 8 ] ]..addtext


You probably need to allow for a non-matching colour and just append nothing, eg.


copystring = copystring..(colourcodes[ colourtable[ i.textcolour or 8 ] ] or "") ..addtext


As for the reported problem, where do you start and end copying exactly? I put in a debugging line to show the start and end columns, like this:


line1= 96 line2= 106 col1= 1 col2= 1
line1= 86 line2= 89 col1= 1 col2= 28


(It was when I tried it on the debugging display I got the second error message).

Now would you get the problem starting at column 1 and ending part-way through a line? The very end of a line? Starting at the end of a previous line?

Maybe do a forum post with underlines showing exactly where the selection is - preferably put in my debugging display so I can see the start and end lines/columns:


print ("line1=", line1, "line2=", line2, "col1=", col1, "col2=", col2)


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 19 Aug 2007 10:38 PM (UTC)
Message
Your scheme will fail for default worlds, which don't have a scripting prefix (by design). It might be better (if you are going to publish it) to make an alias. The accelerator can call the alias, and the alias can do "send to script".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sun 19 Aug 2007 10:40 PM (UTC)

Amended on Sun 19 Aug 2007 10:44 PM (UTC) by Nick Gammon

Message
There is also an "out by one error" - if you select a single letter, it copies that and the one after it. That seems to be the fault of GetSelectionEndColumn.

I am a bit reluctant to change how GetSelectionEndColumn works, because probably quite a few scripts use it. It is probably better to change the documentation.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #5 on Sun 19 Aug 2007 11:13 PM (UTC)
Message
I never got that error for the "for _,i in pairs( GetStyleInfo( j ) ) do" line, so no idea what happened there.

Quote:
You probably need to allow for a non-matching colour and just append nothing, eg.

Still not sure what I would like to do about non-standard colours. Ideally, I'd like to convert them in some way to the basic 16, but that's a bit of a stretch. With testing though, I was just careful to not copy only unmodified output from the mud.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #6 on Sun 19 Aug 2007 11:26 PM (UTC)

Amended on Sun 19 Aug 2007 11:27 PM (UTC) by Shaun Biggs

Message
Quote:
As for the reported problem, where do you start and end copying exactly?

For the example above:
line1= 4910 line2= 4911 col1= 1 col2= 62

Starts with the beginning of the first line, and ends at the end of the second line. Here's another quick example using roughly the same data. The script is the same as before, with the addition of a printing message for lines/columns.

<Worn on back>        (K)(B)(M)(G)(H) a Heart Mirror
<Pinned to chest1>    (K)(M)(G)(H) V2 Order Of The Second Tier
<Pinned to chest2>    (K)(M)(G)(H) V2 Aardwolf Supporters Pin



line1= 4954 line2= 4956 col1= 1 col2= 62

@c<Worn on back>        @R(K)@c(B)@B(M)@W(G)@C(H) @wa @rHeart @WMirror@c<Worn on back>        @R(K)@c(B)@B(M)@W(G)@C(H) @wa @rHeart @WMirror
@c<Pinned to chest1>    @R(K)@B(M)@W(G)@C(H) @WV2@w @RO@rrder @GO@gf @CT@che @RS@recond @GT@gier
@c<Pinned to chest2>    @R(K)@B(M)@W(G)@C(H) @WV2 @RA@rardwolf @GS@gupporters @CP@cin

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Shadowfyr   USA  (1,783 posts)  [Biography] bio
Date Reply #7 on Mon 20 Aug 2007 03:52 PM (UTC)
Message
Imho Nick, failing to fix a bug because, "A lot of things probably rely on it.", is never a good idea. lol That is how you get idiocies like MS Excel containing a date caculation bug from Lotus 123, which isn't even sold any more. Just document the fix, provide a clear explanation for why it changed, then field the 1-2 questions that pop up do to it. I seriously doubt at this point that there are huge numbers of people using the function, who are all going to come charging with pitch forks and torches, just because you fixed a bug. ;)
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #8 on Mon 20 Aug 2007 07:34 PM (UTC)
Message
Well, I figured out why I was getting the double lines. I had a bit of code poorly commented out (as in it registered as uncommented) which only converted the first line. Here is the final product, in case anyone is curious.
colourcodes = { "","@r", "@g", "@y", "@b", "@m", "@c", "@w",
                "","@R", "@G", "@Y", "@B", "@M", "@C", "@W" }

function ColourCopy()
  local line1, line2 = GetSelectionStartLine (), GetSelectionEndLine ()
  local col1, col2 = GetSelectionStartColumn (), GetSelectionEndColumn ()
  local colourtable = {}
  for i = 1,8 do
    colourtable[GetNormalColour(i)] = i
    colourtable[GetBoldColour(i)] = i+8
  end
  if line1 ~= 0 then
    local copystring,addtext = "",""
    local left,right = 1,1
    for j = line1,line2 do
      if j == line1 then left = col1 else left = 1 end
      if j == line2 then right = col2 else right = GetLineInfo(j).length end
      for _,i in pairs( GetStyleInfo( j ) or {} ) do
      -- need the or {} to trap an odd error
	if i.column + i.length >= left and i.column <= right then
          addtext = string.sub( i.text, math.max( left, i.column ) - i.column + 1,
		        	math.min( right, i.column+i.length) -i.column + 1 )
          if addtext ~= "" then
	  -- sometimes grabs colourcodes from previous character
            copystring = copystring..colourcodes[ colourtable[ i.textcolour or 8 ] or "" ]..addtext
	    -- ignore colours that are not part of ansi sequence
	  end -- check blank start
	end -- check for copying
      end -- loop through styles
      if GetLineInfo(j, 3) == true and j ~= line2 then
        copystring = copystring.."\r\n"
      end -- add newline if needed
    end -- loop through lines
    if string.sub( copystring, 1, 2 ) == "@w" then
      copystring = string.sub( copystring, 3 )
    end  -- strip colour if it's just plain white
    SetClipboard( copystring )
  end
end -- function ColourCopy


What exactly is the nature of the GetSelectionEndColumn error? I'd like to code around it if possible.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] 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,542 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]