fText, the text formatting library

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
demonnic
Posts: 559
Joined: Sat Dec 05, 2009 3:19 pm

fText, the text formatting library

Post by demonnic » Sat Jul 13, 2019 7:20 pm

I'm keeping the xml package for this on my github at https://github.com/demonnic/MiscMudlet under the name fText.xml .
I've moved the documentation to the wiki. Updating the forum post is a pain in the butt, and I just feel more comfortable trying to document and give examples for this sort of thing on the wiki. So, for documentation, go here: https://github.com/demonnic/fText/wiki

Some of you may be familiar with this thing I made a while back for formatting text. I named it 'align' and wrote it completely in the global namespace which, looking back, I regret. Someone recently asked about some updates to it, and I looked over the code, and I was embarrassed. So I rewrote the whole thing, and included word wrapping of a sort.

It also has a reusable formatter object, which keeps internal track of its formatting options so you can use it to format any text the same way over and over.

It now also has a tablemaker, allowing you to make spreedsheat like tables to display information in a miniconsole or in your main console.

Now! It has some limitations. You shouldn't try to control the color inside of the string you're formatting if you're going to do wordwrapping yet. I intend to do that eventually but right now it just isn't going to work very well. I provide options for coloring the caps, spacers, and text in the meantime. And I'm only stripping the c| form of hex coloring from strings for the purposes of determining the string's actual length... I haven't figured out the pattern for doing that and # both yet. I imagine that will go in around the same time as I handle maintaining color instructions inside the string being passed for formatting.

And now, some pictures just generally showing what it can do.
fText2.png
tablemaker.png

User avatar
demonnic
Posts: 559
Joined: Sat Dec 05, 2009 3:19 pm

Re: fText(), the next iteration of align()

Post by demonnic » Wed Aug 07, 2019 4:41 am

I have updated this, though I have not really changed the underlying functionality. I have however now included a reusable TextFormatter object. You create it using demonnic.TextFormatter:new(tableOfOptions) . In this case, the tableOfOptions should be the same as the second argument to the align/fText line of functions, and will be reused for formatting. You then use the format(stringToFormat) function of the newly created object to get back a formatted message. So for instance, the code
Code: [show] | [select all] lua
myFormatter = demonnic.TextFormatter:new( {
  width = 40, 
  cap = "[CAP]",
  capColor = "<orange>",
  textColor = "<light_blue>"
})
myMessage = "This is a test of the emergency broadcasting system. This is only a test"
cecho(myFormatter:format(myMessage))
produces the following:
Screenshot from 2019-08-07 00-33-26.png
Screenshot from 2019-08-07 00-33-26.png (5.14 KiB) Viewed 111 times
If you were to then do
Code: [show] | [select all] lua
myFormatter:setCapColor("<green>")
cecho(myFormatter:format(myMessage))
You would get:
Screenshot from 2019-08-07 00-35-22.png
Screenshot from 2019-08-07 00-35-22.png (4.38 KiB) Viewed 111 times
I have included functions to set all the valid options for the align/ftext functions. You can also view or set the options of the formatter directly in the options table it holds, for example:
Code: [show] | [select all] lua
display(myFormatter.options)
myFormatter.options.capColor = "<green>"
However, I recommend using the set functions which provide type checking and more error handling.

User avatar
demonnic
Posts: 559
Joined: Sat Dec 05, 2009 3:19 pm

Re: fText(), the next iteration of align()

Post by demonnic » Tue Aug 13, 2019 4:35 am

I have updated this. It now also include a table maker!
Check it out here: https://github.com/demonnic/fText/wiki/ ... r-examples

User avatar
demonnic
Posts: 559
Joined: Sat Dec 05, 2009 3:19 pm

Re: fText, the text formatting library

Post by demonnic » Wed Aug 14, 2019 4:04 am

New update, adds setCell for the TableMaker, and deleteRow/deleteColumn.

It also allows you to use functions for cell entries, so long as those functions return cells.

Also, I do not deepcopy the tables added via TableMaker:addRow(), which means if you update the original table, the value in the TableMaker will change. IE
Code: [show] | [select all] lua
myStats = { "50", "75", "82" }
myTableMaker:addRow(myStats)
-- now as you update myStats, the new entries will be reflected in the output of myTableMaker:assemble(). 
-- iow, you can update and reprint the table by doing
myStats[1] = "30"
cecho(myTableMaker:assemble())

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests