function to easily align text for display (actually, 4 now)

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

Re: function to easily align text for display (actually, 4 n

Post by demonnic »

Feel free to just copy it right into your script, I'm totally fine with that. If you want to just drop a link to the forum thread in case anyone has questions that might be nice but I'm not even fussed if you don't necessarily. It struck me as a somewhat necessary set of functionality which hadn't been implemented yet, and someone was asking about in the IRC channel... next thing you know it's kind of grown out of hand. I like what I came up with though =)

I could have sworn I had just seen an edit about formatting variables into text. There are a couple of ways to go about it. If you just have the one variable to insert, it's easy enough to use:
Code: [show] | [select all] lua
[[You've encountered ]].. monster .. [[! What do you want to do now? ]] 
For your string. If you have a lot of things, you might want to use string.format()
Code: [show] | [select all] lua
string.format("You've encountered %s! You have seen this thing %s times and defeated it %s percent of the time", monsterName, monsterEncounters, monsterWinPercent)
Though really, it's often a matter of preference. If you're doing a bunch of string repetition inside a loop or something, string.format() is faster iirc, but other than that it's six of one, half a dozen of the other. You can use "" too, but it makes it more difficult to use " themselves inside of the string.

As regards the variable names, if you haven't already you may want to read The Shared Namespace And You
which is an excellent post regarding keeping your stuff compartmentalized inside a table (exactly like I didn't in this post... bad me)

reyl
Posts: 12
Joined: Wed Jul 10, 2013 5:33 pm

Re: function to easily align text for display (actually, 4 n

Post by reyl »

Yes i deleted that edit because I figured it out literally 10 seconds later lol, but thankyou! Regarding the namespace stuff, in other words the idea would be to include this in the package, but put it all into a confined space under the name of my project (eg. in this case myDumbScript.align(), myDumbScript.options = {} etc etc)? And that way mudlet won't cry due to there being two align() functions if they already had one?

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

Re: function to easily align text for display (actually, 4 n

Post by demonnic »

Yes, this way if they've written their own align function you won't over write it. If you over write one of mine from another package it might even upgrade them a bit, but it's unlikely to harm anything.

reyl
Posts: 12
Joined: Wed Jul 10, 2013 5:33 pm

Re: function to easily align text for display (actually, 4 n

Post by reyl »

ok my own project is getting totally out of hand now I know how you feel. I made this arguably overdesigned function because I needed it, i'm going to put it here because i'm so happy it works (but not happy enough to make a new post about it)

It's probably already been done but basically this is a function that prefixes a string (np) with the appropriate indefinite article ("a" or "an"):

Code: [show] | [select all] lua
function indefinite(np)
	local article = ""
	if string.starts(string.lower(np), "a")
	or string.starts(string.lower(np), "e")
	or string.starts(string.lower(np), "i")
	or string.starts(string.lower(np), "o")
	or string.starts(string.lower(np), "u")
	then
	article = "an "
	else article = "a " end
	return article .. np
end

so indefinite("animal") returns "an animal", and indefinite("cat") returns "a cat".

and it actually worked the way i pictured it in my head before I wrote it! Does this make me a programmer now? lmao. I also made a function pluralise(noun) to add an 's' to a string. Yes I know it won't work for irregular nouns, no I don't care. Now that I think of it, surely these kinds of things exist in lua already and I'm just not looking hard enough...

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: function to easily align text for display (actually, 4 n

Post by Jor'Mox »

Now to make it behave for things like "an hour" and "a house". ;)

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

Re: function to easily align text for display (actually, 4 n

Post by demonnic »

you're an evil man, Jor'Mox. I like this about you. =)

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: function to easily align text for display (actually, 4 n

Post by Jor'Mox »

I do what I can.

reyl
Posts: 12
Joined: Wed Jul 10, 2013 5:33 pm

Re: function to easily align text for display (actually, 4 n

Post by reyl »

yep I considered that a little later, lol. As far as I can tell there's nothing one can do but make a list of all the exceptions, which is awful and I'm not volunteering for that particular task haha.

EDIT: another solution would be to speak a more systematic language than English. I'm personally just grateful I'm not trying to do any of this in French. Somebody write a MUD in bahasa indonesia or something similarly light on syntax!

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

Re: function to easily align text for display (actually, 4 n

Post by SlySven »

Only trouble is, at present Mudlet is not perhaps as internationalised as one might wish, we don't handle stuff that is not ASCII as well as we might. The potential is there, we'll need to tweak a good few things but I dream that in the future the Lua scripters will be dealing with multi-byte utf-8 character strings rather than just a-z, A-Z, 0-9 and some punctuation but at present there is no way that you can send, display or receive any characters like ♞ or ☠!

Aside to Demonic: I was going to use "pile of poo" but that turned out to be a s**t idea, can you tell what the issue is with none BMP characters?

User avatar
Zaphob
Posts: 180
Joined: Wed May 09, 2012 8:07 am
Location: mg.mud.de

Re: function to easily align text for display (actually, 4 n

Post by Zaphob »

Nice scripts, thanks demonnic!
SlySven wrote:
Wed Aug 13, 2014 2:09 am
at present there is no way that you can send, display or receive any characters like ♞ or ☠!
At least that is fixed now. :mrgreen:

Post Reply