This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
savior67's profile picture

FormatMe!

Started by savior67, 24 February 2013 - 04:37 AM
savior67 #1
Posted 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


remiX #2
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.
nutcase84 #3
Posted 24 February 2013 - 07:13 AM
cool, But could use screenies.
savior67 #4
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.
TheVarmari #5
Posted 24 February 2013 - 07:48 AM
No pics no clicks
remiX #6
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
savior67 #7
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 ~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

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.
remiX #8
Posted 24 February 2013 - 08:24 AM
Oh I see lol, what keywords does it look for…

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
TheOddByte #9
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.
]]--
Kingdaro #10
Posted 24 February 2013 - 12:50 PM
Oh I see lol, what keywords does it look for…

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

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.
AnthonyD98™ #11
Posted 24 February 2013 - 05:03 PM
Great program :)/>

I remember you from a server . . . .
theoriginalbit #12
Posted 24 February 2013 - 05:08 PM
Nice program :)/>

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.
Maybe you should do a whitespace trim on the line and get it to indent it, instead of ignoring indented code?
superaxander #13
Posted 25 February 2013 - 09:09 AM
This could really come in hand
savior67 #14
Posted 03 March 2013 - 06:19 PM
Nice program :)/>/>
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.
Maybe you should do a whitespace trim on the line and get it to indent it, instead of ignoring indented code?

It does trim the whitespace before indenting.
Kryptanyte #15
Posted 06 March 2013 - 11:57 PM
Nice program Savior, useful for editing in game =P
hego555 #16
Posted 10 March 2013 - 05:54 PM
Didnt work for me -.-
superaxander #17
Posted 10 March 2013 - 07:24 PM
Didnt work for me -.-
Was your code already indented?
hego555 #18
Posted 11 March 2013 - 05:23 PM
Nope, was it supposed to be?
savior67 #19
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.