Function to print indexed text from file?

Post Reply
Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Function to print indexed text from file?

Post by Nyyrazzilyss »

Before I just write it myself: Is there any prexisting commands or function sets to echo segments of a file to the screen? For example, if I have a text file of the below format:

;;id,index name
This is a text message
This is a text message
This is a text message
;;id,index name
(etc)

I would pass id/or index name to the function, and it would respond by echoing the associated contents. Even better would be rendering html, and allowing use of formatting/image linking/etc

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Function to print indexed text from file?

Post by Nyyrazzilyss »

Hadn't heard anything one way or the other, so wrote a quick function to do so - It's extremely basic, just text to screen/doesn't handle colors/html/etc. Quite likely has bugs, I haven't used it excessively yet. The main thing is I need to write some usage documentation for my mudlet package, and this lets me easily organize/edit it in a single file.
Code: [show] | [select all] lua
--function mainpath(xname)
--	This function returns the full pathname inside the profile of a specific file
--end

function showHelp(xindex)
	local index, name
	local found=false
	
	for line in io.lines(mainpath("helpfile.dat")) do
		if string.find(line, ";;") ~= nil then
			if found == false then
				index,name = string.match(line, ";;([0-9]+),([a-zA-Z ]+)")
			else
				return
			end
		else
			if index == tostring(xindex) or name == xindex then
				found=true
				echo(line .. "\n")
			end
		end
	end

	if found == false then
		echo("[No help information found for: " .. xindex .. "]\n")
	end
end

if matches[2] == "" then
	showHelp("index")
else
	showHelp(matches[2])
end
The above code is inside a '#help' alias, the pattern for it is ^#help ?(.*)?

The datafile is of the format

;;number,item name
text
text
;;number,item name
etc

Usage: typing '#help' prints the contents of 'index'. '#help anythingelse' searches the file for the 'anythingelse'' entry, and echoes that to the screen.

Post Reply