Jump to content




FormatMe!

utility

18 replies to this topic

#1 savior67

  • Members
  • 33 posts

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

https://pastebin.com/fqdmwPav



Before

Posted Image



After

Posted Image

Posted Image



#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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.

#3 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 24 February 2013 - 07:13 AM

cool, But could use screenies.

#4 savior67

  • Members
  • 33 posts

Posted 24 February 2013 - 07:42 AM

View PostremiX, on 24 February 2013 - 07:12 AM, said:

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.

#5 TheVarmari

  • Members
  • 70 posts
  • LocationFinland

Posted 24 February 2013 - 07:48 AM

No pics no clicks

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 24 February 2013 - 08:07 AM

View Postsavior67, on 24 February 2013 - 07:42 AM, said:

View PostremiX, on 24 February 2013 - 07:12 AM, said:

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

#7 savior67

  • Members
  • 33 posts

Posted 24 February 2013 - 08:16 AM

View PostremiX, on 24 February 2013 - 08:07 AM, said:

View Postsavior67, on 24 February 2013 - 07:42 AM, said:

View PostremiX, on 24 February 2013 - 07:12 AM, said:

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.

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#9 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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.
]]--


#10 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 24 February 2013 - 12:50 PM

View PostremiX, on 24 February 2013 - 08:24 AM, said:

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.

#11 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 24 February 2013 - 05:03 PM

Great program :)

I remember you from a server . . . .

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 24 February 2013 - 05:08 PM

Nice program :)

View Postsavior67, on 24 February 2013 - 07:42 AM, said:

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?

#13 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 25 February 2013 - 09:09 AM

This could really come in hand

#14 savior67

  • Members
  • 33 posts

Posted 03 March 2013 - 06:19 PM

View PostTheOriginalBIT, on 24 February 2013 - 05:08 PM, said:

Nice program :)/>

View Postsavior67, on 24 February 2013 - 07:42 AM, said:

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.

#15 Kryptanyte

  • Members
  • 95 posts
  • LocationNew Zealand

Posted 06 March 2013 - 11:57 PM

Nice program Savior, useful for editing in game =P

#16 hego555

  • Members
  • 89 posts

Posted 10 March 2013 - 05:54 PM

Didnt work for me -.-

#17 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 10 March 2013 - 07:24 PM

View Posthego555, on 10 March 2013 - 05:54 PM, said:

Didnt work for me -.-
Was your code already indented?

#18 hego555

  • Members
  • 89 posts

Posted 11 March 2013 - 05:23 PM

Nope, was it supposed to be?

#19 savior67

  • Members
  • 33 posts

Posted 12 March 2013 - 12:44 PM

View Posthego555, on 10 March 2013 - 05:54 PM, said:

Didnt work for me -.-

Seems to work ok for everyone else, read the description and make sure you're using it correctly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users