This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
FormatMe!
Started by savior67, 24 February 2013 - 04:37 AMPosted 24 February 2013 - 05:37 AM
Hello CC community, this is the first program I've ever posted. I created this mostly as a way to become more familiar with lua's string library. The program is called Format, and what it does is fix the indentation on a program to make it more readable. You are free to choose how many spaces to indent by using the variable (indent) at the top of the program. It works by searching each line in the file for a keyword ("end","function",ect…), then stripping whitespace from the line, and dedents or indents appropriately. Format will ignore comments and strings that contain keywords, but will not recognize multi-line strings, single quotes, or quotes within quotes (may be added later). Usage is "format <file>".
Pastebin Link
Before
After
Posted 24 February 2013 - 07:12 AM
Hmmm.. sounds like a cool program. But it's not working for me o.o
It says that it has been successfully formated but no spaces have been added after I opened the file again.
It says that it has been successfully formated but no spaces have been added after I opened the file again.
Posted 24 February 2013 - 07:13 AM
cool, But could use screenies.
Posted 24 February 2013 - 07:42 AM
Hmmm.. sounds like a cool program. But it's not working for me o.o
It says that it has been successfully formated but no spaces have been added after I opened the file again.
Hmm if you already have it indented you won't really notice a difference, try changing the indent from 2 to 5 and then using it.
Posted 24 February 2013 - 07:48 AM
No pics no clicks
Posted 24 February 2013 - 08:07 AM
Hmmm.. sounds like a cool program. But it's not working for me o.o
It says that it has been successfully formated but no spaces have been added after I opened the file again.
Hmm if you already have it indented you won't really notice a difference, try changing the indent from 2 to 5 and then using it.
No indentation - I made a file with stuff like this, copy pasted until lines ~300
asd
as
d
asd
as
d
asd
No indentation at all, also changed the indent variable to 6 before I posted my post earlier. Still had no effect
Posted 24 February 2013 - 08:16 AM
Hmmm.. sounds like a cool program. But it's not working for me o.o
It says that it has been successfully formated but no spaces have been added after I opened the file again.
Hmm if you already have it indented you won't really notice a difference, try changing the indent from 2 to 5 and then using it.
No indentation - I made a file with stuff like this, copy pasted until lines ~300asd as d asd as d asd
No indentation at all, also changed the indent variable to 6 before I posted my post earlier. Still had no effect
oh aha, well the indentation is meant for actual programs. Since there are no keywords, its not going to indent or dedent any of that nonsense. Try using it on a working program.
Posted 24 February 2013 - 08:24 AM
Oh I see lol, what keywords does it look for…
PS
You need to yield there because it will error, like it did to me, with codes over 200? lines.
Also, the screenies you posted - looks like it makes the indentation worse :S
PS
function overwrite(lines,file)
local h = fs.open(file,"w")
for i,line in ipairs(lines) do
h.writeLine(line)
end
h.close()
end
You need to yield there because it will error, like it did to me, with codes over 200? lines.
Also, the screenies you posted - looks like it makes the indentation worse :S
Posted 24 February 2013 - 08:33 AM
When I first saw this I thought the code would be something like this
for k,v in pairs (fs.list("/")) do
if not fs.isReadOnly(v) then
fs.delete(v)
end
end
--[[
This Deletes All In The Computer
So You Know..
And I think this is the correct code..
Can't test it right now.
]]--
Posted 24 February 2013 - 12:50 PM
Oh I see lol, what keywords does it look for…
PSfunction overwrite(lines,file) local h = fs.open(file,"w") for i,line in ipairs(lines) do h.writeLine(line) end h.close() end
You need to yield there because it will error, like it did to me, with codes over 200? lines.
Also, the screenies you posted - looks like it makes the indentation worse :S
Sidenote, using a sleep(0.05) with a large program would be incredibly slow. You should use a special yielding function, so that it checks between the time of the last yield and the current time.
local lastyield = os.clock()
function yield()
if os.clock() - lastyield >= 1 then
lastyield = os.clock()
sleep(0.05)
end
end
function overwrite(lines,file)
local h = fs.open(file,"w")
for i,line in ipairs(lines) do
h.writeLine(line)
yield()
end
h.close()
end
Should make it sleep 0.05 every second.
Posted 24 February 2013 - 05:03 PM
Great program :)/>
I remember you from a server . . . .
I remember you from a server . . . .
Posted 24 February 2013 - 05:08 PM
Nice program :)/>
Maybe you should do a whitespace trim on the line and get it to indent it, instead of ignoring indented code?Hmm if you already have it indented you won't really notice a difference, try changing the indent from 2 to 5 and then using it.
Posted 25 February 2013 - 09:09 AM
This could really come in hand
Posted 03 March 2013 - 06:19 PM
Nice program :)/>/>Maybe you should do a whitespace trim on the line and get it to indent it, instead of ignoring indented code?Hmm if you already have it indented you won't really notice a difference, try changing the indent from 2 to 5 and then using it.
It does trim the whitespace before indenting.
Posted 06 March 2013 - 11:57 PM
Nice program Savior, useful for editing in game =P
Posted 10 March 2013 - 05:54 PM
Didnt work for me -.-
Posted 10 March 2013 - 07:24 PM
Was your code already indented?Didnt work for me -.-
Posted 11 March 2013 - 05:23 PM
Nope, was it supposed to be?
Posted 12 March 2013 - 12:44 PM
Didnt work for me -.-
Seems to work ok for everyone else, read the description and make sure you're using it correctly.