Copy and paste colored text from main window?

Post Reply
Seren
Posts: 4
Joined: Sun Jan 17, 2016 11:35 pm

Copy and paste colored text from main window?

Post by Seren »

I'm a former ZMUD/CMUD user, and one of the only things I miss about that otherwise clunky and inferior program was the ability to select, copy, and paste color-coded text directly from the main window, using mouse clicks.

In the game I play, color codes are very important when crafting elaborate items and composing ASCII art, but it can be quite tedious to insert the codes manually. (By color codes, I specifically mean the codes I can send to the MUD to ensure that my text is colored for everyone, not just myself. For example, if I sent "say This color is {Yyellow{x!" to the MUD, the game would return, 'Seren says, "This color is yellow!"' and the word 'yellow' would be colored bright yellow, and the rest of the text would be the default color.) In CMUD, if I wanted to copy colored text for re-use, all I had to do was highlight the text on my screen using my mouse, and the program would do the work of inserting the game's color codes into the text before saving the selection to the clipboard for easy pasting.

As far as I can tell, this functionality is not built into Mudlet, so I've been trying to think of a good way to mimic CMUD's convenient color copying feature via scripts and aliases.

I quickly discovered that mouse click events in Mudlet return pixel coordinates, whereas pretty much every function used to manipulate strings operates on column- and line-based coordinates, so I threw the mouse requirement out the window. I then attempted to use functions like isAnsiFgColor() on incoming lines, instead. So far, I have not been able to get this to work consistently or well. I briefly looked into copying HTML (via right click) and converting it to ANSI color codes from the command line, but I was stymied by the fact that HTML uses a lot of semi-colons, Mudlet's default command separator.

I've about reached the limits of my amateur programming ability. Has anyone else attempted to code this feature in some way, and if so, would you be willing to share your experience?

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Copy and paste colored text from main window?

Post by SlySven »

What are the codes that you need to send to the MUD server to produce colour? Technically speaking sending ASCII Esc codes shouldn't be done without Telnet negotiation of the capabilities of the remove connection (via sub-option 24, a.k.a. TTYPE) and Mudlet does not do such a thing (it does this in the opposite direction so that the MUD Server knows it is talking to a Mudlet client instance, indeed recent versions even report the precise version of the Mudlet application...)

Theoretically it might be possible to define your own aliases to send some colour code (replacing the "{y" and "{x" from your example) but then you may run into the difficulty that Mudlet uses XML Version 1.0 internally to store scripts and such like, which prohibits the encoding of any ASCII character less than 0x20 (Space) except for 0x09 (␉), 0x0A (␊) and 0x0D (␍) - which immediately causes problems with the very first one you will want for ANSCII Colour codes, i.e. the 0x1B (␛) character!

I do have plans for a refinement to improve the code to reproduce the on-screen text as HTML - it could be optimised to remove a lot of repetitive CSS code but that will not help here I think.

Seren
Posts: 4
Joined: Sun Jan 17, 2016 11:35 pm

Re: Copy and paste colored text from main window?

Post by Seren »

{y, {x, {b, etc. are the kinds of "codes" I have to insert into my text in order to produce color in the MUD. The problem isn't in sending these characters to the game, it's in translating the colored text that I receive, so that I can reuse that text elsewhere, colors and all. I created a script using s:gsub functions, which looks at every letter of every word on a line and attempts to determine the foreground color of each character, add the appropriate "color code", then save everything back to a variable before printing the line to the screen. This at least allowed me to turn on a "color code printing mode" so that I could copy and paste the coded lines directly from the main window, using my mouse.

Unfortunately, I couldn't get this script to work flawlessly. In lines that had multiple colors, the script was mixing some of them up in odd ways (not randomly, but not correctly, either), and I couldn't figure out if it was a flaw in the code or if it was perhaps an issue with the way Mudlet was receiving and printing ANSI color from this particular game (or an issue with the way the game was sending color).

Here's a snippet of script to give you some idea of what I was trying to accomplish. Obviously, it's not very refined, but I figured if I could get it to work on a letter-by-letter basis, I could improve the script further:
Code: [show] | [select all] lua
function findRBG(a)
	selectString(a,1)
	if isAnsiFgColor(3) then
		return "{R"..a
	elseif isAnsiFgColor(4) then
		return "{r"..a
	elseif isAnsiFgColor(7) then
		return "{Y"..a
	elseif isAnsiFgColor(8) then
		return "{y"..a
	elseif isAnsiFgColor(5) then
		return "{G"..a
	elseif isAnsiFgColor(6) then
		return "{g"..a
	elseif isAnsiFgColor(13) then
		return "{C"..a
	elseif isAnsiFgColor(14) then
		return "{c"..a
	elseif isAnsiFgColor(9) then
		return "{B"..a
	elseif isAnsiFgColor(10) then
		return "{b"..a
	elseif isAnsiFgColor(11) then
		return "{M"..a
	elseif isAnsiFgColor(12) then
		return "{m"..a
	elseif isAnsiFgColor(2) then
		return "{D"..a
	elseif isAnsiFgColor(1) then
		return "{w"..a
	elseif isAnsiFgColor(15) then
		return "{W"..a
	elseif isAnsiFgColor(16) then
		return "{w"..a
	elseif isAnsiFgColor(0) then
		return "{w"..a
	end
end

function convertColors()
	lineoftext = getCurrentLine()
	colorcodedline = string.gsub(lineoftext, "(%a)", function(a)
		return findRBG(a)
		end)
end

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Copy and paste colored text from main window?

Post by SlySven »

Well I guess it works, sort of, (I'd store the current code and only put in a new one when it changed) and I think there ought to be something more sophisticated in the system for this sort of thing - but I must confess I do not know the details of how all of all of the lua stuff works - so if someone else wants to contribute, please do!

Post Reply