Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion Goto page 1, 2  Next
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 4:36 pm   

A note formatter script?
 
I've been racking my brains on this. It should be simple enough, but I somehow cannot come up with a way to script this. And since the MUD hasn't implemented this, I want to make a script to automatically format my notes for me. Below is an example of a properly formatted note:

Code:
This is an example of a note. Each line can be no more than 79 characters
long, but I do not want it to break off in the middle of a word. Therefore I
need to be able to have the script detect the final word, and if it allows
the line to go over 79 characters, it breaks it off to the space before and
moves it to the next line.


As the note suggests, no more than 79 characters, and I don't want it to break off in the middle of a word. I've no doubt in my mind it's very possible, I just can't figure it out. *sigh* Any help would be appreciated.

Charneus
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 5:23 pm   
 
I would be interested in this too, my mud allows formatting, but the way it formats can screw up some fancy effects so I would prefer to format myself with the fancy effects in tact.

I'm imagining there must be function that can count the number of characters in a line? IF there isn't, perhaps it can count the individual characters of the words, white spaces and such until it reads a carriage return (you may have to make a character to physically represent this) and then feeds to a new line?
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 5:30 pm   
 
Well, to answer your question, chamenas, there is. For instance, I could do:

#ONINPUT {(*)} {#show %len(%1)} and it'll show me the number of characters in the parameter. Heh. So I know that's going to be part of the code. Probably %word, too. But not sure how to all implement it. *sigh*

Charneus
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 5:34 pm   
 
I suggest making a carriage return character, one that you're not going to use, like... ^ so it could put ^ every 79 characters and then a later part of the script could spot them, remove them, and replace with a carriage return.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 5:36 pm   
 
hrmm...

totalcount=%len(%1)

numberoflines=totalcount/79

place a line feed at character 79+79 and run through a loop that ends once it hits numberoflines, addng 79+79+79+...
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 09, 2008 5:38 pm   
 
I haven't got CMUD or ZMUD handy ATM
So I dont expect this to run right out of the box
but how about something like:
Code:
$string = "whatever yada yada yada yada.....several hundred characters later yada"
$slist=""
$str=""
$count=0
#while %regex($string,"^(.{,80}\b)",$str,$count) {
 $slist = %additem($slist,$str)
 $string = %right($string,$count)
}
#for $slist {#send %i}
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri May 09, 2008 5:50 pm   
 
Okay, so we're starting with a string. $string="This is an example of a note. Each line can be no more than 79 characters long, but I do not want it to break off in the middle of a word. Therefore I need to be able to have the script detect the final word, and if it allows the line to go over 79 characters, it breaks it off to the space before and moves it to the next line.".

Now we need to take a substring that's the first 79 characters: $substring=%copy($string,1,79) and then find where the last space in this string is: $break=%regex($substring," [^ ]+$"). Now we know where to break the line, so we do that: #additem Lines %left($string,$break) and snap the part we just used out of the string: $string=%right($string,$break). Repeat until $string is less than 79 chars long.

I won't guarantee that this'll work, but it's my thoughts on the issue.

EDIT: Though I'm really liking Dharkael's \b approach, if you can get that to work.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 5:53 pm   
 
I like the idea behind it, Dharkael, but unfortunately, you said it - it didn't work right out of the box. ;) Hmmm... let me see if I might be able to figure it out until you get CMUD handy...

Charneus
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 09, 2008 5:53 pm   
 
Hmm for some reason I thought you had said 80 chars :p
I think thats the limit on my mud :p
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri May 09, 2008 5:56 pm   
 
My psuedo attempts are still options! Though they of course don't check for finished words Confused
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 6:21 pm   
 
Heh. I tried your example, Fang, and it just froze up CMUD. Unless I messed up somewhere in my own scripting. Below is what the two different scripts look like.

Code:
#CLASS {NoteWriter}
#ALIAS note {#SWITCH (%1="") {~note} (%1="write") {#T+ {NoteWriter|Formatter};~note %-1} {~note %-1}}
#CLASS 0
#CLASS {NoteWriter|Formatter} {disable}
#ONINPUT {(*)} {$string=%1;$slist="";$str="";$count=0;#while %regex($string,"^(.{,79}\b)",$str,$count) {$slist=%additem($slist,$str);$string = %right($string,$count)};#for $slist {#send %i}}
#TRIGGER {^You are back in the game!} {#T- {NoteWriter|Formatter}}
#CLASS 0


using Dharkael's code. Doesn't work (yet).

Fang, using yours:

Code:
#ONINPUT {(*)} {$string=%1;#WHILE (%len($string)>79) {$substring=%copy($string,1,79);$break=%regex($substring," [^ ]+$");$lines=%additem($lines, %left($string,$break));$string=%right($string,$break)};#FORALL $lines {#SEND %i}}


Again, yours froze CMUD, and I had to restart it.

Am I missing something here now?

Charneus
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 09, 2008 6:24 pm   
 
Any chance you can give some info on how it's not working? Like what's going into $slist, is it getting locked somewhere?
If ya give me some info maybe I can trouble shoot my code a bit.

EDIT: I think I have it! In my additem the $slist and $str are backwards
try
Code:
%additem($str,$slist)
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 6:43 pm   
 
Well, after doing the change and the initial start, what's happening is that it's sending the line, completely unchanged. So basically, nothing is happening during the #WHILE. I've done a couple of echoes to echo the local variables, and only the $string comes out. (I don't bother with the $slist, $str, and $count, since I echo them under the #while).

*sigh*

Charneus
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 09, 2008 7:02 pm   
 
Well If you haven't got it in about 2 or so hrs I'll see what I can come up with.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 7:10 pm   
 
Ha. I'm not at my computer right now, but I think I realize why, and I feel like an idiot right now. I really shouldn't use #ONINPUT. Instead, I should just alias it. The script was sending the line unformatted. In the alias, the script has to format the string first, then send it. Heh. Will let you know how it works in a bit.

Charneus
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 7:23 pm   
 
Nope. That didn't work. Then I decided, "Well, maybe the WHILE doesn't have a pattern to match to (thinking the %regex wouldn't satisify it). So, I decided to change it to #WHILE (%len($string)>79) {%regex...} --- All that wound up doing was spamming my screen with the same $string, as it would continually echo it.

EDIT: In fact, all it did was send 0 to the mud multiple times. Heh.

Charneus
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Fri May 09, 2008 8:35 pm   
 
Well that would work.

$length=%len($string)

Then do the #while ($length > 79) but you would have to remove the substring from $string and then update the length after adding it to the list.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Fri May 09, 2008 8:46 pm   
 
Bah, only takes 1 line of code.
Code:

$string="This is an example of a note. Each line can be no more than 79 characters long, but I do not want it to break off in the middle of a word. Therefore I need to be able to have the script detect the final word, and if it allows the line to go over 79 characters, it breaks it off to the space before and moves it to the next line."

#SHOW %subregex($string,"(.{1,79})(?:$| )",%concat("\1",%crlf))
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 09, 2008 8:47 pm   
 
Turns out that in the pattern I had to set the lower bound ^(.{1,79}\b)
Code:
$string="This is an example of a note. Each line can be no more than 79 characters long, but I do not want it to break off in the middle of a word. Therefore I need to be able to have the script detect the final word, and if it allows the line to go over 79 characters, it breaks it off to the space before and moves it to the next line.".
$slist=""
$str=""
$count=0
#while %regex($string,"^(.{1,79}(?!\w))",$str,$count) {
  $slist = %additem($str,$slist)
 $string = %right($string,$count)
}
#for $slist {#send %i}


Also switched form \b to negative lookahead (?!\w)

Tested and it works.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."

Last edited by Dharkael on Fri May 09, 2008 8:49 pm; edited 1 time in total
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri May 09, 2008 8:48 pm   
 
Vijilante wrote:
Bah, only takes 1 line of code.
Code:

$string="This is an example of a note. Each line can be no more than 79 characters long, but I do not want it to break off in the middle of a word. Therefore I need to be able to have the script detect the final word, and if it allows the line to go over 79 characters, it breaks it off to the space before and moves it to the next line."

#SHOW %subregex($string,"(.{1,79})(?:$| )",%concat("\1",%crlf))


Showoff.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Fri May 09, 2008 9:02 pm   
 
lol I was waiting for the regex guru to show up.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Fri May 09, 2008 9:59 pm   
 
That wasn't really showing off. Doing justification would be showing off.
Code:
#FORALL %subregex($string,"(.{1,79})(?:($)| )","\1(?(2)|�%len\(""\1""\)|)") {$len=(79-%eval(%word(%i,2,"�")));#IF ($len>0) {#SHOW %subregex(%word(%i,1,"�")," ",%concat("(?(<=:",$len,")  | )"))} {#SHOW %word(%i,1,"�")}}

_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Fri May 09, 2008 10:02 pm   
 
We are not worthy! *bow*
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri May 09, 2008 10:13 pm   
 
oldguy2 is right. We are definitely not worthy of your greatness, although in your "showing off" example, it added spaces between words in some cases (like for instance, it shows
Code:
be  able

Instead of
Code:
be able


Just something to point out.

Now my next question is, using Vijilante's solution, how would you implement our MUD's color codes? We use @ for color codes on our MUD, and it works in notes, too. So, if we type:

@RThis is an example.

it'll show up on the mud as:

This is an example.

Don't get me wrong - I'm quite happy with this solution, and if need be, I'll have to do the color codes myself. But if there's a simpler way, I know I can count on you guys to provide it. Thanks again, everyone, for all your input!

Charneus
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Fri May 09, 2008 10:20 pm   
 
Quote:
although in your "showing off" example, it added spaces between words in some cases
That is what it is supposed to do. Justification is make it so the words fill to the margins. The one I wrote is somewhat simple and wouldn't really justify correctly if there were a lot of big words in the line.

I also added the handling to not tack on an extra crlf at the end with this example.

To handle color codes all we need to do is make them not take up a character slot in the regex's count of chracters.
Code:
#SHOW %subregex($string,"((?:(?:@.)?.){1,79})(?:($)| )",%concat("\1(?(2)|",%crlf,")"))
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net